本文整理汇总了C++中atomic类的典型用法代码示例。如果您正苦于以下问题:C++ atomic类的具体用法?C++ atomic怎么用?C++ atomic使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了atomic类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: statsThread
void statsThread(atomic<bool>& failed)
{
resetThreadAllocInfo();
for (uint32_t i = 1; i <= 1000; ++i)
{
void* mem = malloc(500);
free(mem);
ros::WallDuration(0.001).sleep();
AllocInfo info = getThreadAllocInfo();
if (info.mallocs != i)
{
ROS_ERROR_STREAM("mallocs is " << info.mallocs << " should be " << i);
failed.store(true);
return;
}
if (info.frees != i)
{
ROS_ERROR_STREAM("mallocs is " << info.frees << " should be " << i);
failed.store(true);
return;
}
}
}
示例2: atomic2
void atomic2(uint64_t cnt){
for(uint64_t i=0; i<cnt; i++){
total2.fetch_add(1);
total2.fetch_sub(1);
//total2++;
}
}
示例3: foo
int foo(atomic<int>& x)
{
for(size_t n = 0; ; ++n)
{
auto expected = x.load();
auto desired = 0;
x.compare_exchange_strong(
expected,
desired);
if(n == loop)
return desired;
}
}
示例4: fetch_and_and
// Perform an atomic bitwise-AND on the operand, and return its previous value.
inline uintptr_t fetch_and_and(atomic<uintptr_t>& operand, uintptr_t value) {
for (tbb::internal::atomic_backoff b;;b.pause()) {
uintptr_t old = operand;
uintptr_t result = operand.compare_and_swap(old&value, old);
if (result==old) return result;
}
}
示例5: CommandHandler
int CommandHandler(XPLMCommandRef inCommand, XPLMCommandPhase inPhase,
void* inRefcon)
{
// if (!gPluginEnabled.load()) {
// return IGNORED_EVENT;
// }
switch (reinterpret_cast<size_t>(inRefcon)) {
case CMD_CONTACT_ATC:
switch (inPhase) {
case xplm_CommandBegin:
case xplm_CommandContinue:
gPTT_On.store(true);
break;
case xplm_CommandEnd:
gPTT_On.store(false);
break;
default:
break;
}
break;
default:
break;
}
return IGNORED_EVENT;
}
示例6: ccr_dec_workers
// Finalizes a worker thread
static int ccr_dec_workers(lua_State *L)
{
int count = (int)luaL_checkinteger(L, 1);
lua_getfield(L, LUA_REGISTRYINDEX, CCR_SELF);
process_t *proc = (process_t*)lua_touserdata(L, -1);
// checks if the calling process is a main process
if(proc->main)
{
if (thr_pool.size() - count < THR_SIZE)
{
lua_pushinteger(L, 0);
lua_pushstring(L, "thread pool is already at the minimum size");
return 2;
}
// sets the numbers threads to kill
free_workers.fetch_and_add(count);
// sets the flag indication to kill threads
free_flag.compare_and_swap(true, false);
// returns the current number of threads in the pool
lua_pushinteger(L, (lua_Integer) thr_pool.size() - count);
return 1;
}
lua_pushinteger(L, 0);
lua_pushstring(L, "only a main process could free threads");
return 2;
}
示例7: push
void push(T const& data) {
node* const new_node = new node(data);
new_node->next = head.load();
//the cew check if the head equal to new_node->next, if it does, replace head with new_node
//if it doesn't, replace the new_node->next with head.(because meanwhile the head has already been modified)
while (!head.compare_exchange_weak(new_node->next, new_node));
}
示例8: run_test
void run_test(void)
{
freelist_type fl(std::allocator<int>(), 8);
std::set<dummy*> nodes;
dummy d;
if (bounded)
test_running.store(true);
for (int i = 0; i != 4; ++i) {
dummy * allocated = fl.template construct<threadsafe, bounded>();
BOOST_REQUIRE(nodes.find(allocated) == nodes.end());
nodes.insert(allocated);
}
BOOST_FOREACH(dummy * d, nodes)
fl.template destruct<threadsafe>(d);
nodes.clear();
for (int i = 0; i != 4; ++i)
nodes.insert(fl.template construct<threadsafe, bounded>());
BOOST_FOREACH(dummy * d, nodes)
fl.template destruct<threadsafe>(d);
for (int i = 0; i != 4; ++i)
nodes.insert(fl.template construct<threadsafe, bounded>());
if (bounded)
test_running.store(false);
}
示例9: ccr_worker
static void* ccr_worker(void *arg)
{
int r, resume;
process_t *proc;
while (1)
{
// Checks if threads need to be killed and the the ready procces's queue is empty
// That way only when the queue is empty the threads are killed
// if ((free_flag.compare_and_swap(false, true)) && (prc_ready.empty()))
if (free_flag.compare_and_swap(false, true))
{
pthread_t thread = pthread_self();
// removes reference from the pool
thr_pool.remove(thread);
// checks if threre's more threads to kill and set the flag
if (free_workers.fetch_and_decrement() > 1)
{
free_flag.compare_and_swap(true, false);
}
//kills the current thread
pthread_exit(NULL);
}
prc_ready.pop(proc);
if (!proc) return NULL;
r = lua_resume(proc->L, 0);
switch (r)
{
case LUA_YIELD:
//cerr << "Yield!\n";
switch (proc->status)
{
case PS_READY:
// releasing the lock acquired in ccr_yield
proc->wlock.release();
prc_ready.push(proc);
break;
case PS_BLOCKING:
proc->status = PS_BLOCKED;
// releasing the lock acquired in ccr_yield
proc->wlock.release();
break;
}
break;
case LUA_ERRRUN:
case LUA_ERRMEM:
case LUA_ERRERR:
cerr << "[ERROR][PROCESSING A LUA PROCESS] " << lua_tostring(proc->L, -1) << endl;
// fall-through
case 0:
lua_close(proc->L);
mbx_close(proc->mbox);
prc_free(proc);
break;
}
}
return NULL;
}
示例10: release
void release( scope_buffer_pool & pool )
{
bool allocated = _status.load( boost::memory_order_relaxed ) != free;
if( !allocated ) return;
pool.deallocate( _data.get() );
_status.store( free, boost::memory_order_release );
}
示例11: await
bool await(function<void()> cb = []{}) {
int my_gen = generation.load();
if (count.fetch_add(1) == N_THREADS - 1) {
if (cb) cb();
count.store(0);
generation.fetch_add(1);
return true;
} else {
do { } while (my_gen == generation.load());
return false;
}
}
示例12: pull
unsigned int pull()
{
int stage = _stage.load( std::memory_order_relaxed );
bool changed = _state[stage].changed.load( std::memory_order_relaxed );
if( changed )
{
_state[_out].changed.store( false, std::memory_order_relaxed );
_out = _stage.exchange( _out, std::memory_order_acquire );
return _state[_out].frames;
}
return 0;
}
示例13: operator
void operator()( int i ) const {
internal::concurrent_monitor::thread_context thr_ctx;
if( i==0 ) {
size_t n_expected_sleepers = NTHRS_USED_IN_DESTRUCTOR_TEST-1;
while( n_sleepers<n_expected_sleepers )
__TBB_Yield();
while( n_sleepers.compare_and_swap( VLN+NTHRS_USED_IN_DESTRUCTOR_TEST, n_expected_sleepers )!=n_expected_sleepers )
__TBB_Yield();
for( int j=0; j<100; ++j )
Harness::Sleep( 1 );
delete mon;
mon = NULL;
} else {
mon->prepare_wait( thr_ctx, uintptr_t(this) );
while( n_sleepers<VLN ) {
try {
++n_sleepers;
mon->commit_wait( thr_ctx );
if( --n_sleepers>VLN )
break;
} catch( tbb::user_abort& ) {
// can no longer access 'mon'
break;
}
mon->prepare_wait( thr_ctx, uintptr_t(this) );
}
}
}
示例14: unlock
void unlock() {
tid zero(0);
tid thread_id = this_thread::get_id();
if(!block.compare_exchange_strong(thread_id, zero, memory_order_release)) {
throw new exception;
}
}
示例15: numberOfFaces
void numberOfFaces(string filePath)
{
if (!instance.get()) {
CascadeClassifier* face_cascade = new CascadeClassifier();
face_cascade->load(face_cascade_name);
instance.reset(face_cascade);
}
Mat faceImage = imread(filePath, IMREAD_COLOR);
if (faceImage.empty()) // Check for invalid input
{
cout << "Could not open or find the image" << endl;
return;
}
Mat frame_gray;
std::vector<Rect> faces;
cvtColor(faceImage, frame_gray, CV_BGR2GRAY);
equalizeHist(frame_gray, frame_gray);
instance->detectMultiScale(frame_gray, faces, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(30, 30));
int numFaces = faces.size();
//cout << "Found " << numFaces << " faces in image: " << filePath << endl;
totalFaces.fetch_add(numFaces, boost::memory_order_seq_cst);
//cout << "Total: " << totalFaces << endl;
/*if (numFaces > 0)
{
drawFaceElipse(faceImage, faces);
}*/
}