Shredded thread of Code: thread class is simple to use

Reproduced in: https: //blog.csdn.net/qq_22494029/article/details/79273127

Simple multi-threaded example:

detch () start the thread:

 1 #include <iostream>
 2 #include <thread>
 3 using namespace std;
 4 void testThread1();
 5 void testThread2();
 6 int main()
 7 {
 8     thread t1(testThread1);
 9     t1.detach();
10     thread t2(testThread2);
11     t2.detach();
12     cout<<"主线程:你好帅!!!"<<endl;
13     std::this_thread::sleep_for(std::chrono::milliseconds(10000));
14     return 0 ;
 15  }
 16  void TestThread1 () // thread. 1 
. 17  {
 18 is      for ( int I = 0 ; I < 10 ; ++ I) {
 . 19          COUT << " TestThread1: " << I << endl;
 20 is          // 100 msec pause 
21 is          STD :: :: sleep_for, which this_thread (STD :: :: Chrono milliseconds ( 100 ));
 22 is      }
 23 is  }
 24  void TestThread2 () // thread 2 
25  {
 26     for (int i = 100; i < 110; ++i) {
27         cout<<"testThread2:"<<i<<endl;
28         this_thread::sleep_for(chrono::milliseconds(100));
29     }
30 }

Description: detch () method, which means that opener thread and the main thread does not wait for the child thread has finished running, but running in parallel, and the child thread.

operation result:

 

 

join () method to start the thread:

 1 #include <iostream>
 2 #include <thread>
 3 using namespace std;
 4 void testThread1();
 5 void testThread2();
 6 int main()
 7 {
 8     thread t1(testThread1);
 9     t1.join();
10     thread t2(testThread2);
11     t2.join();
12     cout<<"主线程:你好帅!!!"<<endl;
13     return 0;
14 }
15 void testThread1()//线程1
16 {
17     for (int i = 0; i <10 ; ++i) {
18         cout<<"testThread1:"<<i<<endl;
19         //暂停100毫秒
20         std::this_thread::sleep_for(std::chrono::milliseconds(100));
21     }
22 }
23 void testThread2()//线程2
24 {
25     for (int i = 100; i < 110; ++i) {
26         cout<<"testThread2:"<<i<<endl;
27         this_thread::sleep_for(chrono::milliseconds(100));
28     }
29 }

Description: join () method, which means that opener thread, the thread will run synchronously in accordance with the order to open.

operation result:

 

 

Sub-thread functions with multi-threading parameters:

 1 #include <iostream>
 2 #include <thread>
 3 using namespace std;
 4 void testThread1(int count);
 5 void testThread2(int start,int count);
 6 int main()
 7 {
 8     thread t1(testThread1,10);
 9     t1.join();
10     thread t2(testThread2,40,50);
11     t2.join();
12     cout<<"主线程:你好帅!!!"<<endl;
13     return 0;
14 }
15 void testThread1(int count)
16 {
17     for (int i = 0; i <count; ++i) {
18         cout<<"testThread1:"<<i<<endl;
19         this_thread::sleep_for(chrono::milliseconds(100));
20     }
21 }
22 void testThread2(int start,int count)
23 {
24     for (int i = start; i <count ; ++i) {
25         cout<<"testThread2:"<<i<<endl;
26         this_thread::sleep_for(chrono::milliseconds(100));
27     }
28 }

operation result:

 

 Multi-thread safe access to shared data example (selling tickets)

1  // multi-thread safe access to shared data examples (ticket) 
2 #include <the iostream>
 . 3 #include <Thread>
 . 4 #include <the mutex>
 . 5  the using  namespace STD;
 . 6  class ThreadTest
 . 7  {
 . 8  Private :
 . 9      // votes the remaining number 
10      int SUM;
 . 11      the mutex the Mutex;
 12 is  public :
 13 is      // constructor that when opening the thread 
14      ThreadTest ()
 15      {
 16          SUM = 20 is ;
 . 17         thread t1(&ThreadTest::Thread1,this);
18         t1.detach();
19         thread t2(&ThreadTest::Thread2,this);
20         t2.detach();
21     }
22     //析构函数
23     ~ThreadTest(){}
24 
25     //卖票线程1
26     void Thread1()
27     {
28         while(true)
29         {
30 
31             this_thread::sleep_for(chrono::milliseconds(100));
32             . Mutex Lock (); // lock 
33 is              - SUM;
 34 is              IF (SUM < 0 )
 35              {
 36                  COUT << " Thread1-- sold out " << SUM << endl;
 37 [                  BREAK ;
 38 is              }
 39              COUT << " Thread1-- remaining votes: " << SUM << endl;
 40              Mutex.unlock ();
 41 is          }
 42 is          Mutex.unlock ();
43     }
44     //Ticketing Thread 2 
45      void Thread2 ()
 46 is      {
 47          the while ( to true )
 48          {
 49              this_thread :: sleep_for, which (Chrono :: milliseconds ( 100 ));
 50              . The Mutex Lock (); // lock 
51 is              sum-- ;
 52 is              IF (SUM < 0 )
 53 is              {
 54 is                  COUT << " Thread2-- sold out " << SUM << endl;
 55                  BREAK ;
 56 is             }
 57 is              COUT << " Thread2-- remaining votes: " << SUM << endl;
 58              Mutex.unlock ();
 59          }
 60          Mutex.unlock ();
 61 is      }
 62 is  };
 63 is  int main ()
 64  {
 65      / / multithreaded ticket type 
66      ThreadTest SaleThread;
 67      this_thread :: sleep_for, which (Chrono :: milliseconds ( 10000 ));
 68      return  0 ;
 69 }

operation result:

 

Guess you like

Origin www.cnblogs.com/yichengming/p/11506395.html