void f() { typedef std::chrono::system_clock Clock; typedef std::chrono::milliseconds milliseconds; L1 lk(m0); assert(test2 == 0); test1 = 1; cv.notify_one(); Clock::time_point t0 = Clock::now(); while (test2 == 0 && cv.wait_for(lk, milliseconds(250)) == std::cv_status::no_timeout) ; Clock::time_point t1 = Clock::now(); if (runs == 0) { assert(t1 - t0 < milliseconds(250)); assert(test2 != 0); } else { assert(t1 - t0 - milliseconds(250) < milliseconds(5)); assert(test2 == 0); } ++runs; }
int main() { { L1 lk(m0); std::thread t(f); assert(test1 == 0); while (test1 == 0) cv.wait(lk); assert(test1 != 0); test2 = 1; lk.unlock(); cv.notify_one(); t.join(); } test1 = 0; test2 = 0; { L1 lk(m0); std::thread t(f); assert(test1 == 0); while (test1 == 0) cv.wait(lk); assert(test1 != 0); lk.unlock(); t.join(); } }
void f() { L1 lk(m0); assert(test2 == 0); test1 = 1; cv.notify_one(); cv.wait(lk, Pred(test2)); assert(test2 != 0); }
void unlock() { if ( --counter == 0 ) { cv.notify_all(); } else { std::unique_lock<sync_object_type> barrier_lock(sync); while(counter != 0) cv.wait_for(barrier_lock, std::chrono::milliseconds(1)); } }
//--------------------------------------------------------------------------- // function : unlock_write /// @brief this function unlock the write operation //--------------------------------------------------------------------------- void unlock_write( void) { //-------------------------- begin -------------------- std::unique_lock <spinlock> UL ( spl); assert ( tid == this_id() and nwrite > 0) ; nwrite -- ; if ( nwrite == 0 ) { cv_write.notify_all() ; cv_read.notify_all() ; }; };
void signals() { std::this_thread::sleep_for(std::chrono::milliseconds(120)); std::cerr << "Notifying...\n"; cv.notify_all(); std::this_thread::sleep_for(std::chrono::milliseconds(100)); i = 1; std::cerr << "Notifying again...\n"; cv.notify_all(); }
void insert(T t) { std::unique_lock<M> lock{mutex}; producers.wait(lock, [this]() { return begin != (end + 1) % SIZE; }); buffer[end] = t; end = (end + 1) % SIZE; consumers.notify_all(); }
T extract() { std::unique_lock<M> lock{mutex}; consumers.wait(lock, [this]() { return begin != end; }); T t = buffer[begin]; begin = (begin + 1) % SIZE; producers.notify_all(); return t; }
void _process(int id) { std::unique_lock<std::mutex> lock(io_datas[id]->mtx); if(io_datas[id]->empty()) { io_datas[id]->cond.wait(lock); /* In case that the queue is not used at the begining and it shall * make dequeue operation failed. */ if(io_datas[id]->empty()) { lock.unlock(); return; } } IoRef io = io_datas[id]->dequeue(); lock.unlock(); auto time_beg = std::chrono::steady_clock::now(); handle_io(io->offset, io->c); auto time_end = std::chrono::steady_clock::now(); auto duration = std::chrono::duration_cast<std::chrono::microseconds>(time_end - time_beg).count(); io_counter_per_thread[id]++; perf.consumed_time += duration; perf.io_finished++; prod_cond.notify_one(); }
// 接続時に呼び出されるイベントリスナ void on_open() { std::cout << "接続しました。" << std::endl; std::unique_lock<std::mutex> lock(sio_mutex); is_connected = true; // 接続処理が終わったのち、待っているメインスレッドを起こす sio_cond.notify_all(); }
void waits() { std::unique_lock<std::mutex> lk(cv_m); std::cout << "Waiting... \n"; cv.wait(lk, []{return i == 1;}); std::cout << "...finished waiting. i == 1\n"; }
void post() { std::lock_guard<std::mutex> lock(mutex_); //++count_; work_waiting = true; condition_.notify_one(); }
/*处理函数*/ void ThreadPool::process() { while(running) { try{ threadMtx.lock(); cond_var.wait(threadMtx); threadMtx.unlock(); Message message=msgQueue.pop(); //从消息队列获得消息 int socket=message.getFd(); //获取消息长度 uint32 size=0; Recv(socket,&size,sizeof(size),0); //获取消息内容 char *buffer=new char[size]; Recv(socket,buffer,size,MSG_WAITALL); //设置消息内容 message.setContent(buffer,size); delete[] buffer; parse(message); //交给parse解析 rmWork(); } catch(Exception &error) { error.exit(); } } }
//--------------------------------------------------------------------------- // function : unlock_read /// @brief This function unlock the read operation //--------------------------------------------------------------------------- void unlock_read ( void) { //-------------------------- begin -------------------- std::unique_lock <spinlock> UL ( spl); assert ( nread > 0 ); nread--; if ( nread == 0 ) cv_no_readers.notify_all() ; };
void on_connected() { _lock.lock(); _cond.notify_all(); connect_finish = true; _lock.unlock(); }
void waits(int idx) { std::unique_lock<std::mutex> lk(cv_m); if(cv.wait_for(lk, std::chrono::milliseconds(idx*100), [](){return i == 1;})) std::cerr << "Thread " << idx << " finished waiting. i == " << i << '\n'; else std::cerr << "Thread " << idx << " timed out. i == " << i << '\n'; }
void ComeToWork() { std::cout << "Hey security, please open the door!\n"; g_Bell.notify_one(); std::mutex mutex; mutex.lock(); g_Door.wait(mutex); mutex.unlock(); }
void wait() { std::unique_lock<std::mutex> lock(mutex_); //while(!count_) while(!work_waiting) condition_.wait(lock); work_waiting = false; //--count_; }
void signals() { std::this_thread::sleep_for(std::chrono::seconds(1)); { std::lock_guard<std::mutex> lk(cv_m); std::cout << "Notifying...\n"; } cv.notify_all(); std::this_thread::sleep_for(std::chrono::seconds(1)); { std::lock_guard<std::mutex> lk(cv_m); i = 1; std::cout << "Notifying again...\n"; } cv.notify_all(); }
//--------------------------------------------------------------------------- // function : wait_no_readers /// @brief This function wait until the number of readers over the data /// structure is 0 //--------------------------------------------------------------------------- void wait_no_readers () { struct shuttle { rw_mutex_data &r ; shuttle ( rw_mutex_data & Alfa):r(Alfa){ }; bool operator( )( void) { return (r.no_readers() );}; } S ( *this); cv_no_readers.wait ( mtx_no_readers,S ); };
void f1() { L1 lk(m0); assert(test1 == 0); while (test1 == 0) cv.wait(lk); assert(test1 == 1); test1 = 2; }
void set() { flag.store(true, std::memory_order_relaxed); std::lock_guard<std::mutex> lk(set_clear_mutex); if (thread_cond) thread_cond->notify_all(); else if (thread_cond_any) thread_cond_any->notify_all(); }
void f2() { L1 lk(m0); assert(test2 == 0); while (test2 == 0) cv.wait(lk); assert(test2 == 1); test2 = 2; }
void Worker::post(const QueryPtr& query) { { std::lock_guard<std::mutex> guard(mutex_); assert(follower_query_ == nullptr); follower_query_ = query; } cond_.notify_one(); }
/*供messagemanager调用*/ bool ThreadPool::addWork() { /*工作线程数加一*/ std::lock_guard<std::mutex> lck(mtx); if(isFull()) throw Exception(ERR_BAD,"线程池已经用完,无法提供ThreadPool::addWork()调用"); freeThreadNumber--; cond_var.notify_one();//唤醒一个线程,并从消息队列取消息处理 return true; }
//--------------------------------------------------------------------------- // function : lock_read /// @brief This function lock for the read operation. It's atomic /// @param [in] /// @return true : locked false : unlocked //--------------------------------------------------------------------------- void lock_read(void) { //--------------------- begin ---------------------------- struct shuttle { rw_mutex_data &r ; shuttle ( rw_mutex_data & Alfa):r(Alfa){ }; bool operator( )( void) { return (r.try_lock_read());}; } S ( *this); cv_read.wait ( mtx_read,S ); };
void Worker::post_to_queue(const QueryPtr& query) { if (!working_) return; { std::lock_guard<std::mutex> guard(mutex_); query_queue_.push_back(query); } cond_.notify_one(); }
// Worker thread void HttpClient::networkThread() { auto scheduler = Director::getInstance()->getScheduler(); while (true) { HttpRequest *request; // step 1: send http request if the requestQueue isn't empty { std::lock_guard<std::mutex> lock(s_requestQueueMutex); while (s_requestQueue->empty()) { s_SleepCondition.wait(s_requestQueueMutex); } request = s_requestQueue->at(0); s_requestQueue->erase(0); } if (request == s_requestSentinel) { break; } // step 2: libcurl sync access // Create a HttpResponse object, the default setting is http access failed HttpResponse *response = new (std::nothrow) HttpResponse(request); processResponse(response, s_errorBuffer); // add response packet into queue s_responseQueueMutex.lock(); s_responseQueue->pushBack(response); s_responseQueueMutex.unlock(); if (nullptr != s_pHttpClient) { scheduler->performFunctionInCocosThread(CC_CALLBACK_0(HttpClient::dispatchResponseCallbacks, this)); } } // cleanup: if worker thread received quit signal, clean up un-completed request queue s_requestQueueMutex.lock(); s_requestQueue->clear(); s_requestQueueMutex.unlock(); if (s_requestQueue != nullptr) { delete s_requestQueue; s_requestQueue = nullptr; delete s_responseQueue; s_responseQueue = nullptr; } }
HttpClient::~HttpClient() { if (s_requestQueue != nullptr) { { std::lock_guard<std::mutex> lock(s_requestQueueMutex); s_requestQueue->pushBack(s_requestSentinel); } s_SleepCondition.notify_one(); } s_pHttpClient = nullptr; }
void enqueue_io(IoRef &&io) { int id = (io->offset / (OBJECT_SIZE / BLOCK_SIZE)) % concurrence; std::unique_lock<std::mutex> lock(io_datas[id]->mtx); lock.unlock(); io_datas[id]->cond.notify_one(); perf.io_started++; if(perf.get_io_flight() > io_capacity) { std::unique_lock<std::mutex> prod_lock(prod_mtx); prod_cond.wait(prod_lock); } }