POSIX multithreaded model Pthread.h function:
pthread_attr_t attr; Thread attribute struct, parameter pthread_attr_init (&attr) added when creating thread; Initialize pthread_attr_setdetachstate (&attr, pthread_create_joinable);//Set you want to specify a thread attribute parameter, This parameter indicates that the thread can join, join work//can indicate that the main program can wait until the end of the thread to do something, implement the main program and thread synchronization function pthread_t tid1, Tid2;//Save two threads idint ret = Pthread_ Create (&TID1, &attr, Say_hello1, (void*) &index1); Create Thread 1ret = Pthread_create (&tid2, &attr, Say_hello2, (void*) &index2); Create Thread 2pthread_join (TID1, NULL); Connect two threads pthread_join (Tid2, NULL);
Functions used by mutexes:
pthread_mutex_t Sum_mutex; Mutex lock Pthread_mutex_init (&sum_mutex, NULL); Initialize the lock Pthread_mutex_lock (&sum_mutex); Occupy the lock//do something here. Pthread_mutex_unlock (&sum_mutex); Release lock Pthread_mutex_destroy (&sum_mutex); Sign out of a lock
The function used for the semaphore:
pthread_cond_t Tasks_cond; Conditional signal Variable pthread_cond_init (&tasks_cond, NULL); Initialize the conditional signal variable pthread_cond_signal (&tasks_cond); The condition satisfies, sends the signal pthread_cond_wait (&tasks_cond, &tasks_mutex); Wait for Signal Pthread_cond_destroy (&tasks_cond); Sign out of conditional signal variables
The above is the C + + multithreaded programming on Linux content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!