Visual Studio 11开发指南(16)C++11更

Visual Studio 11增强支持的标准 C + + 11

现在支持此预览的 Visual Studio 头的 STL 中的新头文件可以进行多线程编程和异步操作管理。

,,,,,,, 头文件作为其名称来创建和操作线程 ~~~ thread t( { cout << "ThreadID : " << std::this_thread::get_id() << endl; }); t.join(); ~~~ 这是传递给线程的类的构造函数的一种方法,而不是在这里我们使用Lambda 表达式中引入C + + 11Join ()方法,这是一个调用阻塞,使主线程等待,直到线程完成他的工作。如果要解耦变量的类型线程,线程在 Windows 那里 调用 的detach()方法,这样做违背计划的detach()方法,不会影响与线程句柄关联的窗口 (CloseHandle)。因此可能是使用变量的 t 型线,旧 Windows API 通过检索的本机句柄,但代码将成为便携式少得多。 ~~~ WaitForSingleObject(t.native_handle()._Hnd ,INFINITE); t.detach(); ~~~ 在线程, join ()方法是实质相同,上述代码 (在 Windows 平台) 。 很可能也与要检索的可用使用hardware_concurrency()方法的虚拟处理器数目的线程 , ~~~ unsigned numLogicalProc=t.hardware_concurrency();  ~~~ 操作的线程,总是会对同步与保护的关键地区。头提供这种排斥同步对象相互示例的效果 注意,使用锁来总是对性能的影响 ! ~~~ std::this_thread::sleep_for (chrono::seconds(1)); for(int i=0;i<10;i++) { m.lock(); cout << "ThreadID : " << std::this_thread::get_id() << ":" << i << endl; m.unlock (); } }); thread t2([&m]() { std::this_thread::sleep_for (chrono::seconds(1)); for(int i=0;i<10;i++) { m.lock (); cout << "ThreadID : " << std::this_thread::get_id() << ":" << i << endl; m.unlock(); } }); t1.join(); t2.join(); ~~~ 注意this_thread命名空间以检索当前线程的标识号或时间类结合创建点的介绍. 它也是执行的可以控制对生产者/消费者下面的示例使用头文件,作为多个线程流。 注意到我们使消费者和生产者为互斥体,我们转向方法wait()变量的类型condition_variable_any (它可能还使用condition_variable unique_lock 型,后者互斥体直接传递到类型unique_lock的初始化过程中未报告的状态。非终止状态指示可以获得互斥体。) ~~~ mutex lockBuffer; volatile BOOL ArretDemande=FALSE; queue buffer; condition_variable_any cndNotifierConsommateurs; condition_variable_any cndNotifierProducteur; thread ThreadConsommateur([&]() { while(true) { lockBuffer.lock (); while(buffer.empty () && ArretDemande==FALSE) { cndNotifierConsommateurs.wait(lockBuffer); } if (ArretDemande==TRUE && buffer.empty ()) { lockBuffer.unlock(); cndNotifierProducteur.notify_one (); break; } long element=buffer.front(); buffer.pop (); cout << "Consommation element :" << element << " Taille de la file :" << buffer.size() << endl; lockBuffer.unlock (); cndNotifierProducteur.notify_one (); } }); thread ThreadProducteur([&]() { //Operation atomic sur un long std::atomic interlock; interlock=1; while(true) { ////Simule une charge std::this_thread::sleep_for (chrono::milliseconds (15)); long element=interlock.fetch_add (1); lockBuffer.lock (); while(buffer.size()==10 && ArretDemande ==FALSE) { cndNotifierProducteur.wait (lockBuffer); } if (ArretDemande==TRUE) { lockBuffer.unlock (); cndNotifierConsommateurs.notify_one (); break; } buffer.push(element); cout << "Production unlement :" << element << " Taille de la file :" << buffer.size() << endl; lockBuffer.unlock (); cndNotifierConsommateurs.notify_one (); } }); std::cout << "Pour arreter pressez [ENTREZ]" << std::endl; 68.getchar(); std::cout << "Arret demande" << endl; ArretDemande=TRUE; ThreadProducteur.join(); ThreadConsommateur.join(); ~~~ 在示例中,该互斥体将传递给无信号使用锁() 方法。不过如果队列为空 ,就可以开始在执行序列中执行。 此互斥体用来保护尾 缓冲区类型。等待() 方法使用另一种机制将这挂起,并将等待唤醒,制造者线程仅当它将调用它的方法notify_one()。 使用这里的元素类型,递增 1 在单个原子操作中我们的队列的元素。在多线程的上下文,另外,例如将总是公平的保证元素操作,而不是抢占式。   头文件。未来用于执行异步操作的返回结果,要检索后,没有不同步或线程流量控制机制。示例中,作为互斥体的多个线程的交会点的方法 join () 和控制流对象。 事实上,假设您想要简单的加法的两个整数 A + B,但是来自两个不同的线程所返回的结果。 在下面的示例中,作为不确定何时执行的概念   ~~~ std::cout << "Thread Principale : ID : " << std::this_thread::get_id() << endl; future f1(async(->int { //Simule une charge std::this_thread::sleep_for (chrono::milliseconds (2000)); std::cout << "Future 1 ID : " << std::this_thread::get_id() << endl; return 42; })); future f2(async(->int { std::cout << "Future 2 ID : " << std::this_thread::get_id() << endl; return 84; })); std::cout << "Resultat : " << f1.get () + f2.get() << endl ; ~~~ 在这里宣布int类型的两个数值以异步类型作为参数的构造函数,它作为其名称在不同的线程中执行异步操作的指示。 两个未来将返回的结果,但不知道何时执行Get ()方法,这是一个调用中担保两个整数的增加会正确的范例。 在将来的VS11调用中,我们使用语法强烈靠近同步语法的异步执行。   赶紧下载VS11体验吧 [http://www.microsoft.com/click/services/Redirect2.ashx?CR_CC=200098144](http://www.microsoft.com/click/services/Redirect2.ashx?CR_CC=200098144)  
文章导航