Linux multithreaded Programming

Source: Internet
Author: User

This article summarizes the multi-threaded programming in Linux can be used in several functions, of course, the need to synchronize operations also need to lock the operation, here, not listed so specific, just the most commonly used functions introduced.

It is necessary to add-lpthread when compiling a multithreaded program, because the Pthread library is used.

/** header file: #include <pthread.h> function: Create a thread parameter: tid: Used to return thread Idattr: Set the properties of the thread, such as thread priority, initial stack size, etc., generally using the default value, that is, Nullfunc: Thread execution function, As can be seen from the definition, it returns the void* type, the argument is also the void* type arg: Arguments passed to the thread, if more than one argument is required, the return value needs to be passed with the struct: execution successfully returns 0, execution failure returns a non-0 value */int pthread_create ( pthread_t *tid, const pthread_attr_t *attr, void * (*FUNC) (void *), void *arg);/** header file: #include <pthread.h> function: Waiting for a thread , similar to the WAIT4 () function, which is used to release the resource parameters of the thread: tid: Specifies the thread to wait for Idstatus: Return value for the returned thread returned: execution successfully returns 0, execution failure returns an error code */int pthread_join (pthread_ T tid, void * * status);/** header file: #include <pthread.h> function: Returns the ID return value for the current thread: Thread ID Note: typedef unsigned long int pthread_t;*/p thread_t pthread_self (void);/** header file: #include <pthread.h> function: Separate threads, similar to turning a process into a background process, that is, out of the current control terminal, then we do not need to call The Pthread_join function waits for the execution end parameter of this thread: TID: The thread ID return value that needs to be detached: execution successfully returns 0, execution failure returns an error code */int Pthread_detach (pthread_t tid);/** header file: # Include <pthread.h> features: General thread execution function at the end of the addition of such a statement, you can also do not write, representing the end of the life cycle of the write thread, note that the end of the threading function can not write the Exit function, because this will make the entire process end, when the process ends, Because the entire address space is reclaimed, and the thread is the shared process address space, all threads within the process are terminated and become a bug parameter for the program: The return value used to bring the end of the thread back */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[thr Ead_count];void *status;for (i = 0; I! = Thread_count; i++) {Pthread_create ((pthread_t *) &tid[i], NULL, Dlut_thread_fu NC, (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 ();p rintf ("%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;}


Linux multithreaded Programming

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.