The use of Linux_c++ thread function

pthread_create function

Function to create thread
int pthread_create(pthread_t *pid,const pthread_att_t *attr,void *(*start_routine)(void*),void *arg)

  • pid: pointer to the new thread ID that was created successfully
  • attr: Pointer to the thread attribute structure pthread_attr_t. If NULL, it is the default attribute. The default attribute is a non-separated thread and a 1Mb stack, which has the same priority as the parent thread.
  • start_routine: thread callback function
  • arg: parameter information of the callback function

If the creation is successful, return 0, and the rest of the return values ​​are creation failure

#include<pthread.h>
#include<iostream>
#include<unistd.h>

using namespace std;

void *func(void* arg)
{
    
    
	cout<<"pthread_creat func ~~~~"<<endl;
	return 0;
}

int main()
{
    
    
	pthread_t tidp; //线程的Id
	int ret;
	ret = pthread_create(&tidp,NULL,func,NULL); 
	if (ret)
	{
    
    
		cout<<"pthead_creat error"<<endl;
		return -1;
	}
	sleep(10);
	cout<<"pthread_creat success"<<endl;
	return 0;
}

pthread_create function passing parameters

Pass parameters according to string

When passing parameters according to character strings, constant character strings need to be used.

#include<pthread.h>
#include<iostream>

using namespace std;

void *func(void* arg)
{
    
    
	char *ret;
	ret  = (char*) arg;
	cout<<ret<<endl;
	cout<<"new pthread create success"<<endl;
}

int main()
{
    
    
	pthread_t tidp;
	int ret;
	const char *str = "hello world";
	ret = pthread_create(&tidp,NULL,func,(void *)str);
	if(ret)
	{
    
    
		cout<<"create pthread error"<<endl;
		return -1;
	}
	pthread_join(tidp,NULL);
	cout<<"create pthread success"<<endl;
	return 0;
}

Pass parameters according to the structure

#include<pthread.h>
#include<iostream>

using namespace std;

typedef struct{
    
    
	int n;
	const char* str; 
}MyStruct;

void *func(void *arg)
{
    
    
	MyStruct *p = (MyStruct*)arg;
	cout<<p->n<<endl;
	cout<<p->str<<endl;
	cout<<"new pthread create success"<<endl;
}

int main()
{
    
    
	pthread_ t tidp;
	int ret;
	MyStruct mystruct;//定义一个结构体
	//初始化结构体
	mystruct.n = 10;
	mystruct.str = "hello world"; //常量字符串赋值给常量
	ret = pthread_create(&tidp,NULL,func,(void*)&mystruct);
	if(ret)
	{
    
    
		cout<<"pthead_creat error"<<endl;
		return -1;
	}
	pthread_join(tidp,NULL);
	cout<<"pthread_creat success"<<endl;
	return 0;
}

pthread_join function

Thread wait function, because the new thread created may be slower than the main thread, so we need to wait for the new thread to finish executing before exiting the main thread.

#include<pthread.h>
#include<iostream>

using namespace std;

void *func(void* arg)
{
    
    
	cout<<"new pthread create success"<<endl;
}

int main()
{
    
    
	pthread_t tidp;
	int ret;
	ret = pthread_create(&tidp,NULL,func,NULL);
	if(ret)
	{
    
    
		cout<<"pthead_creat error"<<endl;
		return -1;
	}
	pthread_join(tidp,NULL);
	cout<<"pthread_creat success"<<endl;
	return 0;
}

pthread_join will always wait for the end of the child thread before executing the code behind the function. The first parameter is the ID of the child thread, and the second parameter is the thread exit code. If you don't care, you can set it to NULL.

Get thread attributes pthread_getattr_np

Thread attributes, including separation state, scheduling strategy, parameters, scope, stack, etc., are all stored in the union. It is particularly inconvenient for us to view, because Linux provides us with a method.int pthread_getattr_np(pthread_t pid,pthread_attr_t *attr)
Insert picture description here

  • pid: Id of the thread
  • attr: the content of the thread attribute structure

If the execution is successful, return 0

Initialize thread attributes pthread_attr_init

When we use the pthread_create function to create a thread, the second parameter is NULL by default. If we want to modify the following attributes of the thread ourselves, we need to use the pthread_attr_init function.
int pthread_attr_init(pthread_attr_t *attr);

Set thread separation properties

The thread is connected by default, and its attribute is joinable. A thread in the connection attribute will not reclaim resources by itself, and must wait for other threads to reclaim its resources. If the parent process does not call pthread_join to wait, the thread resources in the connection have not been released, and zombie threads will be programmed, causing resources to be wasted.

A thread can only be waited by one thread.

int pthread_attr_setdatachstate(pthread_attr_t *attr,int detachstate

  • detachstate: There are two options: PTHREAD_CREATE_JOINABLEandPTHREAD_CREATE_DETACHED
//实现一个可以分离的线程
#include<pthread.h>
#include<iostream>
#include<unistd.h>

using namespace std;


void *func(void* arg)
{
    
    
	cout<<("pthread callback func\n");
	return NULL;
}

int main()
{
    
    
	pthread_t pid;
	pthread_attr_t pattr;
	int ret ;
	ret = pthread_attr_init(&pattr);
	if(ret)
	{
    
    
		cout<<"pthread init error"<<endl;
		return -1;
	}
	ret = pthread_attr_setdetachstate(&pattr,PTHREAD_CREATE_DETACHED);
	if(ret)
	{
    
    
		cout<<"pthread set detach error"<<endl;
		return -1;
	}
	ret = pthread_create(&pid,&pattr,func,NULL);
	if(ret)
	{
    
    
		cout<<"pthread create error"<<endl;
		return -1;
	}
	cout<<"main pthread will exit"<<endl;
	sleep(1);
	//sleep1:确保子线程执行,但是我们不确定sleep多少,子线程才会执行,因此我们可以使用pthread_exit
	//pthread_exit(NULL)
	return 0;
}

The pthread_exit thread exit function, executed in the main thread, will not cause the process to exit. The process will not exit until all child threads exit.

Advantages and disadvantages of thread separation

  • The attribute of the thread after thread separation is detachthat after exiting, the system will automatically release the resources it occupied. Therefore, the separated thread cannot be recycled or killed by other threads.
  • joinableThe thread in the attribute needs other threads to use join_able to wait, and its resources will be released.

Thread exit

There are four main scenarios for thread exit:

  • Call pthread_exitfunction
  • Call the return function
  • Thread is notified by other threads to end
  • End of process

Guess you like

Origin blog.csdn.net/qq_42708024/article/details/111314667