当前位置: 首页>>代码示例>>C++>>正文


C++ atomic类代码示例

本文整理汇总了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;
    }
  }
}
开发者ID:HiroyukiMikita,项目名称:usc-clmc-ros-pkg,代码行数:26,代码来源:test_malloc_wrappers.cpp

示例2: atomic2

void atomic2(uint64_t cnt){
  for(uint64_t i=0; i<cnt; i++){
    total2.fetch_add(1);
    total2.fetch_sub(1);
    //total2++;
  }
}
开发者ID:zhuhk,项目名称:test-toys,代码行数:7,代码来源:t_c11.cpp

示例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;
  }
}
开发者ID:DemonGiggle,项目名称:ClangThreadSanitizerTests,代码行数:14,代码来源:threadsanitizer_atomic_int.cpp

示例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;
    }
}
开发者ID:00liujj,项目名称:dealii,代码行数:8,代码来源:reader_writer_lock.cpp

示例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;
}
开发者ID:jpoirier,项目名称:xplane-commviewer-plugin,代码行数:26,代码来源:commviewer.cpp

示例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;
}
开发者ID:DaiYamatta,项目名称:ALua,代码行数:29,代码来源:ccr.cpp

示例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));
	}
开发者ID:dingyiheng,项目名称:Concurrency,代码行数:7,代码来源:lock_free.cpp

示例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);
}
开发者ID:mokerjoke,项目名称:boost-svn,代码行数:32,代码来源:freelist_test.cpp

示例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;
}
开发者ID:DaiYamatta,项目名称:ALua,代码行数:59,代码来源:ccr.cpp

示例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 );
	}
开发者ID:dpalkowski,项目名称:iSuperColliderKit,代码行数:9,代码来源:scope_buffer.hpp

示例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;
		}
	}
开发者ID:Feder1co5oave,项目名称:GameOfLife,代码行数:12,代码来源:Barrier_atomic.hpp

示例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;
	}
开发者ID:Angeldude,项目名称:sonic-pi,代码行数:13,代码来源:scope_buffer.hpp

示例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) );
            }
        }
    }
开发者ID:adiog,项目名称:tbb,代码行数:30,代码来源:test_concurrent_monitor.cpp

示例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;
     }
 }
开发者ID:okalitova,项目名称:programming_tasks,代码行数:7,代码来源:futex_aquire_release.cpp

示例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);
	}*/
}
开发者ID:bobpaulin,项目名称:OpenCVFaceFinder,代码行数:30,代码来源:Main.cpp


注:本文中的atomic类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。