Home>Article>Operation and Maintenance> Does Linux have a function to create threads?
Linux has a function to create threads, namely the "pthread_create()" function. This function is a function that creates threads in Unix-like operating systems. It supports four parameters: parameter 1 is a pointer to the thread identifier, parameter 2 is used to set thread attributes, parameter 3 is the starting address of the thread running function, and parameter 4 is Parameters to run the function.
#The operating environment of this tutorial: linux5.9.8 system, Dell G3 computer.
Linux has a function to create threads, which is the pthread_create() function.
pthread_create() is a function that creates threads in Unix-like operating systems (Unix, Linux, Mac OS X, etc.)
Header file
# includecc7ade6c52b5948d3b8647c25104afae
Function declaration
int pthread_create( pthread_t *restrict tidp, //新创建的线程ID指向的内存单元。 const pthread_attr_t *restrict attr, //线程属性,默认为NULL void *(*start_rtn)(void *), //新创建的线程从start_rtn函数的地址开始运行 void *restrict arg //默认为NULL。上述函数需要参数,将参数放入结构中并将地址作为arg传入。 );
Return value
Function usage
#include#include #include #include #include #include #include "main.h" using namespace std; struct Sample { uint32_t index; char sex; uint32_t age; uint32_t result; }; void* TaskEntry(void *args) { Sample *sa = (Sample*)args; uint32_t num = sa->index; if (num == 0) { printf("TaskEntry entry num = 0\n"); // 线程1执行体 sleep(10); printf("TaskEntry entry num = 0 is over!!!\n"); } else if (num == 1) { printf("TaskEntry entry num = 1\n"); // 线程2执行体 sleep(10); printf("TaskEntry entry num = 1 is over!!!\n"); } else if (num == 2) { printf("TaskEntry entry num = 2\n"); // 线程3执行体 sleep(2); printf("TaskEntry entry num = 2 is over!!!\n"); } } uint32_t CreateTask(pthread_t& pid, Sample& sample) { // 假设Sample.index == 2创建任务失败,直接返回 if (sample.index == 2) { return 2; } pthread_attr_t attr; // 设置线程属性 pthread_attr_init(&attr); pthread_attr_setstacksize(&attr, 64 * 1024); // 设置线程栈大小为64KB uint32_t ret = pthread_create(&pid, &attr, (void*(*)(void*))TaskEntry, (void*)&sample); if (ret != 0) { return ret; } pthread_attr_destroy(&attr); // 取消线程的设置属性 return 0; } void VerifyTask(vector & taskID, vector & taskArgs) { void *ret; for (int index = 0; index<2; index++) { // 等待线程结束,释放相应的资源。pthread_join会堵塞主线程不会堵塞其他子线程,然后等待监控的线程执行完成,再返回主线程 // 在此处线程执行顺序为:线程1--主线程--线程2--主线程--线程3 pthread_join(taskID[index], &ret); // 堵塞主线程,执行子线程taskID[index],等待子线程taskID[index]执行完成释放资源 printf("task[%d] is over\n", index); // 主线程执行打印操作 } } int main(void) { // 创建3个线程 vector taskID(3); vector taskArgs(3); for (int i = 0; i < 3; i++) { taskArgs[i] = { i, 'a', 90, 0}; uint32_t ret = CreateTask(taskID[i], taskArgs[i]); if (ret != 0) { // 模拟如下场景:任务创建失败,直接停止前面的任务 for (int j = 0; j Note that when using compilation, you need to add the compilation option -lpthread, for example: g -lpthread main.cpp -o main Related recommendations: " Linux Video Tutorial"
The above is the detailed content of Does Linux have a function to create threads?. For more information, please follow other related articles on the PHP Chinese website!