Chinaunix首页 | 论坛 | 博客
  • 博客访问: 275656
  • 博文数量: 113
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1044
  • 用 户 组: 普通用户
  • 注册时间: 2015-02-15 16:09
文章分类

全部博文(113)

文章存档

2016年(5)

2015年(108)

我的朋友

分类: C/C++

2015-09-17 23:12:15

注意编译用,gcc   -o  test  test.c   lpthread

 点击(此处)折叠或打开

  1. /*********
  2. 多线程编程
  3. **********/
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <pthread.h>
  7.    
  8.    
  9. void mythread()
  10. {
  11.       int i;
  12.       for(i=0;i<3;i++)
  13.            printf("This is a pthread:/n");
  14.       pthread_exit("Thank you for the CPU time/n");
  15. }
  16. int main()
  17. {
  18.       pthread_t id;
  19.       int i, ret;
  20.       void *thread_result;
  21.       ret = pthread_create(&id,NULL,(void *)mythread,NULL);
  22.       if(ret!=0)
  23.       {
  24.            printf("Create pthread error");
  25.            exit(0);
  26.       }
  27.       for(i=0;i<3;i++)
  28.            printf("This is the main process:/n");
  29.       pthread_join(id,&thread_result);
  30.       printf("Thread joined,it returned:%s",(char *)thread_result);
  31.       return 0;
  32. }
  33.    
  34.    
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <pthread.h>
  38.    
  39. void mythread()
  40. {
  41.       int i;
  42.       for(i=0;i<3;i++)
  43.       {
  44.            printf("This is a pthread:/n");
  45.            sleep(1);
  46.       }
  47.       pthread_exit("Thank you for the CPU time/n");
  48. }
  49. int main()
  50. {
  51.       pthread_t id;
  52.       int i, ret;
  53.       void *thread_result;
  54.       ret = pthread_create(&id,NULL,(void *)mythread,NULL);
  55.       if(ret!=0)
  56.       {
  57.            printf("Create pthread error");
  58.            exit(0);
  59.       }
  60.       for(i=0;i<3;i++)
  61.       {
  62.            printf("This is the main process:/n");
  63.            sleep(1);
  64.       }
  65.       pthread_join(id,&thread_result);
  66.       printf("Thread joined,it returned:%s",(char *)thread_result);
  67.       return 0;
  68. }
  69.    
  70.    
  71. /*********
  72. 取消线程
  73. **********/
  74. #include <stdio.h>
  75. #include <stdlib.h>
  76. #include <pthread.h>
  77.    
  78. void mythread()
  79. {
  80.       int i,ret;
  81.       ret = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL);
  82.       if(ret!=0)
  83.       {
  84.            printf("Thread pthread_setcancelstate error");
  85.            exit(1);
  86.       }
  87.       ret = pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,NULL);
  88.       if(ret!=0)
  89.       {
  90.            printf("Thread pthread_setcanceltype error");
  91.            exit(1);
  92.       }
  93.       for(i=0;i<10;i++)
  94.       {
  95.            printf("Thread is running(%d):/n",i);
  96.            sleep(1);
  97.       }
  98.       pthread_exit("Thank you for the CPU time/n");
  99. }
  100. int main()
  101. {
  102.       pthread_t id;
  103.       int i, ret;
  104.       void *thread_result;
  105.       ret = pthread_create(&id,NULL,(void *)mythread,NULL);
  106.       if(ret!=0)
  107.       {
  108.            printf("Create pthread error");
  109.            exit(0);
  110.       }
  111.       sleep(2);
  112.       printf("Canceling thread/n");
  113.       ret = pthread_cancle(id);
  114.       if(ret!=0)
  115.       {
  116.            printf("Thread cancelation failed/n");
  117.            exit(1);
  118.       }
  119.       sleep(2);
  120.       pthread_join(id,&thread_result);
  121.       printf("Thread joined,it returned:%s",(char *)thread_result);
  122.       return 0;
  123. }
  124.    
  125.    
  126. /*********
  127. 线程属性
  128. **********/
  129. #include <stdio.h>
  130. #include <stdlib.h>
  131. #include <pthread.h>
  132. int thread_finished = 0;
  133. void mythread()
  134. {
  135.       printf("Thread is running.../n");
  136.       sleep(1);
  137.       printf("Thread is exiting now/n");
  138.       thread_finished=1;
  139.       pthread_exit(NULL);
  140. }
  141. int main()
  142. {
  143.       pthread_t id;
  144.       pthread_attr_t thread_attr;
  145.       int ret;
  146.       ret=pthread_attr_init(&thread_attr);
  147.       if(ret!=0)
  148.       {
  149.            printf("Pthread_attr_init error/n");
  150.            exit(0);
  151.       }
  152.       ret = pthread_attr_setdetachstate(&thread_attr,PTHREAD_CREATE_DETACHED);
  153.       if(ret!=0)
  154.       {
  155.            printf("pthread_attr_setdetachstate error/n");
  156.            exit(0);
  157.       }
  158.       ret = pthread_create(&id,&thread_attr,(void *)mythread,NULL);
  159.       if(ret!=0)
  160.       {
  161.            printf("Create pthread error/n");
  162.            exit(0);
  163.       }
  164.       while(!thread_finished)
  165.       {
  166.            printf("Waiting for thread finished .../n");
  167.            sleep(1);
  168.       }
  169.       printf("Done/n");
  170.       return 0;
  171. }
  172.    
  173. /*********
  174. 线程互斥量
  175. **********/
  176. #include <stdio.h>
  177. #include <stdlib.h>
  178. #include <pthread.h>
  179. int x;
  180. pthread_mutex_t mutex;
  181.    
  182. void thread1(void)
  183. {
  184.       while(x>0)
  185.       {
  186.            pthread_mutex_lock(&mutex);
  187.            printf("Thread 1 is running x=%d/n",x);
  188.            x--;
  189.            pthread_mutex_unlock(&mutex);
  190.            sleep(1);
  191.       }
  192.       pthread_exit(NULL);
  193. }
  194.    
  195. void thread2(void)
  196. {
  197.       while(x>0)
  198.       {
  199.            pthread_mutex_lock(&mutex);
  200.            printf("Thread 2 is running x=%d/n",x);
  201.            x--;
  202.            pthread_mutex_unlock(&mutex);
  203.            sleep(1);
  204.       }
  205.       pthread_exit(NULL);
  206. }
  207.    
  208. int main()
  209. {
  210.       pthread_t id1,id2;
  211.       int ret;
  212.       ret = pthread_mutex_init(&mutex,NULL);
  213.       if(ret!=0)
  214.       {
  215.            printf("pthread_mutex_init error/n");
  216.            exit(0);
  217.       }
  218.       x=10;
  219.       ret = pthread_create(&id1,NULL,(void *)thread1,NULL);
  220.       if(ret!=0)
  221.       {
  222.            printf("Create pthread 1 error/n");
  223.            exit(0);
  224.       }
  225.       ret = pthread_create(&id2,NULL,(void *)thread2,NULL);
  226.       if(ret!=0)
  227.       {
  228.            printf("Create pthread 2 error/n");
  229.            exit(0);
  230.       }
  231.       pthread_join(id1,NULL);
  232.       pthread_join(id2,NULL);
  233.       printf("Done/n");
  234.       return 0;
  235. }
  236.    
  237.    
  238. /*********
  239. 线程条件
  240. **********/
  241. #include <stdio.h>
  242. #include <stdlib.h>
  243. #include <pthread.h>
  244. int x;
  245. pthread_mutex_t mutex;
  246. pthread_cond_t cond;
  247.    
  248. void producer(void)
  249. {
  250.       while(1)
  251.       {
  252.            pthread_mutex_lock(&mutex);
  253.            int i;
  254.            for(i=0;i<3-x;i++)
  255.            {
  256.                  x++;
  257.                  printf("Producing:x=%d/n",x);
  258.                  sleep(1);
  259.            }
  260.            if(x>=3)
  261.            {
  262.                  pthread_cond_signal(&cond);
  263.                  printf("Producing complete %d/n",x);
  264.            }
  265.            pthread_mutex_unlock(&mutex);
  266.            sleep(1);
  267.       }
  268.       pthread_exit(NULL);
  269. }
  270.    
  271. void consumer(void)
  272. {
  273.       while(1)
  274.       {
  275.            pthread_mutex_lock(&mutex);
  276.            while(x<3)
  277.            {
  278.                  pthread_cond_wait(&cond,&mutex);
  279.                  printf("start consuming %d/n",x);
  280.            }
  281.            if(x>0)
  282.            {
  283.                  x--;
  284.                  printf(" consuming %d/n",x);
  285.                  sleep(1);
  286.            }
  287.            pthread_mutex_unlock(&mutex);
  288.       }
  289.       pthread_exit(NULL);
  290. }
  291.    
  292. int main()
  293. {
  294.       pthread_t id1,id2;
  295.       int ret;
  296.       ret = pthread_mutex_init(&mutex,NULL);
  297.       if(ret!=0)
  298.       {
  299.            printf("pthread_mutex_init error/n");
  300.            exit(0);
  301.       }
  302.       ret=pthread_cond_init(&cond,NULL);
  303.       if(ret!=0)
  304.       {
  305.            printf("pthread_cond_init error/n");
  306.            exit(0);
  307.       }
  308.       ret = pthread_create(&id1,NULL,(void *)producer,NULL);
  309.       if(ret!=0)
  310.       {
  311.            printf("Create pthread producer error/n");
  312.            exit(0);
  313.       }
  314.       ret = pthread_create(&id2,NULL,(void *)consumer,NULL);
  315.       if(ret!=0)
  316.       {
  317.            printf("Create pthread consumer, error/n");
  318.            exit(0);
  319.       }
  320.       pthread_join(id1,NULL);
  321.       pthread_join(id2,NULL);
  322.       printf("Done/n");
  323.       return 0;
  324. }
  325.    
  326. #include <stdio.h>
  327. #include <stdlib.h>
  328. #include <string.h>
  329. #include <pthread.h>
  330. #include <semaphore.h>
  331. sem_t sem;
  332. char buffer[256];
  333.    
  334. void mythread(void)
  335. {
  336.       sem_wait(&sem);
  337.       while(strncmp("exit",bufer,4))
  338.       {
  339.            printf("You input %d characters/n",strlen(buffer)-1);
  340.            sem_wait(&sem);
  341.       }
  342.       pthread_exit(NULL);
  343. }
  344. int main()
  345. {
  346.       pthread_t id;
  347.       int ret;
  348.       ret=sem_init(&sem,0,0);
  349.       if(ret!=0)
  350.       {
  351.            printf("Semaphare initialization failed/n");
  352.            exit(1);
  353.       }
  354.       ret=pthread_create(&id,NULL,(void *)&mythread,NULL)
  355.       if(ret!=0)
  356.       {
  357.            printf("pthread_create failed/n");
  358.            exit(1);
  359.       }
  360.       printf("Input some text,Enter'.'to finish/n");
  361.       while(strncmp("exit",buffer,4)!=0)
  362.       {
  363.            fgets(buffer,256,stdin);
  364.            sem_post(&sem);
  365.       }
  366.       pthread_join(id,NULL);
  367.       printf("Thtead joined,/n");
  368.       sem_destroy(&sem);
  369.       return 0;
  370. }

阅读(1171) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~