线程相关介绍

一、线程的操作:

demo1.c ------------------join回收整型数int

线程相关介绍_第1张图片

  1 #include
  2 #include 
  3 
  4      /*  int pthread_create(pthread_t *restrict thread,
  5                           const pthread_attr_t *restrict attr,
  6                           void *(*start_routine)(void *),
  7                           void *restrict arg);
  8      */
  9 void *func1(void *param)
 10 {
 11         printf("func1 param is %d\n",*((int *)param));
 12         static int retval = 100;
 13 //      pthread_t pthread_self(void);
 14         printf("pthread1 ID is %ld\n",pthread_self());
 15 //      noreturn void pthread_exit(void *retval);
 16         pthread_exit((void *)&retval);
 17 }
 18 int main()
 19 {
 20         pthread_t thread1;
 21         int param = 10;
 22         int ret;
 23         int *val = NULL;
 24         pthread_create(&thread1,NULL,func1,(void *)¶m);
 25         if(ret == 0){
 26                 printf("create thread1 sucess\n");
 27         }
 28         printf("main thread ID is %ld\n",pthread_self());
 29 //      int pthread_join(pthread_t thread, void **retval);
 30         pthread_join(thread1,(void **)&val);
 31         printf("val=%d\n",*val);
 32         return 0;
 33 }
~         

demo1.c ------------------join回收字符串char

线程相关介绍_第2张图片

  1 #include
  2 #include 
  3 
  4      /*  int pthread_create(pthread_t *restrict thread,
  5                           const pthread_attr_t *restrict attr,
  6                           void *(*start_routine)(void *),
  7                           void *restrict arg);
  8      */
  9 void *func1(void *param)
 10 {
 11         printf("func1 param is %d\n",*((int *)param));
 12         static char *retval = "hello,world!";
 13 //      pthread_t pthread_self(void);
 14         printf("pthread1 ID is %ld\n",pthread_self());
 15 //      noreturn void pthread_exit(void *retval);
 16         pthread_exit((void *)retval);
 17 }
 18 int main()
 19 {
 20         pthread_t thread1;
 21         int param = 10;
 22         int ret;
 23         char *val = NULL;
 24         pthread_create(&thread1,NULL,func1,(void *)¶m);
 25         if(ret == 0){
 26                 printf("create thread1 sucess\n");
 27         }
 28         printf("main thread ID is %ld\n",pthread_self());
 29 //      int pthread_join(pthread_t thread, void **retval);
 30         pthread_join(thread1,(void **)&val);
 31         printf("val=%s\n",val);
 32         return 0;
 33 }

线程共享进程的内存空间的代码验证

线程相关介绍_第3张图片

  1 #include 
  2 #include
  3 #include 
  4 
  5 /*  int pthread_create(pthread_t *restrict thread,
  6     const pthread_attr_t *restrict attr,
  7     void *(*start_routine)(void *),
  8     void *restrict arg);
  9     */
 10 int p_data = 100;
 11 void *func1(void *param)
 12 {
 13         printf("func1 param is %d\n",*((int *)param));
 14         static char *retval = "hello,world!";
 15         //      pthread_t pthread_self(void);
 16         printf("pthread1 ID is %ld\n",pthread_self());
 17         //      noreturn void pthread_exit(void *retval);
 18         while(1){
 19                 p_data++;
 20                 printf("func1 p_data = %d\n",p_data);
 21                 sleep(1);
 22         }
 23         pthread_exit((void *)retval);
 24 }
 25 void *func2(void *param)
 26 {
 27         printf("func2 param is %d\n",*((int *)param));
 28         static char *retval2 = "hello,world!";
 29         //      pthread_t pthread_self(void);
 30         printf("pthread2 ID is %ld\n",pthread_self());
 31         //      noreturn void pthread_exit(void *retval);
 32         while(1){
 33                 p_data++;
 34                 printf("func2 p_data = %d\n",p_data);
 35                 sleep(1);
 36         }
 37         pthread_exit((void *)retval2);
 38 }
 39 int main()
 40 {
 41         pthread_t thread1;
 42         pthread_t thread2;
 43         int param = 10;
 44         int ret;
 45         char *val = NULL;
 46         pthread_create(&thread1,NULL,func1,(void *)¶m);
 47         if(ret == 0){
 48                 printf("create thread1 sucess\n");
 49         }
 50         pthread_create(&thread2,NULL,func2,(void *)¶m);
 51         if(ret == 0){
 52                 printf("create thread2 sucess\n");
 53         }
 54         printf("main thread ID is %ld\n",pthread_self());
 55         //      int pthread_join(pthread_t thread, void **retval);
 56         pthread_join(thread1,(void **)&val);
 57         pthread_join(thread2,(void **)&val);
 58         printf("val=%s\n",val);
 59         return 0;
 60 }
~                                                                                                                                                                                                                                                                                                                                                                                                                       
~                                                                                                                                                                                                                                                                                                                                                                                                                       
~                                                                                                                                                                                                                                                                                                                                                                                                                       
~                        

 二、与互斥锁相关的操作:

互斥锁的创建等等

p_data = 5 打印=================================

线程相关介绍_第4张图片

  1 #include 
  2 #include 
  3 #include
  4 #include 
  5 
  6 pthread_mutex_t mutex;
  7 int p_data = 0;
  8 void *func1(void *param)
  9 {
 10         pthread_mutex_lock(&mutex);
 11         printf("func1 param is %d\n",*((int *)param));
 12         static char *retval = "hello,world!";
 13         //      pthread_t pthread_self(void);
 14         printf("pthread1 ID is %ld\n",pthread_self());
 15         //      noreturn void pthread_exit(void *retval);
 16         //      int pthread_mutex_lock(pthread_mutex_t *mutex);
 17         while(1){
 18                 p_data++;
 19                 printf("func1 p_data = %d\n",p_data);
 20                 sleep(2);
 21                 if(p_data>5){
 22                         printf("p_data=5   ==============================\n");
 23                         pthread_mutex_unlock(&mutex);
 24                         p_data = 0;
 25                         sleep(1);
 26                 }
 27         }
 28                 pthread_exit((void *)retval);
 29 }
 30 void *func2(void *param)
 31 {
 32         pthread_mutex_lock(&mutex);
 33         printf("func2 param is %d\n",*((int *)param));
 34         static char *retval2 = "hello,world!";
 35         //      pthread_t pthread_self(void);
 36         printf("pthread2 ID is %ld\n",pthread_self());
 37         //      noreturn void pthread_exit(void *retval);
 38         while(1){
 39                 p_data++;
 40                 printf("func2 p_data = %d\n",p_data);
 41                 pthread_mutex_unlock(&mutex);
 42                 sleep(2);
 43         }
 44         pthread_exit((void *)retval2);
 45 }
 46 int main()
 47 {
 48         pthread_t thread1;
 49         pthread_t thread2;
 50         /*      int pthread_mutex_init(pthread_mutex_t *restrict mutex,
 51                 const pthread_mutexattr_t *restrict attr);
 52                 */
 53         pthread_mutex_init(&mutex,NULL);
 54         int param = 10;
 55         int ret;
 56         char *val = NULL;
 57         pthread_create(&thread1,NULL,func1,(void *)¶m);
 58         if(ret == 0){
 59                 printf("create thread1 sucess\n");
 60         }
 61         pthread_create(&thread2,NULL,func2,(void *)¶m);
 62         if(ret == 0){
 63                 printf("create thread2 sucess\n");
 64         }
 65         printf("main thread ID is %ld\n",pthread_self());
 66         //      int pthread_join(pthread_t thread, void **retval);
 67         pthread_join(thread1,(void **)&val);
 68         pthread_join(thread2,(void **)&val);
 69         printf("val=%s\n",val);
 70         //      int pthread_mutex_destroy(pthread_mutex_t *mutex);
 71         pthread_mutex_destroy(&mutex);
 72         return 0;
 73 }
~                                                                                                                                                                                                                                                                                                                                                                                                                       
~                                                                                                                                                                                                                                                                                                                                                                                                                       
~                                              

 

什么是死锁?

        首先要有两把锁,线程1先有锁1后又想拿走锁2,但是锁2被线程2给拿走了。线程2一开始拿了锁2,但又想拿锁1,结果锁1被线程1给拿了,所以双方都卡在那边不动了。中间要有时间片sleep(1),因为线程之间是竞争关系。

线程相关介绍_第5张图片

 三、线程条件控制实现线程的同步

实现p_data = 5时打印==================

线程相关介绍_第6张图片

  1 #include 
  2 #include 
  3 #include
  4 #include 
  5 
  6 pthread_mutex_t mutex;
  7 pthread_cond_t cond;
  8 int p_data = 0;
  9 void *func1(void *param)
 10 {
 11         printf("func1 param is %d\n",*((int *)param));
 12         static char *retval = "hello,world!";
 13         printf("pthread1 ID is %ld\n",pthread_self());
 14         while(1){
 15                 pthread_cond_wait(&cond,&mutex);
 16 
 17                 printf("func1 p_data = %d\n",p_data);
 18                         printf("=================================\n");
 19                         p_data = 0;
 20                         sleep(1);
 21         }
 22                 pthread_exit((void *)retval);
 23 }
 24 void *func2(void *param)
 25 {
 26         pthread_mutex_lock(&mutex);
 27         printf("func2 param is %d\n",*((int *)param));
 28         static char *retval2 = "hello,world!";
 29         //      pthread_t pthread_self(void);
 30         printf("pthread2 ID is %ld\n",pthread_self());
 31         //      noreturn void pthread_exit(void *retval);
 32         while(1){
 33                 p_data++;
 34                 printf("func2 p_data = %d\n",p_data);
 35                 if(p_data==5){
 36                         pthread_cond_signal(&cond);
 37                 }
 38                 pthread_mutex_unlock(&mutex);
 39                 sleep(2);
 40         }
 41         pthread_exit((void *)retval2);
 42 }
 43 int main()
 44 {
 45         pthread_t thread1;
 46         pthread_t thread2;
 47         /*      int pthread_mutex_init(pthread_mutex_t *restrict mutex,
 48                 const pthread_mutexattr_t *restrict attr);
 49                 */
 50         pthread_mutex_init(&mutex,NULL);
 51         pthread_cond_init(&cond,NULL);
 52         int param = 10;
 53         int ret;
 54         char *val = NULL;
 55         pthread_create(&thread1,NULL,func1,(void *)¶m);
 56         if(ret == 0){
 57                 printf("create thread1 sucess\n");
 58         }
 59         pthread_create(&thread2,NULL,func2,(void *)¶m);
 60         if(ret == 0){
 61                 printf("create thread2 sucess\n");
 62         }
 63         printf("main thread ID is %ld\n",pthread_self());
 64         //      int pthread_join(pthread_t thread, void **retval);
 65         pthread_join(thread1,(void **)&val);
 66         pthread_join(thread2,(void **)&val);
 67         printf("val=%s\n",val);
 68         //      int pthread_mutex_destroy(pthread_mutex_t *mutex);
 69         pthread_mutex_destroy(&mutex);
 70         pthread_cond_destroy(&cond);
 71         return 0;
 72 }
~                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
~                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        

你可能感兴趣的:(Linux系统编程,服务器,linux,开发语言,运维)