shopify网站建设,wordpress 国内加速,北京高端网站建设公司哪家好,永久免费建站系统C11开始支持多线程编程#xff0c;之前多线程编程都需要系统的支持#xff0c;在不同的系统下创建线程需要不同的API如pthread_create()#xff0c;Createthread()#xff0c;beginthread()等#xff0c;使用起来都比较复杂#xff0c;C11提供了新头文件thread、…C11开始支持多线程编程之前多线程编程都需要系统的支持在不同的系统下创建线程需要不同的API如pthread_create()Createthread()beginthread()等使用起来都比较复杂C11提供了新头文件thread、mutex、atomic、future等用于支持多线程。 使用C11开启一个线程是比较简单的下面来看一个简单的例子 #include thread #include iostream void hello() { std::cout Hello from thread std::endl; } int main() { std::thread t1(hello); t1.join(); std::coutMain Threadstd::endl; return 0; } 运行结果 说明通过thread 类直接申明一个线程t1,参数是这个线程执行的回调函数的地址通过jion()方法阻塞主线程直到t1线程执行结束为止。 C11支持Lambda表达式因此一个新线程的回调函数也可以是有一个Lambda表达式的形式但是注意如果使用Lambda表达式最好不要使用引用的方式应该使用值传递的方式来访问数据在多线程中使用引用容易造成混乱。下面这个例子稍微复杂创建了多个子线程并使用了get_id()方法来获取当前线程的id。 #include thread #include iostream #include vector int main() { std::vectorstd::thread threads; for(int i 0; i 5; i){ threads.push_back(std::thread([](){ std::cout Hello from lamda thread std::this_thread::get_id() std::endl; })); } for(auto thread : threads){ thread.join(); } std::coutMain Thread\tstd::this_thread::get_id()std::endl; return 0; } 运行结果 上述代码中使用vector来存放每个线程线程的回调函数通过Lambda表达式产生注意后面join的使用方式。 可以通过sleep_for来使线程睡眠一定的时间 #include thread #include iostream #include mutex using namespace std; int main() { std::mutex m; thread t1([m]() { std::this_thread::sleep_for (chrono::seconds(10)); for(int i0;i10;i) { m.lock(); cout In t1 ThreadID : std::this_thread::get_id() : i endl; m.unlock (); } } ); thread t2([m]() { std::this_thread::sleep_for (chrono::seconds(1)); for(int i0;i10;i) { m.lock (); cout In t2 ThreadID : std::this_thread::get_id() : i endl; m.unlock(); } } ); t1.join(); t2.join(); coutMain Threadendl; return 0; } 运行结果 可以看出由于线程t1睡眠的时间较长t2先执行了。 延时有这几种类型nanoseconds、microseconds、milliseconds、seconds、minutes、hours。 在使用多线程的程序中操作共享数据的时候一定要小心由于线程的乱序执行可能会得到意想不到的结果。通过下面的程序来看 #include thread #include iostream #include vector #include mutex struct Counter { std::mutex mutex; int value; Counter() : value(0) {} void increment(){ // mutex.lock(); 【1】表示没有使用锁 value; // mutex.unlock(); 【1】 } void decrement(){ mutex.lock(); --value; mutex.unlock(); } }; int main(){ Counter counter; std::vectorstd::thread threads; for(int i 0; i 5; i){ threads.push_back(std::thread([](){ for(int i 0; i 10000; i){ counter.increment(); } })); } for(auto thread : threads){ thread.join(); } std::cout counter.value std::endl; return 0; } 运行结果 【1】 运行结果使用了锁 说明由于创建线程是使用lambda表达式并使用引用的方式访问counter这个变量当没有使用lock来保护的时候情况【1】执行的结果可能不像预期的5000程序的意思是每个线程使counter中的value自加1000次5个线程运行结束的时候应该是5000当没有使用锁的时候自加的操作可能被其他线程打断因此结果可能会小于5000。 make it simple, make it happen 转载于:https://www.cnblogs.com/lvdongjie/p/4487723.html