1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| class TestMutex { Mutex m_mutex; int count = 0;
public: void run() { while (count < 30) { MutexLocker locker(&m_mutex); std::cout << std::this_thread::get_id() << ": " << ++count << std::endl; } } };
int main(int argc, char *argv[]) { TestMutex test; std::thread t1(std::bind(&TestMutex::run, &test)); std::thread t2(std::bind(&TestMutex::run, &test)); t1.join(); t2.join();
return 0; }
|