Linux multi-thread programming and linux multi-thread programming
This article summarizes the several functions that can be used in multi-threaded programming in Linux. Of course, the locking operation is also required for synchronous operations. Here, the specifics are not listed, just introduce the most common functions.
-Lpthread must be added during compilation of multi-threaded programs because the pthread library is used.
/** Header file: # include <pthread. h> function: Create a thread parameter: tid: Used to return the thread idattr: Set the thread attributes, such as the thread priority and initial stack size. The default value is generally used, that is, NULLfunc: the thread executes the function. It can be seen from the definition that it returns the void * type. The parameter is also the void * type arg: The parameter passed to the thread. If there are more than one parameter, you need to use a struct to pass the return value: if the execution succeeds, 0 is returned. If the execution fails, a non-0 value */int pthread_create (pthread_t * tid, const pthread_attr_t * attr, void * (* func) (void *) is returned *), void * arg);/** header file: # include <pthread. h> function: Wait for a thread. Similar to the wait4 () function, it is used to release the resource parameter occupied by the thread: tid: Specifies the thread idstatus to wait for: return value for bringing back the thread: if the execution succeeds, 0 is returned. If the execution fails, an error code */int pthread_join (pthread_t tid, void ** status) is returned./** header file: # include <pthread. h> function: return the id returned value of the current thread: thread id. Note: typedef unsigned long int pthread_t; */pthread_t pthread_self (void);/*** header file: # include <pthread. h> function: detach a thread. This function is similar to converting a process into a background process, that is, discontinuing from the current control terminal. Therefore, we do not need to call the pthread_join function to wait for the thread to run the end parameter: tid: Return Value of the thread id to be separated: 0 is returned for successful execution, and an error code */int pthread_detach (pthread_t tid) is returned for failed execution./** header file: # include <pthread. h> function: Generally, you can add such a statement at the end of the thread's function execution, or leave it empty. This indicates that the end of the thread's life cycle. Note that the exit function cannot be written at the end of the thread's function execution, this will end the entire process. When the process ends, the whole address space will be reclaimed while the thread is sharing the address space of the process. Therefore, all threads in the process will be terminated, it will become a BUG parameter of the program: Used to bring back the return value of the end of the thread */void pthread_exit (void * status );
Finally, an example of using these functions is given:
#include <stdio.h>#include <pthread.h>#include <unistd.h>#define THREAD_COUNT2void dlut_print_thread_id();void * dlut_thread_func(void *arg);int main(int argc, char **argv, char **environ){int i = 0;pthread_t tid[THREAD_COUNT];void *status;for (i = 0; i != THREAD_COUNT; i++){pthread_create((pthread_t *)&tid[i], NULL, dlut_thread_func, (void *)"BruceZhang");}for (i = 0; i != THREAD_COUNT; i++){pthread_detach(tid[i]);}sleep(1);return 0;}void dlut_print_thread_id(char *s){pthread_t tid;tid = pthread_self();printf("%s : this new thread's id is %d\n", s, (int)tid);return;}void * dlut_thread_func(void *arg){dlut_print_thread_id((char *)arg);return (void *)0;}