1 Thread creation
#include <stdio.h> #include <pthread.h> #include <stdlib.h>void thread (void) {int i;for (i=0;i<3;i + +) {printf ("This is a pthread\n");}} int main (void) {pthread_t Id;int I,ret;ret = Pthread_create (&id,null, (void *) thread,null); if (ret! = 0) {printf (" Create Pthread error\n "); exit (1);} for (i=0;i<3;i++) printf ("This is the main process\n");p thread_join (id,null);p rintf ("Thread over here\n"); return 0;}
2 Thread Bindings
Above 1 of the code, using Pthread_create to create a thread, in this thread, using the default parameters, that is, the function of the second parameter pthread_attr_t *attr is set to null.
Property values include whether to bind, detach, stack address, stack size, priority. The default properties are non-binding, non-detached, default 1M stacks, and the same level of precedence as the parent process.
The binding on threads involves another concept: light threading. A light thread can be understood as a kernel thread, which is located between the user layer and the system layer. The system's allocation of thread resources, the control of threads, is implemented by light threading, and a light thread can control one or more threads. By default, how many light threads are started and which light threads control which threads are controlled by the system, which is called Unbound. In the binding state, as the name implies, a thread is pinned on top of a light thread. The bound thread has a high response speed because the control room of the CPU time slice is oriented toward the light process. The bound process can guarantee that it always has a light process available when needed. By setting the priority and dispatch level of the lightweight process being bound, the bound thread can satisfy requirements such as real-time response.
The function that sets the thread binding state is Pthread_attr_setscope, the first parameter is a pointer to the property structure, and the second parameter is the binding type: Pthread_scope_system (binding type) pthread_scope_process ( Non-binding)
#include <stdio.h> #include <pthread.h>void my_func (void); int main () {pthread_attr_t attr;pthread_t tid; Pthread_attr_init (&attr);/*pthread_scope_system means bonding thread, pthread_scope_process:unbonding Pthread, can is unsed for action needed right Time*/pthread_attr_setscope (&attr,pthread_scope_system);p Thread_ Create (&tid,&attr, (void *) my_func,null);p thread_join (tid,null);//here is blockedprintf ("Thread over\n"); return 0;} void My_func (void) {sleep (4);p rintf ("Thread process\n");//while (1);}
3 Blocking of threads
With the default parameters of Pthread_create (), in the main thread (threads that create threads), by calling Pthread_join () to wait for the thread, it will block until the thread exits. example, the main function cannot exit because the thread is always in the while loop
#include <stdio.h> #include <pthread.h>void *my_func (void *arg); int main () {pthread_t Tid1,tid2;pthread_ Create (&tid1,null, (void *) My_func, "Thread 1");p thread_create (&tid2,null, (void *) My_func, "Thread 2"); Pthread_join (tid1,null);//here is Blockedpthread_join (tid2,null);p rintf ("Thread over\n"); return 0;} void *my_func (void *arg) {printf ("Thread process\n"), while (ARG) {sleep (2);p rintf ("%s\n", (char *) arg);}}
4 blocking function and the use of the unblocking signal
#include <stdio.h> #include <pthread.h> #include <stdlib.h> #include <unistd.h> #include < string.h>pthread_mutex_t mymutex1 = pthread_mutex_initializer;//pthread_mutex_t Mymutex2 = pthread_mutex_ initializer;pthread_cond_t Mycond = pthread_cond_initializer;void *mythread1 (void *param) {printf ("Begin mythread1.\n ");p Thread_mutex_lock (&mymutex1);p rintf (" Wait in my thread1.\n ");p thread_cond_wait (&MYCOND,&MYMUTEX1) ;p Thread_mutex_unlock (&mymutex1);p rintf ("End mythread1.\n"); return NULL;} void *mythread2 (void *param) {printf ("Begin mythread2.\n");p Thread_mutex_lock (&mymutex1);p rintf ("Wait in my thread2.\n ");p thread_cond_wait (&mycond,&mymutex1);p thread_mutex_unlock (&mymutex1);p rintf (" End Mythread2.\n "); return NULL;} int main (void) {printf ("Begin main thread\n"); int i;pthread_t tid1,tid2;pthread_create (&tid1,null,mythread1,null );p thread_create (&tid2,null,mythread2,null); sleep (5);p rintf ("Try to wake up Mythread1 and mythread2 in main tHread.\n "), #if 1if (Pthread_cond_broadcast (&mycond)) {printf (" unlock error\n "); return 1;} #endifvoid *res;pthread_join (tid1,&res);p thread_join (tid2,&res);p rintf ("End main thread.\n"); return 0;}
Some references:
http://blog.csdn.net/hudashi/article/details/7709421
Thread programming for Linux application programming