1 pthread_create
pthread_create
It is a thread creation function of operating systems (Unix, Linux, Mac OS X), etc. Its function is to create a thread (actually to determine the entry point to call the thread function) , after the thread is created, it starts to run the related thread function.
Function prototype declaration:
#include <pthread.h>
int pthread_create(
pthread_t *restrict tidp, //指向新创建线程标识符的指针
const pthread_attr_t *restrict attr,//设置线程属性,默认为NULL
void *(*start_rtn)(void *), //线程运行函数的起始地址
void *restrict arg //运行函数的参数结构,默认为NULL
);
//成功,返回0;出错,返回错误码。
Note on passing parameters:
Problem:
Avoid passing the changed amount directly in the passed parameters , otherwise the result will be unpredictable.
Even if only a single thread is created, the variable value obtained by the thread may have been modified by the main thread when the thread has not obtained the passed parameters.The usual solution:
re-apply for a piece of memory, store the parameters that need to be passed, and then pass in this address as arg.
2 Precautions for use
- Supporting the use of pthread_join function
By default by pthread_create
the thread function to create a non-separation of property by pthread_create function of the decision to the second argument , in the case of non-separation , when the end of a thread, it occupied system resources and is not completely true Released, there is no real termination . At this pthread_join
time, the thread will release its resources only when the function returns.
pthread_join()
The function will block the calling thread until the specified thread terminates . After pthread_join()
returning, the application can reclaim any data storage space associated with the terminated thread.
However, at the same time it needs to be noted that it must be used in conjunction with a thread created above, so that it can also play a role of mutual exclusion. Otherwise, multi-threading may preempt CPU resources, resulting in uncertain running results.
- Set the thread to be detached (detached attribute)
You can also set it as a detached attribute (the second parameter setting) when the thread is created or in the thread function call, so that the end of a thread will immediately release the resources it occupied.
Attached
pthread_join() function prototype
int pthread_join(
pthread_t tid, //The thread that needs to wait, the specified thread must be in the current process, and must not be a separate thread
void **status //The return value of the function executed by the thread tid (the return value address needs to be valid), where status can be NULL
);
pthread
Non-linux
system default library, you need to link manually -thread library-lpthread
Return value: The
call succeeds and returns 0.ESRCH
Description: The thread corresponding to the given thread ID is not found. (If multiple threads are waiting for the same thread to terminate, all waiting threads will wait until the target thread terminates. Then one waiting thread returns successfully. The remaining waiting threads will fail and return ESRCH error)EDEADLK
Description: A deadlock will occur, such as a thread waiting By itself, or thread A and thread B wait for each other.EINVAL
Description: The thread corresponding to the given thread ID is a detached thread.
For more detailed procedures, see: pthread_create() function of multithreading
More pairspthread_create参数的解析见:多线程pthread_create的参数
For the discussion of the number of threads to be created, refer to: linux-how many threads can be created by pthread_create()?