当前位置:网站首页>Concurrency - create thread

Concurrency - create thread

2022-06-10 09:35:00 Classmate Peng is her deskmate

Create thread

Include header file #include<thread>

Create functions with threads

void threadBegin()
{
}
thread a(threadBegin);// Create a thread object You need to put a callable object inside the brackets to construct the object This function becomes the starting point of the thread

The problem of creating thread function parameters

void mprint(const int &a,char* b)
{
    
    cout << a << endl;
    cout << b << endl;
}
int main()
{
      
    int a = 0;
    char b[] = "abcd";
    thread mobj(mprint,a,b);// In this way, the parameters are passed into 
    //mobj.detach(); 
    mobj.join();
    return 0;
}

Although it uses const int &a That is, the way of reference In fact, within the child thread a And in the main thread a Different addresses It doesn't really quote It's actually value passing
But if you pass through a pointer In a child thread b And the b It's an address So this is very dangerous

Problems with type conversion

Because it is not safe to pass the pointer directly So we convert the value stored in this pointer to string It will be better Will become value passing But there will still be problems Because of concurrency, we don't know
char b[] When is it converted to string Of May appear char b[] Has been recycled in the main function Proceed again string Type conversion of ( I don't understand here If this problem occurs What then? int a Value transfer can be realized Won't it appear int a Called back in the main function Then the value is passed to the child thread a Do you )

void mprint(const int &a,const string& b)
{
    
    cout << a << endl;
    cout << b << endl;
}
int main()
{
      
    int a = 0;
    char b[] = "abcd";
    thread mobj(mprint,a,b);// The right way is thread mobj(mprint,a,string(b));  Create a temporary string
    //mobj.detach(); 
    mobj.join();
    return 0;
}
prove string(b) It is feasible.
class A {
    
public:
	double x, y;
	A()
	{
    
		cout << " Constructors " << endl;
	}
	A(int i) {
    // Type conversion constructors 
		cout << " Type conversion constructors " << endl;
		x = i; y = 0;
	}
	A(const A& c) :x(c.x), y(c.y)
	{
    
		cout << " Called copy constructor " << endl;
	}
	~A()
	{
    
		cout << " It's deconstructed " << endl;
	}

};
void start(const A& a)
{
    
	cout << " Entering the sub thread " << endl;
}
int main()
{
    
	int x = 1;
	thread mobj(start, x);
	mobj.detach();
	return 0;
}

 Please add a picture description
It is obvious that It is possible to execute after the main thread ends A a = x Type conversion of
So it is not completely implemented thread mobj(start,x) All the structures inside The main thread will continue to execute So there will be the front char b[] Has been recycled in the main function Proceed again string Type conversion of
When we change to A(x) when
 Please add a picture description
Obviously When we create a temporary A When the class object It must be executed first
Why ? I think so A(x) This sentence must be executed by the main thread So it must be possible to execute before the main thread ends also We can also see that the copy constructor is called explain At this point, the temporary created A The object is copied to... In the thread a

Create a thread with a class object

When creating threads with class objects Will copy a class object to the thread
So if the class object in the main thread is destroyed Class objects in child threads are not destroyed

class A
{
    
public:
    A()
    {
    
        cout << "A Constructor for "<<endl;
    }
    A(const A& a)
    {
    
        cout << "A Copy constructor for " << endl;
    }
    void operator()() 
    {
    
        cout << " I'm a class ";
    }
    ~A()
    {
    
        cout << "A Destructor of " << endl;
    }

};
int main()
{
     
    A a;
    thread mobj(a);
    //mobj.detach(); 
    mobj.join();
    return 0;
}

 Please add a picture description

The call is found A Copy constructor for Description is a copy of A Class object to child thread

lambda Expression creation

auto mylambda = []{
    
	cout<<"lambda Execution start "<<endl;
	cout<<"lambda end of execution "<<endl;
};
thread mobj(mylambda);

join()

Block main thread Let the main thread wait for the child thread to finish executing Then the child thread and the main thread merge

detach()

Separate the child thread from the main thread

void mprint()
{
    
    cout << " First thread "<<endl;
    cout << " First thread " << endl;
    cout << " First thread " << endl;
    cout << " First thread " << endl;
}
int main()
{
    
    thread mobj(mprint);
    mobj.detach();
    cout << "main"<<endl;
    cout << "main" << endl;
    cout << "main" << endl;
    cout << "main" << endl;
}

detach() The latter child thread loses contact with the main thread The child thread resides in the background and runs Child threads are C++ Runtime library takes over After the execution of the sub thread is completed, the runtime library will clean up the relevant resources ( The guardian thread )
That is, the main thread can execute concurrently with the sub thread And join Opposite
In the child thread detach No more join Come back Will report a mistake

joinable()

Determine whether you can use join perhaps detach
if(mobj.joinable() == true)

原网站

版权声明
本文为[Classmate Peng is her deskmate]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206100925405391.html