Linux创建线程

 

 
 
  1. #include"stdio.h"  
  2. #include"unistd.h"  
  3. #include"stdlib.h"  
  4. #include"pthread.h"  
  5. #include"semaphore.h"  
  6. #include"string.h"  
  7. void *thread_function(void *arg);  
  8. pthread_mutex_t work_mutex;  
  9.  
  10. #define WORK_SIZE 1024  
  11. char work_area[WORK_SIZE];  
  12. int time_to_exit=0;  
  13.  
  14. int main()  
  15. {  
  16.     int res;  
  17.     pthread_t a_thread;  
  18.     void *thread_result;  
  19.     res=pthread_mutex_init(&work_mutex,NULL);  
  20.     if(res!=0){  
  21.         perror("Mutex initialization failed");  
  22.         exit(EXIT_FAILURE);  
  23.     }  
  24.     res=pthread_create(&a_thread,NULL,thread_function,NULL);  
  25.     if(res!=0){  
  26.         perror("Thread creation failed!\n");  
  27.         exit(EXIT_FAILURE);  
  28.     }  
  29.       
  30.     pthread_mutex_lock(&work_mutex);  
  31.     printf("Input some text.Enter 'end' to finish\n");  
  32.     while(!time_to_exit){  
  33.         fgets(work_area,WORK_SIZE,stdin);  
  34.         pthread_mutex_unlock(&work_mutex);  
  35.         while(1){  
  36.             pthread_mutex_unlock(&work_mutex);  
  37.             if(work_area[0]!='\0'){  
  38.                 pthread_mutex_unlock(&work_mutex);  
  39.                 sleep(1);  
  40.             }  
  41.             else{  
  42.                 break;  
  43.             }  
  44.         }  
  45.         pthread_mutex_unlock(&work_mutex);  
  46.         printf("\nWaiting for thresd to finish...\n");  
  47.         res=pthread_join(a_thread,&thread_result);  
  48.         if(res!=0){  
  49.             perror("Thread join failed!\n");  
  50.             exit(EXIT_FAILURE);  
  51.         }  
  52.         printf("Thread joined!\n");  
  53.         pthread_mutex_destroy(&work_mutex);  
  54.         exit(EXIT_SUCCESS);  
  55.     }  
  56. }  
  57. void *thread_function(void *arg)  
  58. {  
  59.     sleep(1);  
  60.     pthread_mutex_lock(&work_mutex);  
  61.     while(strncmp("end",work_area,3)!=0){  
  62.         printf("You input %d chatacters\n",strlen(work_area)-1);  
  63.         work_area[0]='\0';  
  64.         pthread_mutex_lock(&work_mutex);  
  65.         sleep(1);  
  66.         pthread_mutex_lock(&work_mutex);  
  67.         while(work_area[0]=='\0'){  
  68.             pthread_mutex_unlock(&work_mutex);  
  69.             sleep(1);  
  70.             pthread_mutex_lock(&work_mutex);  
  71.         }  
  72.     }  
  73.     time_to_exit=1;  
  74.     work_area[0]='\0';  
  75.     pthread_mutex_unlock(&work_mutex);  
  76.     pthread_exit(0);  
  77. }  

 

本文出自 “阿凡达” 博客,请务必保留此出处http://shamrock.blog.51cto.com/2079212/702535

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linux 中,可以使用以下步骤来创建线程: 1. 包含头文件:首先,需要包含 `<pthread.h>` 头文件,该头文件提供了线程相关的函数和数据类型的定义。 2. 定义线程函数:接下来,需要定义一个函数作为线程的入口点。线程函数的返回类型为 `void*`,参数为 `void*`。这个函数将在新线程中执行。 3. 创建线程:使用 `pthread_create` 函数来创建线程。该函数接受四个参数:指向线程标识符的指针、线程属性、线程函数的指针和传递给线程函数的参数。 4. 等待线程结束(可选):如果需要等待线程执行完成,可以使用 `pthread_join` 函数。该函数会阻塞调用线程,直到指定的线程结束。 下面是一个简单的示例代码: ```c #include <stdio.h> #include <pthread.h> void* thread_func(void* arg) { int thread_num = *(int*)arg; printf("Hello from thread %d\n", thread_num); pthread_exit(NULL); } int main() { pthread_t thread_id; int thread_num = 1; // 创建线程 if (pthread_create(&thread_id, NULL, thread_func, &thread_num) != 0) { printf("Failed to create thread\n"); return 1; } // 等待线程结束 if (pthread_join(thread_id, NULL) != 0) { printf("Failed to join thread\n"); return 1; } printf("Thread joined\n"); return 0; } ``` 在上面的示例中,我们创建了一个新线程,它将执行 `thread_func` 函数,并传递了一个整数作为参数。主线程等待新线程执行完成后,输出 "Thread joined"。请注意,这只是一个简单的示例,实际应用中可能需要更复杂的线程管理和同步机制。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值