pthread_create function Function Introduction pthread_create is a UNIX environment thread creation function Header Files #include<pthread.h> Function declaration int pthread_create(pthread_t *restrict tidp,const pthread_attr_t *restrict_attr,void*(*start_rtn)(void*),void *restrict arg); Return Value If successful, it returns 0, otherwise it returns an error number parameter The first parameter is a pointer to the thread identifier. The second parameter is used to set thread attributes. The third parameter is the address of the function that the thread runs. The last argument is the parameters to run the function with. Notice When compiling, remember to add the -lpthread parameter to call the static link library. Because pthread is not the default library of the Linux system. pthread_join Function Function Introduction The function pthread_join is used to wait for the end of a thread. The function prototype is: extern int pthread_join __P (pthread_t __th, void **__thread_return); parameter: The first parameter is the thread identifier being waited for. The second parameter is a user-defined pointer that can be used to store the return value of the waited thread. Notice This function is a thread blocking function. The function that calls it will wait until the waiting thread ends. When the function returns, the resources of the waiting thread are reclaimed. If the execution is successful, it will return 0, if it fails, it will return an error number. example: #include <stdio.h> #include<stdlib.h> #include<pthread.h> /*Declare the structure*/ struct member { int num; char *name; }; /* Define thread pthread */ static void * pthread(void *arg) { struct member *temp; /* Thread pthread starts running*/ printf("pthread start!\n"); /* Let the main thread continue to execute */ sleep(2); /* Print the passed parameters */ temp = (struct member *)arg; printf("member->num:%d\n",temp->num); printf("member->name:%s\n",temp->name); return NULL; } /* main function */ int main(int agrc,char* argv[]) { pthread_t tidp; struct member *b; /* Assign a value to the structure variable b*/ b = (struct member *)malloc(sizeof(struct member)); b->num=1; b->name="mlq"; /* Create a thread pthread */ if ((pthread_create(&tidp, NULL, pthread, (void*)b)) == -1) { printf("create error!\n"); return 1; } /* Let the thread pthread run first*/ sleep(1); /* Thread pthread sleeps for 2s, then main can be executed first*/ printf("mian continue!\n"); /* Waiting for thread pthread to be released*/ if (pthread_join(tidp, NULL)) { printf("thread is not exit...\n"); return -2; } return 0; } Compilation and execution results The compilation and execution results are shown in the figure below. You can see that the main thread main and the thread pthread are executed alternately. That is to say, after we created the thread pthread, both threads were executed, proving that the creation was successful. In addition, you can see that when the thread pthread is created, the passed parameters are printed correctly. This is the end of this article about the specific use of pthread_create in linux to create threads. For more relevant linux pthread_create content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: js realizes the function of clicking to switch cards
>>: The front-end must know how to lazy load images (three methods)
1. Download from the official website and unzip h...
1. ROW_NUMBER() Definition: The ROW_NUMBER() func...
Often, after a web design is completed, the desig...
This database query statement is one of 50 databa...
Table of contents 1. Overview of the page 2. Infi...
This article mainly introduces the relevant solut...
1. Add the ul tag in the body first <!-- Unord...
background Temporary tablespaces are used to mana...
1. Installation Instructions Compared with local ...
Table of contents Preface 1. The significance of ...
I hope to implement some properties of the query ...
1. Embedded Software Level 1) Bootloader->Boot...
1. Download MySQL Official website download addre...
Today is still a case of Watch app design. I love...
Table of contents 1. Introduction 2. Solution Imp...