现在的位置: 首页 > 综合 > 正文

linux多线程(二) .

2014年10月11日 ⁄ 综合 ⁄ 共 9485字 ⁄ 字号 评论关闭

一、多线程的创建和启动

               一个多线程的程序是通过先创建后启用的方式运行起来的。可以在创建的时候传递参数,也可以在结束的时候返回参数。注意当第二个进程sleep时,第一个线程也在运行。当线程退出时,才继续向下运行主程序。上一个线程未退出,是不会向下执行主程序的。

 

  1. #include<stdio.h>   
  2. #include<pthread.h>   
  3. #include<stdlib.h>
      
  4.   
  5. pthread_t tid,tid2;  
  6. void* myfunc1(void* argv){  
  7.     printf("receive %s\n",(char*)argv);  
  8.     printf("New process:  PID: %d,TID: %u.\n",getpid(),pthread_self());      //得到TID
      
  9.     printf("New process:  PID: %d,TID: %u.\n",getpid(),tid);  
  10.     //sleep(6);   //测试结果为6秒以后阻塞的信息一起输出
      
  11.     sleep(2);  
  12.     pthread_exit((void*)1);  
  13. }  
  14.   
  15. void* myfunc2(void* argv){  
  16.     printf("the second thread is running......\n");  
  17.     sleep(3);  
  18.     return (void*)2;  
  19. }  
  20.   
  21. int main(){  
  22.     void* ret;  
  23.     if(pthread_create(&tid,NULL,myfunc1,"gaga")!=0){     //传递参数
      
  24.         printf("create error\n");  
  25.         exit(1);  
  26.     }  
  27.     if(pthread_create(&tid2,NULL,myfunc2,NULL)!=0){  
  28.         printf("create error\n");  
  29.         exit(1);  
  30.     }  
  31.     printf("the pthread id is %d \n",tid);  
  32.     printf("Main process: PID: %d,TID: %u.\n",getpid(),pthread_self());  
  33.     if(pthread_join(tid,&ret) !=0){  
  34.         printf("join thread 1 error!\n");  
  35.         return 1;  
  36.     }  
  37.     printf("wa kakaka it is %d\n",(int)ret);  
  38.     if(pthread_join(tid2,&ret) != 0){  
  39.         printf("join thread 2 error!\n");  
  40.         return 1;  
  41.     }  
  42.     printf(" %d \n",(int)ret);  
  43. }  

运行结果为:

  1. [fsy@localhost thread]$ ./thread_create   
  2. the pthread id is -1216914576                 //此处还未初始化  
  3. Main process: PID: 6333,TID: 3078055616.  
  4. the second thread is running......            //2线程先运行然后sleep  
  5. receive gaga                                  //1线程运行  
  6. New process:  PID: 6333,TID: 3078052720.  
  7. New process:  PID: 6333,TID: 3078052720.  
  8. wa kakaka it is 1                            //两秒后打印,sleep(6)就在此阻塞6秒  
  9.  2                                           //2线程退出,再运行主程序  
  10. [fsy@localhost thread]$   

 

二、取消线程

             通过pthread_cancel() 函数来结束另一个线程。

 

  1. #include<stdio.h>   
  2. #include<pthread.h>   
  3. #include<stdlib.h>
      
  4.   
  5. pthread_t tid,tid2;  
  6. void* myfunc1(void* argv){  
  7.     pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL); //设置可被其他进程取消
      
  8.     while(1){  
  9.         printf("thread 1 is read to be killed\n");  
  10.         sleep(1);  
  11.     }  
  12.     return 0;  
  13. }  
  14.   
  15. void* myfunc2(void* argv){  
  16.     printf("thread 2 is running!\n");  
  17.     sleep(3);  
  18.     if(pthread_cancel(tid) == 0){  
  19.         printf("Thread 2  will kill Thread 1\n");  
  20.     }  
  21.     return 0;  
  22. }  
  23.   
  24. int main(){  
  25.     if(pthread_create(&tid,NULL,myfunc1,NULL)!=0){  
  26.         printf("create error\n");  
  27.         exit(1);  
  28.     }  
  29.     if(pthread_create(&tid2,NULL,myfunc2,NULL)!=0){  
  30.         printf("create error\n");  
  31.         exit(1);  
  32.     }  
  33.     if(pthread_join(tid,NULL) !=0){  
  34.         printf("join thread 1 error!\n");  
  35.         return 1;  
  36.     }  
  37.     printf("Thread 1 is never back.....\n");  
  38.     if(pthread_join(tid2,NULL) != 0){  
  39.         printf("join thread 2 error!\n");  
  40.         return 1;  
  41.     }  
  42. }  

     运行输出结果为:

 

  1. [fsy@localhost thread]$ ./thread  
  2. thread 2 is running!  
  3. thread 1 is read to be killed  
  4. thread 1 is read to be killed  
  5. thread 1 is read to be killed  
  6. Thread 2  will kill Thread 1  
  7. Thread 1 is never back.....  

三、互斥锁

               两个进程同时访问一个数据就需要加锁。

      用pthread_mutex_lock、pthread_mutex_unlock控制锁。pthread_mutex_destory删除锁。pthread_mutex_trylock判断是否加锁。

  

  1. #include<stdio.h>   
  2. #include<pthread.h>   
  3. #include<stdlib.h>
      
  4.   
  5. pthread_mutex_t mutex;  
  6. pthread_t tid,tid2;  
  7. int meter=0;  
  8. int seconds=0;  
  9. void* myfunc1(void* argv){  
  10.     pthread_mutex_lock(&mutex);    //上锁
      
  11.     while(1){  
  12.         printf("thread is running %d seconds it is %d meter\n",seconds,meter);  
  13.         meter++;  
  14.         seconds++;  
  15.         sleep(1);  
  16.     }  
  17.     pthread_mutex_unlock(&mutex);  //去锁
      
  18.     return 0;  
  19. }  
  20.   
  21. void* myfunc2(void* argv){  
  22.     sleep(1);  
  23.     pthread_mutex_lock(&mutex);  
  24.     while(1){  
  25.         meter++;  
  26.         seconds++;  
  27.     }  
  28.     pthread_mutex_unlock(&mutex);  
  29. }  
  30.   
  31. int main(){  
  32.     pthread_mutex_init(&mutex,NULL);    //第二个参数NULL为互斥锁
      
  33.     if(pthread_create(&tid,NULL,myfunc1,NULL)!=0){  
  34.         printf("create error\n");  
  35.         exit(1);  
  36.     }  
  37.     if(pthread_create(&tid2,NULL,myfunc2,NULL)!=0){  
  38.         printf("create error\n");  
  39.         exit(1);  
  40.     }  
  41.     if(pthread_join(tid,NULL) !=0){  
  42.         printf("join thread 1 error!\n");  
  43.         return 1;  
  44.     }  
  45.     if(pthread_join(tid2,NULL) != 0){  
  46.         printf("join thread 2 error!\n");  
  47.         return 1;  
  48.     }  
  49. }  

     经过加锁,能够依次输出数字。

 

四、信号量

 

             解决同步问题,加锁可以用信号量,它更强大一些,可以用它来控制进程的运行顺序。

             sem_init()初始化一个函数。sem_wait()执行P操作。sem_post()执行V操作。sem_destory()销毁信号量。

 

  1. #include<stdio.h>   
  2. #include<pthread.h>   
  3. #include<stdlib.h>
      
  4. #include<semaphore.h>   
  5.   
  6. pthread_t tid,tid2;  
  7. sem_t sem1;  
  8. sem_t sem2;  
  9. void* myfunc1(void* argv){  
  10.     sem_wait(&sem1);    //p操作使其他线程无法访问
      
  11.     sleep(2);  
  12.     printf("thread 1 is running....\n");  
  13.     sem_post(&sem2);   //V操作使2线程可以运行
      
  14. }  
  15.   
  16. void* myfunc2(void* argv){  
  17.     sem_wait(&sem2);   //在未进行V操作之前会卡住 
      
  18.     printf("thread 2 is running!\n");  
  19.     sleep(1);  
  20.     sem_post(&sem1);  
  21. }  
  22.   
  23. int main(){  
  24.     if(pthread_create(&tid,NULL,myfunc1,NULL)!=0){  
  25.         printf("create error\n");  
  26.         exit(1);  
  27.     }  
  28.     if(pthread_create(&tid2,NULL,myfunc2,NULL)!=0){  
  29.         printf("create error\n");  
  30.         exit(1);  
  31.     }  
  32.     //第二个参数为固定的0,第三个参数为1表示线程可访问,0表示线程不可访问
      
  33.     sem_init(&sem1,0,1);  
  34.     sem_init(&sem2,0,0);  
  35.     if(pthread_join(tid,NULL) !=0){  
  36.         printf("join thread 1 error!\n");  
  37.         return 1;  
  38.     }  
  39.     if(pthread_join(tid2,NULL) != 0){  
  40.         printf("join thread 2 error!\n");  
  41.         return 1;  
  42.     }  
  43.     sem_destroy(&sem1);  
  44.     sem_destroy(&sem2);  
  45.     printf("finish!\n");  
  46. }  

    通过信号量和P、V的操作实现了线程同步的控制。
 

 

 

 本篇博客出自 
阿修罗道
,转载请注明出处:http://blog.csdn.net/fansongy/article/details/6882586

抱歉!评论已关闭.