4 线程

线程:轻量化的进程

  优点:CPU在多线程中切换资源占用时间会缩短
  缺点:多线程具有共享空间,操作共享数据时需要加入同步和互斥机制

线程共享空间
  每个线程具有独立的文本段和系统数据段,但是一个进程中所有线程共享同一数据段
  优点:方便多线程间通信
  缺点:共享空间中数据可能被更改,有风险
pthread_create.c

 1 /* 线程:轻量化的进程
 2  * 优点:CPU在多线程中切换资源占用时间会缩短
 3  * 缺点:多线程具有共享空间,操作共享数据时需要加入同步和互斥机制
 4  *
 5  * 线程共享空间
 6  * 每个线程具有独立的文本段和系统数据段,但是一个进程中所有线程共享同一数据段
 7  * 优点:方便多线程间通信
 8  * 缺点:共享空间中数据可能被更改,有风险
 9  * */
10 #include <stdio.h>
11 #include <pthread.h>
12 void *thread(void *arg)/*线程函数*/
13 {
14     while(1)
15     {
16         printf("我是线程!\n");
17             sleep(1);
18     }
19     return NULL;
20 }
21 
22 int main(void)
23 {
24     /*在调用进程中创建一个线程  
25      * int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
26      *                   void *(*start_routine) (void *), void *arg);
27      * 参数:  
28      * thread:保存线程ID空间的首地址
29      * attr:线程的属性
30      * start_routine:线程函数的首地址
31      * arg:给线程函数传递的参数
32      *成功返回0;失败返回非0。
33      * */
34     pthread_t tid;
35     if(0 != (pthread_create(&tid,NULL,thread,NULL)))
36     {
37             perror("fail to pthread_create!");
38             return -1;
39     }
40 
41     getchar();
42     return 0;
43 }

pthread_create1.c

 1 /* 线程:轻量化的进程
 2  * 优点:CPU在多线程中切换资源占用时间会缩短
 3  * 缺点:多线程具有共享空间,操作共享数据时需要加入同步和互斥机制
 4  *
 5  * 线程共享空间
 6  * 每个线程具有独立的文本段和系统数据段,但是一个进程中所有线程共享同一数据段
 7  * 优点:方便多线程间通信
 8  * 缺点:共享空间中数据可能被更改,有风险
 9  * */
10 #include <stdio.h>
11 #include <pthread.h>
12 int a = 20;
13 void *thread1(void *arg)/*线程函数*/
14 {
15     int count = 0;
16     while(1)
17     {
18         printf("我是线程1  a = %d\n",a);
19         if(count == 5) a = 666;
20             sleep(1);
21             count++;
22     }
23     return NULL;
24 }
25 void *thread2(void *arg)
26 {
27     while(1)
28     {
29         printf("我是线程2 a = %d\n",a);
30         sleep(1);
31     }
32     return NULL;
33 
34 }
35 
36 int main(void)
37 {
38     /*在调用进程中创建一个线程  
39      * int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
40      *                   void *(*start_routine) (void *), void *arg);
41      * 参数:  
42      * thread:保存线程ID空间的首地址
43      * attr:线程的属性
44      * start_routine:线程函数的首地址
45      * arg:给线程函数传递的参数
46      *成功返回0;失败返回非0。
47      * */
48     pthread_t tid1,tid2;
49     if(0 != (pthread_create(&tid1,NULL,thread1,NULL)))
50     {
51             perror("fail to pthread_create!");
52             return -1;
53     }
54 
55     if(0 != (pthread_create(&tid2,NULL,thread2,NULL)))
56     {
57             perror("fail to pthread_create!");
58             return -1;
59     }
60 
61     getchar();
62     return 0;
63 }

pthread_create_multi.c

 1 #include <stdio.h>
 2 #include <pthread.h>
 3 void *thread1(void *arg)/*线程函数*/
 4 {
 5     printf("我是线程1!\n");
 6     pthread_exit("线程1退出...\n");
 7     return NULL;
 8 }
 9 
10 void *thread2(void *arg)/*线程函数*/
11 {
12     printf("我是线程2!\n");
13     pthread_exit("线程2退出...\n");
14     return NULL;
15 }
16 
17 void *thread3(void *arg)/*线程函数*/
18 {
19     printf("我是线程3!\n");
20     pthread_exit("线程3退出...\n");
21     return NULL;
22 }
23 
24 void *thread4(void *arg)/*线程函数*/
25 {
26     printf("我是线程4!\n");
27     pthread_exit("线程4退出...\n");
28     return NULL;
29 }
30 
31 void *thread5(void *arg)/*线程函数*/
32 {
33     printf("我是线程5!\n");
34     pthread_exit("线程5退出...\n");
35     return NULL;
36 }
37 
38 
39 int main(void)
40 {
41     /*在调用进程中创建一个线程  
42      * int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
43      *                   void *(*start_routine) (void *), void *arg);
44      * 参数:  
45      * thread:保存线程ID空间的首地址
46      * attr:线程的属性
47      * start_routine:线程函数的首地址
48      * arg:给线程函数传递的参数
49      *成功返回0;失败返回非0。
50      * */
51     int i = 0;
52     pthread_t tid[i];
53     void *ret = NULL;
54     /*函数指针数组,存放着各个函数作为数组元素*/
55     void *(*thread[])(void *) = {thread1,
56                                 thread2,
57                                 thread3,
58                                 thread4,
59                                 thread5
60                             };
61     for(i = 0;i < 5;i++)
62     {
63         if(0 != (pthread_create(&tid[i],NULL,thread[i],NULL)))/*arg 给线程函数传递的参数  此为NULL*/
64         {
65                 perror("fail to pthread_create!");
66                 return -1;
67         }
68     }
69     
70     for(i = 0;i < 5;i++)
71     {
72         if(0 != (pthread_join(tid[i],&ret)))/*ret 接受线程退出的值*/
73         {
74                 perror("fail to pthread_create!");
75                 return -1;
76         }
77         printf("ret = %s\n",(char *)ret);
78     }
79     return 0;
80 }

pthread_exit.c

 1 #include <stdio.h>
 2 #include <pthread.h>
 3 /*让一个线程退出!
 4  * void pthread_exit(void *retval);
 5  * retval:退出的值
 6  * */
 7 void *thread(void *arg)/*线程函数*/
 8 {
 9         printf("我是线程!\n");
10         pthread_exit("我要退出了!\n");//没有pthread_join这样的函数来接受线程结束时的状态值,故不打印
11         printf("我还活着!\n");
12 }
13 
14 int main(void)
15 {
16     pthread_t tid;
17     if(0 != (pthread_create(&tid,NULL,thread,NULL)))
18     {
19             perror("fail to pthread_create!");
20             return -1;
21     }
22 
23     getchar();
24     return 0;
25 }

pthread_join.c

 1 #include <stdio.h>
 2 #include <pthread.h>
 3 
 4 void *thread(void *arg)/*线程函数*/
 5 {
 6     sleep(3);
 7     printf("我是线程1 即将退出...\n");
 8     pthread_exit("线程退出...\n");
 9     return NULL;
10 }
11 int main(void)
12 {
13     pthread_t tid;
14     if(0 != (pthread_create(&tid,NULL,thread,NULL)))
15     {
16             perror("fail to pthread_create!");
17             return -1;
18     }
19     /*回收退出的线程资源
20      * int pthread_join(pthread_t thread, void **retval);
21      * thread:线程ID号;
22      * retval:接受线程退出的值
23      * 成功返回0;失败返回非0.
24      */
25     void *ret = NULL;
26     if(0 != (pthread_join(tid,&ret)))//用ret来接受线程退出的状态值
27     {
28             perror("fail to pthread_join!");
29             return -1;
30     }
31     printf("ret = %s\n",(char*)ret);/*显示线程退出状态*/
32     getchar();
33     return 0;
34 }

 pthread_sem.c

 1 /*
 2  * 信号量(资源)
 3  * 第一种线程间同步方式(包含互斥):
 4  * 可使用信号量(一种资源)——多个线程协同处理某事件
 5  *                        //信号量也可用于进程间同步
 6  * */
 7 #include <stdio.h>
 8 #include <pthread.h>
 9 #include <semaphore.h>
10 
11 sem_t sem_r;
12 sem_t sem_w;
13 char buff[64] = {0};
14 
15 void *thread1(void *arg)/*线程函数*/
16 {
17         while(1)
18         {
19             sem_wait(&sem_w);    /*申请-1;        2:申请一个资源:若资源数>0,则该资源数减1;
20                                                 若资源数=0,则阻塞等待能获得该资源或者被信号中断为止*/
21             //write(stdin,buff,sizeof(buff));
22             fgets(buff,sizeof(buff),stdin);
23             printf("我是线程1...\n");
24             sem_post(&sem_r);    /*释放+1;        3:释放一个信号量*/
25         }
26         return NULL;
27 }
28 
29 void *thread2(void *arg)/*线程函数*/
30 {
31         while(1)
32         {
33             sem_wait(&sem_r);
34             printf("我是线程2...\n");
35             printf("buff = %s\n",buff);
36             sem_post(&sem_w);
37         }
38         return NULL;
39 }
40 int main(void)
41 {
42     pthread_t tid1,tid2; 
43     /*初始化信号量
44      * int sem_init(sem_t *sem, int pshared, unsigned int value);
45      * sem: 信号量地址
46      * pshared:信号量的共享域/0为线程间共享;1为进程间共享
47      * value:信号量初始值
48      * 成功返回0;失败返回-1.
49      * */
50     if(-1 == (sem_init(&sem_w,0,1)))//1:初始化信号量
51     {
52         perror("fail to sem_init!");
53         return -1;
54     }
55     if(-1 == (sem_init(&sem_r,0,0)))
56     {
57         perror("fail to sem_init!");
58         return -1;
59     }
60 
61     if(0 != (pthread_create(&tid1,NULL,thread1,NULL)))
62     {
63             perror("fail to pthread_create1!");
64             return -1;
65     }
66     if(0 != (pthread_create(&tid2,NULL,thread2,NULL)))
67     {
68             perror("fail to pthread_create2!");
69             return -1;
70     }
71 
72     if(0 != (pthread_join(tid1,NULL)))/*记得回收线程资源*/
73     {
74             perror("fail to pthread_join!");
75             return -1;
76     }
77     if(0 != (pthread_join(tid2,NULL)))
78     {
79             perror("fail to pthread_join!");
80             return -1;
81     }
82     getchar();
83     return 0;
84 }

pthread_lock.c

 1 /*
 2  *第二种线程间同步方式(包含互斥):
 3  *线程间互斥使用互斥锁(一种资源)——多个线程互斥处理某事件
 4  *临界区域:互斥锁中间的一段代码
 5  *原子操作:CPU执行操作的最小单元,CPU在执行原子操作时不可能在进程或者线程间切换
 6  * */
 7 #include <stdio.h>
 8 #include <pthread.h>
 9 #include <semaphore.h>
10 
11 pthread_mutex_t lock;/*定义一把互斥锁*/
12 char a,b;
13 char data = 10;
14 
15 void *thread1(void *arg)/*线程函数*/
16 {
17         while(1)
18         {
19             pthread_mutex_lock(&lock);/*加锁*/
20             a = data;
21             b = data;
22             a+=1;
23             pthread_mutex_unlock(&lock);/*解锁*/
24             sleep(1);
25         }
26         return NULL;
27 }
28 
29 void *thread2(void *arg)/*线程函数*/
30 {
31         while(1)
32         {
33             pthread_mutex_lock(&lock);
34             if(a != b) printf("a = %d b = %d \n",a,b);
35             pthread_mutex_unlock(&lock);
36             sleep(1);
37         }
38         return NULL;
39 }
40 int main(void)
41 {
42     pthread_t tid1,tid2;
43     /* 定义互斥锁
44        pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;//lock
45        初始化一个互斥锁
46        int pthread_mutex_init(pthread_mutex_t *restrict mutex,const pthread_mutexattr_t *restrict attr);
47            参数:
48             mutex:互斥锁地址
49             attr:互斥锁属性
50             成功返回0;失败返回非0.
51 
52        销毁一个互斥锁
53        int pthread_mutex_destroy(pthread_mutex_t *mutex);
54        
55        int pthread_mutex_lock(pthread_mutex_t *mutex);
56        int pthread_mutex_trylock(pthread_mutex_t *mutex);
57        int pthread_mutex_unlock(pthread_mutex_t *mutex);
58 
59        */
60 
61     if(0 != (pthread_mutex_init(&lock,NULL)))/*初始化互斥锁*/
62     {
63         perror("fail to pthread_mutex_init!");
64         return -1;
65     }
66 
67     if(0 != (pthread_create(&tid1,NULL,thread1,NULL)))/*创建线程*/
68     {
69             perror("fail to pthread_create1!");
70             return -1;
71     }
72     if(0 != (pthread_create(&tid2,NULL,thread2,NULL)))
73     {
74             perror("fail to pthread_create2!");
75             return -1;
76     }
77 
78     if(0 != (pthread_join(tid1,NULL)))/*记得回收退出线程的资源*/
79     {
80             perror("fail to pthread_join!");
81             return -1;
82     }
83     if(0 != (pthread_join(tid2,NULL)))
84     {
85             perror("fail to pthread_join!");
86             return -1;
87     }
88     if(0 != (pthread_mutex_destroy(&lock)))/*销毁互斥锁*/
89     {
90         perror("fail to pthread_mutex_destroy!");
91         return -1;
92     }
93     return 0;
94 }

pthread_cond.c

 1 /*
 2  * 第三种线程间同步方式(包含互斥):
 3  * 条件变量:
 4  * 线程锁一般都是自己去解锁,但是条件变量可以让其他线程帮助该线程解锁
 5  */
 6 #include <stdio.h>
 7 #include <stdlib.h>
 8 #include <error.h>
 9 #include <errno.h>
10 #include <pthread.h>
11 
12 #define error_exit(_errmsg_) error(EXIT_FAILURE,errno,_errmsg_)
13 
14 pthread_cond_t cond;    /*定义一个条件变量*/
15 pthread_mutex_t lock;    /*定义一个互斥锁*/
16 /*初始化一个条件变量
17  * pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
18  * int pthread_cond_init(pthread_cond_t *restrict cond,const pthread_condattr_t *restrict attr);
19  * 参数:
20  *     cond:条件变量的地址
21  *     attr:条件变量的属性
22  *     成功返回0;失败返回非0.
23  * int pthread_cond_destroy(pthread_cond_t *cond);
24  *
25  *int pthread_cond_wait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex); //挂起该线程
26  *int pthread_cond_signal(pthread_cond_t *cond);                                        //唤醒该线程
27 
28  * */
29 
30     /* 定义互斥锁     复习回顾
31        pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;//lock
32        初始化一个互斥锁
33        int pthread_mutex_init(pthread_mutex_t *restrict mutex,const pthread_mutexattr_t *restrict attr);
34            参数:
35             mutex:互斥锁地址
36             attr:互斥锁属性
37             成功返回0;失败返回非0.
38        销毁一个互斥锁
39        int pthread_mutex_destroy(pthread_mutex_t *mutex); 
40        
41        int pthread_mutex_lock(pthread_mutex_t *mutex);
42        int pthread_mutex_trylock(pthread_mutex_t *mutex);
43        int pthread_mutex_unlock(pthread_mutex_t *mutex);
44        */
45 void *thread1(void *arg)
46 {
47     printf("hello world----->1\n");
48     pthread_mutex_lock(&lock);//加互斥锁
49 
50     pthread_cond_wait(&cond,&lock);/*让本线程开始挂起*/ //3.让一个线程挂起...无休止的等待...
51 
52     printf("I love you, baby----->1\n");
53     pthread_mutex_unlock(&lock);//解互斥锁
54     return NULL;
55 }
56 
57 void *thread2(void *arg)
58 {
59     sleep(3);//3s
60     pthread_cond_signal(&cond);/*唤醒1线程*/ //4.唤醒条件变量对应的线程
61 
62     return NULL;
63 }
64 
65 int main(void)
66 {
67     pthread_t tid1,tid2;
68 
69     if(0 != (pthread_mutex_init(&lock,NULL)))
70         error_exit("fail to pthread_mutex_init!");
71 
72     if(0 != (pthread_cond_init(&cond,NULL)))//1.初始化一个条件变量
73         error_exit("fail to pthread_cond_init!");
74 
75     if(0 != (pthread_create(&tid1,NULL,thread1,NULL)))
76         error_exit("fail to pthread_create1!");
77     if(0 != (pthread_create(&tid2,NULL,thread2,NULL)))
78         error_exit("fail to pthread_create2!");
79 
80     if(0 !=(pthread_join(tid1,NULL)))
81         error_exit("fail to pthread_exit1!");
82     if(0 !=(pthread_join(tid2,NULL)))
83         error_exit("fail to pthread_exit2!");
84 
85     if(0 != (pthread_mutex_destroy(&lock)))
86         error_exit("fail to pthread_mutex_destroy!");
87 
88     if(0 != (pthread_cond_destroy(&cond)))//2.销毁一个条件变量
89         error_exit("fail to pthread_cond_destroy!");
90 
91     return 0;
92 }

 

posted @ 2017-03-14 21:20  bkycrmn  阅读(290)  评论(0)    收藏  举报