current position:Home>C + + concurrent programming (3)

C + + concurrent programming (3)

2022-05-15 05:17:26Robot Laboratory of Hefei University of Technology

C++ Concurrent programming (3) and C++ Concurrent programming (2) Corresponding to Chapter 2 of the book ,C++ Concurrent programming (3) This is the closing part of Chapter 2

Transfer thread ownership

For a specific execution thread , Its ownership can be in multiple thread Transfer between instances , There are several uses

  1. stay thread Transfer the ownership of thread between instances
  2. thread Object as the return value
  3. thread Object as a function parameter
  4. towards STL Loading in container thread object

The sample code is as follows :

#include <iostream>
#include <thread>
#include <string.h>
#include <vector>

using namespace std;

const int thread_num = 10;

void smile()
{
    
    cout << "Smile" << endl;
}

void cry()
{
    
    cout << "cry" << endl;
}

thread test()
{
    
    thread t_(smile);

    cout << "test t_ id:" << t_.get_id() << endl;

    return t_;
}

void test02(thread t_)
{
    
    if (t_.joinable())
    {
    
        cout << "test02 t_ joinable" << endl;
        cout << "test02 t_ id:" << t_.get_id() << endl;
        t_.join();
    }
    else
    {
    
        cout << "test02 t_ unjoinable" << endl;
        cout << "test02 t_ id:" << t_.get_id() << endl;
    }
}

int main()
{
    
    thread t;
		// Accept... From within a function thread object 
    t = test();
    cout << "main t id:" << t.get_id() << endl;
    if(t.joinable())
    {
    
				cout << "main t joinable" << endl;
        t.join();
    }

    thread t1 = thread(cry);
    cout << "main t1 id:" << t1.get_id() << endl;
		//t = move(t1);  error !!! 

		// stay thread Transfer the ownership of thread between objects 
    thread t2 = move(t1);
    cout << "main t2 id:" << t2.get_id() << endl;

		// Pass... Inside the function thread object 
    test02(move(t));
    t2.join();

		// In a container thread object 
    vector<thread> v;
    for (int i = 0; i < thread_num; ++i)
    {
    
        if (i % 2 == 0)
        {
    
            v.emplace_back(smile);
        }
        else
        {
    
            v.emplace_back(cry);
        }
    }
    int k = 0;
    for(auto& v_t:v)
    {
    
        cout << "v_t " << k << " id:" << v_t.get_id() << endl;
        v_t.join();
        k++;
    }

    cout << "main end" << endl;

    return 0;
}

Operation output :

[email protected]-VirtualBox:~/Cplus_learning/build$ ../bin/thread_03
test t_ id:140260519634688
main t id:140260519634688
main t joinable
Smile
main t1 id:140260519634688
main t2 id:140260519634688
test02 t_ unjoinable
test02 t_ id:thread::id of a non-executing thread
cry
v_t 0 id:140260519634688
Smile
cry
Smile
Smile
cry
cry
cry
Smile
cry
Smile
v_t 1 id:140260511241984
v_t 2 id:140260502849280
v_t 3 id:140260494456576
v_t 4 id:140260411438848
v_t 5 id:140260403046144
v_t 6 id:140260394653440
v_t 7 id:140260386260736
v_t 8 id:140260377868032
v_t 9 id:140260369475328
main end

If to the associated thread thread Object transfers the ownership of the thread , be std::terminate() Function will be called , Terminate the whole program

So long as std::thread Object is controlling a thread , You can't simply assign a new value to it

copyright notice
author[Robot Laboratory of Hefei University of Technology],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/131/202205111245214979.html

Random recommended