#include <iostream> // std::cout #include <mutex> #include <thread> // std::thread #include <condition_variable> using namespace std; mutex mu; int flag = 10; condition_variable cv; void f(int num) { for (int i = 0; i < 50; i++) { unique_lock<mutex> lk(mu); while (flag != num) // 这里不能用if,一定要用while cv.wait(lk); // 条件变量调用wait会自动调用锁 for (int j = 0; j < num; ++j) { cout << j << " "; } cout << endl; flag = (num == 10) ? 100 : 10; cv.notify_one(); } } int main() { thread child(f, 10); f(100); child.join(); return 0; }
#include <iostream> // std::cout #include <mutex> #include <thread> // std::thread #include <condition_variable> using namespace std; mutex mu; int flag = 0; condition_variable cv; void f(int num) { for (int i = 0; i < 10; i++) { unique_lock<mutex> lk(mu); while (flag != num) // 这里不能用if 一定要用while cv.wait(lk); // 条件变量调用wait会自动调用锁 char c = ‘A‘ + flag; cout << c; flag = (flag+1)%3; cv.notify_all(); //唤醒其他进程 } } int main() { thread child1(f, 0); thread child2(f, 1); f(2); child1.join(); child2.join(); return 0; }
#include <iostream> // std::cout #include <mutex> #include <thread> // std::thread #include <condition_variable> using namespace std; mutex mu; int flag = 0; // 改变flag的值,就改变了1234的输出顺序 condition_variable cv; void f(int num) { for (int i = 0; i < 100; i++) { unique_lock<mutex> lk(mu); while (flag != num) cv.wait(lk); // 条件变量调用wait会自动调用锁 cout << num + 1 << " "; flag = (flag + 1) % 4; cv.notify_all(); //唤醒其他进程 } } int main() { thread child1(f, 0); thread child2(f, 1); thread child3(f, 2); f(3); child1.join(); child2.join(); child3.join(); cout << endl; //system("pause"); return 0; }
// future example #include <iostream> // std::cout #include <mutex> #include <thread> // std::thread #include <condition_variable> #include <atomic> #include <future> using namespace std; class rwlock { private: mutex _lock; condition_variable _wcon, _rcon; unsigned _writer, _reader; int _active; public: void read_lock() { unique_lock<mutex> lock(_lock); ++_reader; while (_active < 0 || _writer > 0) _rcon.wait(lock); --_reader; ++_active; } void write_lock() { unique_lock<mutex> lock(_lock); ++_writer; while (_active != 0) _wcon.wait(lock); --_writer; _active = -1; } void unlock() { unique_lock<mutex> lock(_lock); if (_active > 0) { --_active; if (_active == 0) _wcon.notify_one(); } else { _active = 0; if (_writer > 0) _wcon.notify_one(); else if (_reader > 0) _rcon.notify_all(); } } rwlock() :_writer(0), _reader(0), _active(0) { } }; void t1(rwlock* rwl) { while (1) { cout << "I want to write." << endl; rwl->write_lock(); cout << "writing..." << endl; this_thread::sleep_for(chrono::seconds(5)); rwl->unlock(); this_thread::sleep_for(chrono::seconds(5)); } } void t2(rwlock* rwl) { while (1) { cout << "t2-I want to read." << endl; rwl->read_lock(); cout << "t2-reading..." << endl; this_thread::sleep_for(chrono::seconds(1)); rwl->unlock(); } } void t3(rwlock* rwl) { while (1) { cout << "t3-I want to read." << endl; rwl->read_lock(); cout << "t3-reading..." << endl; this_thread::sleep_for(chrono::seconds(1)); rwl->unlock(); } } int main() { rwlock* rwl = new rwlock(); thread th1(t1, rwl); thread th2(t2, rwl); thread th3(t3, rwl); th1.join(); th2.join(); th3.join(); system("pause"); return 0; }
原文:https://www.cnblogs.com/qiang-wei/p/12431377.html