C++11 Multi-Threading 03

Source: Internet
Author: User
Tags mutex

First, correct the previous error: The thread has run before the join () has been called.

#include <iostream>#include<thread>#include<mutex>voidShared_print (std::stringName,intnum) {Std::cout<<name<<":"<<num<<Std::endl;}voidThread_fun () { for(inti =0; i>- -; i--) {Shared_print ("Child Threads", i); }}intMain () {Std::thread T (thread_fun);  for(intI=0;i< -; i++) {Shared_print ("Main Thread", i); }//T.join ();    return 0;}

The following still add the join, the current program output is very messy

#include <iostream>#include<thread>#include<mutex>voidShared_print (std::stringName,intnum) {Std::cout<<name<<":"<<num<<Std::endl;}voidThread_fun () { for(inti =0; i>- -; i--) {Shared_print ("Child Threads", i); }}intMain () {Std::thread T (thread_fun);  for(intI=0;i< -; i++) {Shared_print ("Main Thread", i);    } t.join (); return 0;}

Use basic mutex to solve resource competition, see output not messy

#include <iostream>#include<thread>#include<mutex>Std::mutex mu;voidShared_print (std::stringName,intnum) {mu.Lock(); Std::cout<<name<<":"<<num<<std::endl;//Cons: If a thread crashes here, it can never be unlock.Mu.unlock ();}voidThread_fun () { for(inti =0; i>- -; i--) {Shared_print ("Child Threads", i); }}intMain () {Std::thread T (thread_fun);  for(intI=0;i< -; i++) {Shared_print ("Main Thread", i);    } t.join (); return 0;}

Improved: auto-release mu

#include <iostream>#include<thread>#include<mutex>Std::mutex mu;voidShared_print (std::stringName,intnum) {    //Advantage: When the guard is deconstructed, MU will be released automatically, not afraid of the following sentence exception//Cons: Cout is a global object, not fully protected, other places can still be used in lock-up situationsStd::lock_guard<std::mutex>Guard (MU); Std::cout<<name<<":"<<num<<Std::endl;}voidThread_fun () { for(inti =0; i>- -; i--) {Shared_print ("Child Threads", i); }}intMain () {Std::thread T (thread_fun);  for(intI=0;i< -; i++) {Shared_print ("Main Thread", i);    } t.join (); return 0;}

Continue optimization: Remove the disadvantage of the previous program

#include <iostream>#include<thread>#include<mutex>#include<fstream>classloffile{ Public: Loffile () {F.open ("Log.txt"); }    voidShared_print (std::stringName,intnum) {Std::lock_guard<std::mutex>Locker (M_mutex); F<<name<<": "<<num<<Std::endl; }Private: Std::mutex M_mutex; Std::ofstream F;};voidThread_fun (loffile&log) {     for(inti =0; i>- -; i--) {Log.shared_print ("Child Threads", i); }}intMain () {loffile log; Std::thread T (thread_fun,std::ref(log));  for(intI=0;i< -; i++) {Log.shared_print ("Main Thread", i);    } t.join (); return 0;}

C++11 Multi-Threading 03

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.