//--------------------------------------------------------------------------- // 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 on_connected() { _lock.lock(); _cond.notify_all(); connect_finish = true; _lock.unlock(); }
//--------------------------------------------------------------------------- // 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_open() { std::cout << "接続しました。" << std::endl; std::unique_lock<std::mutex> lock(sio_mutex); is_connected = true; // 接続処理が終わったのち、待っているメインスレッドを起こす sio_cond.notify_all(); }
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(); }
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 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)); } }
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; }
bool ThreadPool::close() { /*关闭线程池*/ std::lock_guard<std::mutex> lck(mtx); running=false; cond_var.notify_all(); for(auto thread:threadPool) { thread->join(); delete thread; } }
int main() { std::thread t1(f1); std::thread t2(f2); std::this_thread::sleep_for(std::chrono::milliseconds(100)); { L1 lk(m0); test1 = 1; test2 = 1; } cv.notify_all(); { std::this_thread::sleep_for(std::chrono::milliseconds(100)); L1 lk(m0); } t1.join(); t2.join(); assert(test1 == 2); assert(test2 == 2); }
// "run"コマンドのイベントリスナ void on_run(sio::event& e) { std::unique_lock<std::mutex> lock(sio_mutex); sio_queue.push(e.get_message()); // イベントをキューに登録し、待っているメインスレッドを起こす sio_cond.notify_all(); }