本文整理汇总了C++中boost::atomic::fetch_add方法的典型用法代码示例。如果您正苦于以下问题:C++ atomic::fetch_add方法的具体用法?C++ atomic::fetch_add怎么用?C++ atomic::fetch_add使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类boost::atomic
的用法示例。
在下文中一共展示了atomic::fetch_add方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: run_inlet
// run an inlet for some time (optionally with sporadic interruptions in between)
void run_inlet(const double duration_=0.0, const string name_=string(), const string type_=string(), const int in_chunks_=-1, const int request_info_=-1, const int request_time_=-1, const double seconds_between_failures_=0.0) {
num_inlets.fetch_add(1);
std::ostringstream s; s << boost::this_thread::get_id();
srand((unsigned)boost::hash<string>()(s.str()));
try {
// choose random parameters if desired
double duration = (duration_ == 0.0) ? 1.0+rand()%(max_outlet_duration-1) : duration_;
string name = name_.empty() ? names[rand()%(sizeof(names)/sizeof(names[0]))] : name_;
string type = type_.empty() ? types[rand()%(sizeof(types)/sizeof(types[0]))] : type_;
int request_info = (request_info_==-1) ? rand()%3 == 0 : request_info_;
int request_time = (request_time_==-1) ? rand()%3 == 0 : request_time_;
double seconds_between_failures = (seconds_between_failures_ == 0.0) ? (inlet_min_failure_interval_ms+rand()%outlet_max_failure_interval_ms)/1000.0 : seconds_between_failures_;
// resolve by type...
vector<lsl::stream_info> results = lsl::resolve_stream("type",type,1,5);
if (results.empty())
throw lsl::lost_error("No stream found.");
lsl::stream_info result = results[rand()%results.size()];
vector<float> chunk;
// and run...
double t=0.0;
for (double endtime = lsl::local_clock()+duration;lsl::local_clock()<endtime;) {
// run a single execution of the inlet
{
cout << "new inlet(" << name << "," << type << ")...";
lsl::stream_inlet inlet(result,max_buffered);
cout << "done." << endl;
int numchans = inlet.info().channel_count();
init_sample(numchans*(int)ceil(max_chunk_len_ms*result.nominal_srate()/1000*max_chunk_oversize_factor),chunk);
if (request_info) {
cout << " info = " << inlet.info(1.0).name() << endl;
}
for (double fail_at = lsl::local_clock()+seconds_between_failures;lsl::local_clock()<fail_at;) {
boost::this_thread::sleep(boost::posix_time::milliseconds(1+rand()%max_inlet_poll_interval_ms));
inlet.pull_chunk_multiplexed(&chunk[0],NULL,chunk.size(),0);
if (request_time)
t = inlet.time_correction(1.0);
}
}
cout << "del inlet(" << name << "," << type << ")" << endl;
if (request_time)
cout << " tcorr = " << t << endl;
// downtime
boost::this_thread::sleep(boost::posix_time::millisec(100*(rand()%50)));
}
}
catch(lsl::timeout_error &) { std::cerr << "Timeout exceeded; stopping inlet." << std::endl; }
catch(lsl::lost_error &) { std::cerr << "Found no matching outlet; stopping inlet." << std::endl; }
catch(std::exception &e) {
std::cerr << "ERROR during run_inlet() Stress-test function: " << e.what() << std::endl;
}
num_inlets.fetch_sub(1);
}
示例2: call_void
void call_void()
{
++count_call_void;
// make sure this function is not concurrently invoked
HPX_TEST_EQ(count_active_call_void.fetch_add(1) + 1, 1);
hpx::this_thread::suspend(boost::chrono::microseconds(100));
--count_active_call_void;
HPX_TEST_EQ(count_active_call_void.load(), 0);
}
示例3: SessionMaintainerRequest
explicit SessionMaintainerRequest(ApiFunctor * af) :
af_(*af)
{
#if ! defined(NDEBUG)
const int object_count = session_maintainer_request_count.fetch_add(1,
boost::memory_order_relaxed);
# ifdef OUTPUT_DEBUG // Debug code
std::cout << "++SessionMaintainerRequest: " << (object_count+1) << std::endl;
# endif
assert( object_count < 100000 );
#endif
}
示例4: sizeof
TORRENT_NO_RETURN TORRENT_EXPORT void assert_fail(char const* expr, int line
, char const* file, char const* function, char const* value, int kind)
{
#ifdef TORRENT_PRODUCTION_ASSERTS
// no need to flood the assert log with infinite number of asserts
if (assert_counter.fetch_add(1) + 1 > 500) return;
#endif
char stack[8192];
stack[0] = '\0';
print_backtrace(stack, sizeof(stack), 0);
char const* message = "assertion failed. Please file a bugreport at "
"http://code.google.com/p/libtorrent/issues\n"
"Please include the following information:\n\n"
"version: " LIBTORRENT_VERSION "\n"
LIBTORRENT_REVISION "\n";
switch (kind)
{
case 1:
message = "A precondition of a libtorrent function has been violated.\n"
"This indicates a bug in the client application using libtorrent\n";
}
assert_print("%s\n"
#ifdef TORRENT_PRODUCTION_ASSERTS
"#: %d\n"
#endif
"file: '%s'\n"
"line: %d\n"
"function: %s\n"
"expression: %s\n"
"%s%s\n"
"stack:\n"
"%s\n"
, message
#ifdef TORRENT_PRODUCTION_ASSERTS
, assert_counter.load()
#endif
, file, line, function, expr
, value ? value : "", value ? "\n" : ""
, stack);
// if production asserts are defined, don't abort, just print the error
#ifndef TORRENT_PRODUCTION_ASSERTS
// send SIGINT to the current process
// to break into the debugger
raise(SIGINT);
abort();
#endif
}
示例5: run_outlet
// run an outlet for some time (optionally with sporadic interruptions in between)
void run_outlet(const double duration_=0.0, const string name_=string(), const string type_=string(), const int numchan_=0, const lsl::channel_format_t fmt_=lsl::cf_undefined, const double srate_=0.0, const double seconds_between_failures_=0.0, const int chunk_len_=0) {
num_outlets.fetch_add(1);
std::ostringstream s; s << boost::this_thread::get_id();
srand((unsigned)boost::hash<string>()(s.str()));
try {
// choose random parameters if desired
double duration = (duration_ == 0.0) ? 1.0+rand()%(max_outlet_duration-1) : duration_;
string name = name_.empty() ? names[rand()%(sizeof(names)/sizeof(names[0]))] : name_;
string type = type_.empty() ? types[rand()%(sizeof(types)/sizeof(types[0]))] : type_;
int numchan = (numchan_ == 0) ? 1+(rand()%(max_channels-1)) : numchan_;
double srate = (srate_ == 0.0) ? 1.0 + (rand()%(max_srate-1)) : srate_;
lsl::channel_format_t fmt = (fmt_ == lsl::cf_undefined) ? fmts[rand()%6] : fmt_;
double seconds_between_failures = (seconds_between_failures_ == 0.0) ? (inlet_min_failure_interval_ms+rand()%outlet_max_failure_interval_ms)/1000.0 : seconds_between_failures_;
int chunk_len = (chunk_len_ == 0) ? std::max(min_chunk_len_ms,(rand()%max_chunk_len_ms)) : chunk_len_;
// create a new streaminfo
lsl::stream_info info(name,type,numchan,srate,fmt,boost::uuids::to_string(boost::uuids::random_generator()()));
// initialize data to send
vector<float> chunk;
init_sample((int)(numchan*floor(chunk_len*srate/1000*max_chunk_oversize_factor)),chunk);
// and run...
for (double endtime = lsl::local_clock()+duration;lsl::local_clock()<endtime;) {
// run a single execution of the outlet
{
cout << "new outlet(" << name << "," << type << "," << numchan << "," << fmt << "," << srate << ")...";
lsl::stream_outlet outlet(info,0,max_buffered);
cout << "done." << endl;
// send in bursts
double now, start_time = lsl::local_clock(), fail_at=start_time+seconds_between_failures;
for (int target,diff,written=0;written<max_samples && !stop_outlet;written+=diff) {
boost::this_thread::sleep(boost::posix_time::milliseconds(chunk_len));
now = lsl::local_clock();
if (now>fail_at)
break;
target = (int)floor((now-start_time)*srate);
int num_elements = (int)std::min((std::size_t)((target-written)*numchan),chunk.size());
if (num_elements)
outlet.push_chunk_multiplexed(&chunk[0],num_elements);
diff = num_elements/numchan;
}
}
cout << "del outlet(" << name << "," << type << "," << numchan << "," << fmt << "," << srate << ")" << endl;
// downtime
boost::this_thread::sleep(boost::posix_time::millisec(100*(rand()%50)));
}
} catch(std::exception &e) {
std::cerr << "ERROR during run_outlet() Stress-test function: " << e.what() << std::endl;
}
num_outlets.fetch_sub(1);
}
示例6: make_ready_future
hpx::future<void> plain_future_void()
{
++count_plain_future_void;
// make sure this function is not concurrently invoked
HPX_TEST_EQ(count_active_plain_future_void.fetch_add(1) + 1, 1);
hpx::this_thread::suspend(boost::chrono::microseconds(100));
--count_active_plain_future_void;
HPX_TEST_EQ(count_active_plain_future_void.load(), 0);
return hpx::make_ready_future();
}
示例7: push_back
void push_back(T elem) {
// Construct an element to hold it
synclist_item<T>* itm = new synclist_item<T>();
itm->value = elem;
itm->prev.store(m_last.load(boost::memory_order_release), boost::memory_order_acquire);
itm->next.store(NULL, boost::memory_order_acquire);
// Insert the element in the list
synclist_item<T>* tmpItm = itm;
synclist_item<T>* prevLast = m_last.exchange(tmpItm, boost::memory_order_consume);
tmpItm = itm;
synclist_item<T>* null = NULL;
m_first.compare_exchange_strong(null, tmpItm, boost::memory_order_consume, boost::memory_order_acquire);
if(prevLast != NULL) {
prevLast->next.store(itm, boost::memory_order_consume);
}
m_length.fetch_add(1, boost::memory_order_consume);
}
示例8:
bool
test_arithmetic(boost::atomic<value_type> & shared_value, size_t instance)
{
size_t shift = instance * 8;
value_type mask = 0xff << shift;
value_type increment = 1 << shift;
value_type expected = 0;
for (size_t n = 0; n < 255; n++) {
value_type tmp = shared_value.fetch_add(increment, boost::memory_order_relaxed);
if ( (tmp & mask) != (expected << shift) )
return false;
expected ++;
}
for (size_t n = 0; n < 255; n++) {
value_type tmp = shared_value.fetch_sub(increment, boost::memory_order_relaxed);
if ( (tmp & mask) != (expected << shift) )
return false;
expected --;
}
return true;
}
示例9: generateIDNumberString
UString generateIDNumberString() {
return uint64ToString(idNumberString.fetch_add(1));
}
示例10: generateIDNumber
uint32 generateIDNumber() {
return idNumber.fetch_add(1);
}
示例11: thread
void thread()
{
a.fetch_add(1, boost::memory_order_relaxed);
}