 b64f8b07c1
			
		
	
	b64f8b07c1
	
	
	
		
			
			git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@119395 91177308-0d34-0410-b5e6-96231b3b80d8
		
			
				
	
	
		
			57 lines
		
	
	
		
			1.0 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			57 lines
		
	
	
		
			1.0 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
| //===----------------------------------------------------------------------===//
 | |
| //
 | |
| //                     The LLVM Compiler Infrastructure
 | |
| //
 | |
| // This file is dual licensed under the MIT and the University of Illinois Open
 | |
| // Source Licenses. See LICENSE.TXT for details.
 | |
| //
 | |
| //===----------------------------------------------------------------------===//
 | |
| 
 | |
| // <condition_variable>
 | |
| 
 | |
| // class condition_variable;
 | |
| 
 | |
| // ~condition_variable();
 | |
| 
 | |
| #include <condition_variable>
 | |
| #include <mutex>
 | |
| #include <thread>
 | |
| #include <cassert>
 | |
| 
 | |
| std::condition_variable* cv;
 | |
| std::mutex m;
 | |
| typedef std::unique_lock<std::mutex> Lock;
 | |
| 
 | |
| bool f_ready = false;
 | |
| bool g_ready = false;
 | |
| 
 | |
| void f()
 | |
| {
 | |
|     Lock lk(m);
 | |
|     f_ready = true;
 | |
|     cv->notify_one();
 | |
|     delete cv;
 | |
| }
 | |
| 
 | |
| void g()
 | |
| {
 | |
|     Lock lk(m);
 | |
|     g_ready = true;
 | |
|     cv->notify_one();
 | |
|     while (!f_ready)
 | |
|         cv->wait(lk);
 | |
| }
 | |
| 
 | |
| int main()
 | |
| {
 | |
|     cv = new std::condition_variable;
 | |
|     std::thread th2(g);
 | |
|     Lock lk(m);
 | |
|     while (!g_ready)
 | |
|         cv->wait(lk);
 | |
|     lk.unlock();
 | |
|     std::thread th1(f);
 | |
|     th1.join();
 | |
|     th2.join();
 | |
| }
 |