The difference between thread::join() and thread1::detach() in c++

The difference between thread::join() and thread::detach() is whether the main thread is blocked.
After the thread is released using thread::join(), the memory allocated to the thread by the system is emptied and released, so it can only be executed once, but before the join is executed, the main thread cannot do other things.
After the thread::detach() method is used, it will be separated from the pointer to the thread, that is, the main thread loses control of the thread, the main thread does not need to wait for the end of the thread to end, and the thread will execute after the end Free up space by yourself.
The code is as follows:
no thread effect is added

void hellowworld()
{
	int i = 10;
	while (i--)
	{
		cout << " hello world" << endl;
	}	
}

void helloguangzhou()
{
	int i = 10;
	while (i--) {
		cout << " hello guangzhou" << endl;
	}	
}
int main()
{
    hellowworld();
	helloguangzhou();
  return 0;
}

The output is output in order.

 hello world
 hello world
 hello world
 hello world
 hello world
 hello world
 hello world
 hello world
 hello world
 hello world
 hello guangzhou
 hello guangzhou
 hello guangzhou
 hello guangzhou
 hello guangzhou
 hello guangzhou
 hello guangzhou
 hello guangzhou
 hello guangzhou
 hello guangzhou

Join the thread and use thread::join() to release the effect

void helloguangzhou()
{
    
    
	int i = 10;
	while (i--) {
    
    
		cout << " hello guangzhou" << endl;
	}
	
}
void hellowworld()
{
    
    
	int i = 10;
	while (i--)
	{
    
    
		cout << " hello world" << endl;
	}

}
int main()
{
    
    
std::thread t(hellowworld);
	
	std::thread s(helloguangzhou);
	t.join();
	s.join();
return 0;
}

The output is not the same as the above example, it is not sequential, because the two threads are running at the same time.

 hello world
 hello world
 hello world
 hello guangzhou
 hello guangzhou
 hello world
 hello world
 hello world
 hello world
 hello guangzhou
 hello guangzhou
 hello guangzhou
 hello guangzhou
 hello guangzhou
 hello guangzhou
 hello guangzhou
 hello guangzhou
 hello world
 hello world
 hello world

Join the thread and use thread::detach() to release the effect

void helloguangzhou()
{
    
    
	int i = 10;
	while (i--) {
    
    
		cout << " hello guangzhou" << endl;
	}
	
}
void hellowworld()
{
    
    
	int i = 10;
	while (i--)
	{
    
    
		cout << " hello world" << endl;
	}

}
int main()
{
    
    
std::thread t(hellowworld);
	
	std::thread s(helloguangzhou);
	t.detach();
	s.detach();
return 0;
}

There are only a few lines of output or even no output, because the main thread is finished before the execution of the child thread is finished, and no output can be seen.

hello guangzhou
 hello guangzhou

If you add a code before return to delay the end of the main thread, you can see the complete output.

std::thread t(hellowworld);
	
	std::thread s(helloguangzhou);
	t.detach();
	s.detach();
	std::this_thread::sleep_for(std::chrono::seconds(5));
return 0;

Output result

hello world hello guangzhou
 hello guangzhou
 hello guangzhou
 hello guangzhou
 hello world
 hello world
 hello world

 hello guangzhou
 hello guangzhou
 hello world
 hello world hello guangzhou

 hello world
 hello guangzhou
 hello world
 hello world
 hello guangzhou
 hello world
 hello guangzhou

Guess you like

Origin blog.csdn.net/weixin_43448686/article/details/106554546