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


C++ mutex::try_lock方法代码示例

本文整理汇总了C++中std::mutex::try_lock方法的典型用法代码示例。如果您正苦于以下问题:C++ mutex::try_lock方法的具体用法?C++ mutex::try_lock怎么用?C++ mutex::try_lock使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在std::mutex的用法示例。


在下文中一共展示了mutex::try_lock方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: customer

void customer()
{
    std::unique_lock<std::mutex> graud(mut);

    if(mut.try_lock())
    {
        std::cout << "mutex unlocked after unique_lock" <<std::endl;
    }else{
        std::cout <<"mutex locked after unique_lock" <<std::endl;
    }

    while(flag == 0)
    {
        std::cout << "wait........." <<std::endl;
        cond.wait(graud);
    }


    if(mut.try_lock())
    {
        std::cout << "mutex unlocked after wait" <<std::endl;
    }else{
        std::cout <<"mutex locked after wait" <<std::endl;
    }

    std::cout << "flag = " << flag << std::endl;
}
开发者ID:wangmeijing,项目名称:thread,代码行数:27,代码来源:main.cpp

示例2: remove

    /**
     *  To remove from the list, we need to keep a pointer to the previous
     *  node, too.  Note that this is much easier on account of us having a
     *  sentinel
     */
    void remove(int val)
    {
		while (true) {
			if (linkMtx.try_lock()) { 
				// find the node whose val matches the request
				Node* prev = sentinel;
				Node* curr = prev->next;
				while (curr != NULL) {
					// if we find the node, disconnect it and end the search
					if (curr->val == val) {
						prev->next = curr->next;

						// delete curr...
						free(curr);
						break;
					}
					else if (curr->val > val) {
						// this means the search failed
						break;
					}
					// advance one node
					prev = curr;
					curr = prev->next;
				}
				linkMtx.unlock();
				return;
			}
		}
    }
开发者ID:iluoyi,项目名称:parallel-computing-course,代码行数:34,代码来源:List.hpp

示例3: deliver_new_event

					void deliver_new_event(std::size_t, const void* inbnd) {
#ifndef NDEBUG
						bool success = assert_single_threaded.try_lock();
						assert(success);
						AtScopeEnd ase{[&]{assert_single_threaded.unlock();}};
#endif
						//std::cout << "received message" << std::endl;
						if (on_first_message){
							//std::cout << "expected this message is size_t" << std::endl;
							recv.first = *((int*)(inbnd));
							on_first_message = false;
							event_fd.notify();
							//async_tick(c);
						}
						else{
							//std::cout << "expected this message is vector" << std::endl;
							recv.second = from_bytes<std::vector<unsigned char> >(nullptr,((char*) inbnd));
							{
								stringstream ss;
								ss << "received " << recv.second->size() << " element vector" << std::endl;
								//std::cout << ss.str();
							}
							on_first_message = true;
							event_fd.notify();
							//async_tick(c);
						}
					}
开发者ID:mpmilano,项目名称:mutils-networking,代码行数:27,代码来源:test_batched_connection_server.cpp

示例4: insert

    /**
     * insert method; find the right place in the list, add val so that it is
     * in sorted order; if val is already in the list, exit without inserting
     */
    void insert(int val)
    {
		while (true) {
			if (linkMtx.try_lock()) { 
				// traverse the list to find the insertion point
				Node* prev =sentinel;
				Node* curr = prev->next;
				while (curr != NULL) {
					if (curr->val >= val)
						break;
					prev = curr;
					curr = prev->next;
				}

				// now insert new_node between prev and curr
				//
				// NB: if the test fails, it means we quit the above loop on account
				// of /finding/ val, in which case we just exit
				if (!curr || ((curr->val) > val)) {
					// create the new node
					Node* i = (Node*)malloc(sizeof(Node));
					i->val = val;
					i->next = curr;
					// insert it
					prev->next = i;
				}
				linkMtx.unlock();
				return;
			}
		}
    }
开发者ID:iluoyi,项目名称:parallel-computing-course,代码行数:35,代码来源:List.hpp

示例5: attempt_10k_incresses

void attempt_10k_incresses() {
	for (auto i = 0; i < 10000; ++i)
		if (mtx.try_lock()) {	// only increase if currently not locked
			++counter;
			mtx.unlock();
		}
}
开发者ID:wfxr,项目名称:CppLab,代码行数:7,代码来源:Source.cpp

示例6: SimulateClient

void SimulateClient(InternetCafe* internetCafe, std::vector<Client*>& clients, unsigned int clientId, std::mutex& mtx)
{
	Client* curClient = nullptr;
	curClient = clients[clientId];
	bool clientIsRunning = true;

	if (curClient)
	{
		while (clientIsRunning)
		{
			if (mtx.try_lock())
			{
				if (internetCafe->RequestComputer(curClient->GetId(), curClient->GetMoney(), clientIsRunning))
				{
					curClient->SetMoney(curClient->GetMoney() - internetCafe->GetHireCost());
				}

				mtx.unlock();
			}

			std::this_thread::sleep_for(std::chrono::duration_cast<std::chrono::milliseconds>
				(std::chrono::duration<double, std::ratio<1, 1000>>(TIMESTEP)));
		}

		std::cout << "User " << curClient->GetId() << " has exited simulation" << std::endl;
	}
}
开发者ID:hyzor,项目名称:ThreadsAndLocks_Assignment1,代码行数:27,代码来源:main.cpp

示例7: run

        // the main device method
        virtual void run() {

            if(!opened) {

                scene->addItem(item);

                // lock the image viewer
                img_view_mutex.lock();

                // update the pixmap and unlock the image viewer
                show_image();

                // open a window to show the QGraphicsScene
                view->show();

                // set the flag to true
                opened = true;

            } else {

                if (img_view_mutex.try_lock())  {

                    // a new thread!
                    std::thread show_image_thread(&ImageViewerDevice::show_image, this);

                    // leave it alone
                    show_image_thread.detach();

                }

            }

        }
开发者ID:josiasalexandre,项目名称:smoothPursuit,代码行数:34,代码来源:ImageViewerDevice.hpp

示例8: wait_for_all_entering_ep

/**
 * I hope everyone come inside, and everyone know
 * how many had been here
 */
int wait_for_all_entering_ep()
{  
    static std::atomic<bool> wait_finish;
    static std::atomic<int> thread_inside_count;
    static std::atomic<int> saved_thread_inside_count;

    static std::mutex mtx;

    wait_finish = false;
    thread_inside_count++;
    if (mtx.try_lock())
    {  
        while (thread_inside_count != get_expected_thread_count())
        {  
            // Well, furiosly infinit looping until all the threads runnin in the 
            // kernel are trapped here
        }
        saved_thread_inside_count = thread_inside_count.load();
        wait_finish = true;
        mtx.unlock();
    }
    else
    {
        while (wait_finish == false);
    }

    thread_inside_count--;
    return saved_thread_inside_count.load();
}
开发者ID:DemonGiggle,项目名称:thread_issues,代码行数:33,代码来源:test.cpp

示例9: try_lock

 bool try_lock()
 {
     check_for_hierarchy_violation();
     if(!internal_mutex.try_lock())
         return false;
     update_hierarchy_value();
     return true;
 }
开发者ID:GoatHunter,项目名称:concurrency-in-action,代码行数:8,代码来源:listing_3.8.cpp

示例10: attempt_10k_inc

void attempt_10k_inc() {
    for (int i=0; i<10000; ++i) {
        if (mtx.try_lock()) {
            // only increase if lock can be got successfully
            ++counter;
            mtx.unlock();
        }
    }
}
开发者ID:FinixLei,项目名称:C_CPP_Samples,代码行数:9,代码来源:thread_04_mutex.cpp

示例11: ConnectOnInput

void ConnectOnInput(int wiimote_number)
{
  if (!g_wiimotes_mutex.try_lock())
    return;

  if (g_wiimotes[wiimote_number])
    g_wiimotes[wiimote_number]->ConnectOnInput();

  g_wiimotes_mutex.unlock();
}
开发者ID:FLyrfors,项目名称:dolphin,代码行数:10,代码来源:WiimoteReal.cpp

示例12: try_lock

  bool try_lock() {
    if (_mutex.try_lock())
      {
	_isLocked = true;
	return true;
      }
    else {
      _isLocked = false;
      return false;
    }
  };
开发者ID:antgar,项目名称:rtype_cpp,代码行数:11,代码来源:Mutex.hpp

示例13: iDroneFSM

void iDroneFSM() {

    while (run) {
        if (navLock.try_lock()) {
            fsm.act(model);
            navLock.unlock();
        }


        //std::this_thread::sleep_for(std::chrono::seconds(2));

    }
}
开发者ID:Witnessthis,项目名称:iDrone,代码行数:13,代码来源:InitNode.cpp

示例14: threadfunction_mutex_try_lock

void threadfunction_mutex_try_lock(int arg) 
{
  long long count = 0;
  while(true) {
    if(coutLock.try_lock()) {
        cout << "mutex try_lock: " << arg << ", this_thread::get_id=" << this_thread::get_id() << ", tried " << count << " times before we attained the lock" << "\n";
      coutLock.unlock();
      break;
    } else { // lock is busy, do something else.  Let's count the number of times we tried.
      count++;
    }
  }
}
开发者ID:DanBrennan33,项目名称:SenecaOOP345-attic,代码行数:13,代码来源:threading101d.cpp

示例15: thread_function_increase

void thread_function_increase()
{
    for (int i=0; i<3; i++)
    {
        if(g_counter_mutex.try_lock())
            //g_counter_mutex.lock();
        {
            ++g_counter;
            cout << this_thread::get_id() << ": " << i << endl;
            g_counter_mutex.unlock();
            this_thread::sleep_for(std::chrono::seconds(2));
        }
    }
}
开发者ID:guyueguakenan,项目名称:StudyProjects,代码行数:14,代码来源:Mutex.cpp


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