Visual Studio 11增强支持的标准 C + + 11 现在支持此预览的 Visual Studio 头的 STL 中的新头文件可以进行多线程编程和异步操作管理。 <thread>,<future>,<atomic>,<time>,<mutex>,<condition_variable>,<ratio>,<filesystem> 头文件<thread>作为其名称来创建和操作线程
[cpp] view plaincopy
- 1.thread t([]()
- 2. {
- 3. cout << "ThreadID : " << std::this_thread::get_id() << endl;
- 4. });
- 5. t.join();
这是传递给线程的类的构造函数的一种方法,而不是在这里我们使用Lambda 表达式中引入C + + 11Join ()方法,这是一个调用阻塞,使主线程等待,直到线程完成他的工作。如果要解耦变量的类型线程,线程在 Windows 那里 调用 的detach()方法,这样做违背计划的detach()方法,不会影响与线程句柄关联的窗口 (CloseHandle)。因此可能是使用变量的 t 型线,旧 Windows API 通过检索的本机句柄,但代码将成为便携式少得多。
[cpp] view plaincopy
- 1.WaitForSingleObject(t.native_handle()._Hnd ,INFINITE);
- 2. t.detach();
在线程, join ()方法是实质相同,上述代码 (在 Windows 平台) 。 很可能也与要检索的可用使用hardware_concurrency()方法的虚拟处理器数目的线程 ,
[cpp] view plaincopy
- unsigned numLogicalProc=t.hardware_concurrency();
操作的线程,总是会对同步与保护的关键地区。头<mutex>提供这种排斥同步对象相互示例的效果
注意,使用锁来总是对性能的影响 ! [cpp] view plaincopy
- std::this_thread::sleep_for (chrono::seconds(1));
- 5. for(int i=0;i<10;i++)
- 6. {
- 7. m.lock();
- 8. cout << "ThreadID : " << std::this_thread::get_id() << ":" << i << endl;
- 9. m.unlock ();
- 10. }
- 11.});
- 12.thread t2([&m]()
- 13.{
- 14. std::this_thread::sleep_for (chrono::seconds(1));
- 15. for(int i=0;i<10;i++)
- 16. {
- 17. m.lock ();
- 18. cout << "ThreadID : " << std::this_thread::get_id() << ":" << i << endl;
- 19. m.unlock();
- 20. }
- 21.});
- 22.t1.join();
- 23.t2.join();
注意this_thread命名空间以检索当前线程的标识号或时间类结合创建点的介绍. 它也是执行的可以控制对生产者/消费者下面的示例使用头文件<condition_variable>,作为多个线程流。
注意到我们使消费者和生产者为互斥体,我们转向方法wait()变量的类型condition_variable_any (它可能还使用condition_variable unique_lock <mutex>型,后者互斥体直接传递到类型unique_lock的初始化过程中未报告的状态。非终止状态指示可以获得互斥体。)
[cpp] view plaincopy
- 1.mutex lockBuffer;
- 2.volatile BOOL ArretDemande=FALSE;
- 3.queue<long> buffer;
- 4.condition_variable_any cndNotifierConsommateurs;
- 5.condition_variable_any cndNotifierProducteur;
- 6.
- 7.thread ThreadConsommateur([&]()
- 8.{
- 9.
- 10. while(true)
- 11. {
- 12.
- 13. lockBuffer.lock ();
- 14. while(buffer.empty () && ArretDemande==FALSE)
- 15. {
- 16. cndNotifierConsommateurs.wait(lockBuffer);
- 17. }
- 18. if (ArretDemande==TRUE && buffer.empty ())
- 19. {
- 20. lockBuffer.unlock();
- 21. cndNotifierProducteur.notify_one ();
- 22. break;
- 23. }
- 24.
- 25. long element=buffer.front();
- 26. buffer.pop ();
- 27. cout << "Consommation element :" << element << " Taille de la file :" << buffer.size() << endl;
- 28.
- 29. lockBuffer.unlock ();
- 30. cndNotifierProducteur.notify_one ();
- 31. }
- 32.
- 33.});
- 34.
- 35.thread ThreadProducteur([&]()
- 36.{
- 37. //Operation atomic sur un long
- 38. std::atomic<long> interlock;
- 39. interlock=1;
- 40. while(true)
- 41. {
- 42. ////Simule une charge
- 43. std::this_thread::sleep_for (chrono::milliseconds (15));
- 44. long element=interlock.fetch_add (1);
- 45. lockBuffer.lock ();
- 46. while(buffer.size()==10 && ArretDemande ==FALSE)
- 47. {
- 48.
- 49. cndNotifierProducteur.wait (lockBuffer);
- 50. }
- 51. if (ArretDemande==TRUE)
- 52. {
- 53.
- 54. lockBuffer.unlock ();
- 55. cndNotifierConsommateurs.notify_one ();
- 56. break;
- 57. }
- 58. buffer.push(element);
- 59. cout << "Production unlement :" << element << " Taille de la file :" << buffer.size() << endl;
- 60. lockBuffer.unlock ();
- 61. cndNotifierConsommateurs.notify_one ();
- 62. }
- 63.
- 64.});
- 65.
- 66.
- 67.std::cout << "Pour arreter pressez [ENTREZ]" << std::endl;
- 68.getchar();
- 69.
- 70.std::cout << "Arret demande" << endl;
- 71.ArretDemande=TRUE;
- 72.
- 73.ThreadProducteur.join();
- 74.ThreadConsommateur.join();
在示例中,该互斥体将传递给无信号使用锁() 方法。不过如果队列为空 ,就可以开始在执行序列中执行。
此互斥体用来保护尾 <int> 缓冲区类型。等待() 方法使用另一种机制将这挂起,并将等待唤醒,制造者线程仅当它将调用它的方法notify_one()。
使用这里的元素类型,递增 1 在单个原子操作中我们的队列的元素。在多线程的上下文,另外,例如将总是公平的保证元素操作,而不是抢占式。
头文件<future>。未来用于执行异步操作的返回结果,要检索后,没有不同步或线程流量控制机制。示例中,作为互斥体的多个线程的交会点的方法 join () 和控制流对象。 事实上,假设您想要简单的加法的两个整数 A + B,但是来自两个不同的线程所返回的结果。 在下面的示例中,作为不确定何时执行的概念
[cpp] view plaincopy
- 1.std::cout << "Thread Principale : ID : " << std::this_thread::get_id() << endl;
- 2. future<int> f1(async([]()->int
- 3. {
- 4. //Simule une charge
- 5. std::this_thread::sleep_for (chrono::milliseconds (2000));
- 6. std::cout << "Future 1 ID : " << std::this_thread::get_id() << endl;
- 7.
- 8. return 42;
- 9. }));
- 10.
- 11. future<int> f2(async([]()->int
- 12. {
- 13.
- 14. std::cout << "Future 2 ID : " << std::this_thread::get_id() << endl;
- 15.
- 16. return 84;
- 17. }));
- 18.
- 19. std::cout << "Resultat : " << f1.get () + f2.get() << endl ;
- 20.
在这里宣布int类型的两个数值以异步类型作为参数的构造函数,它作为其名称在不同的线程中执行异步操作的指示。
两个未来将返回的结果,但不知道何时执行Get ()方法,这是一个调用中担保两个整数的增加会正确的范例。
在将来的VS11调用中,我们使用语法强烈靠近同步语法的异步执行。
|