4.多线程实例:thread.c

 1.在nfs目录下创建 thread.c文件

Code:
  1. #include<stdio.h>  
  2. #include<stdlib.h>  
  3. #include<unistd.h>  
  4. #include<sys/ioctl.h>  
  5. #include<pthread.h>  
  6.   
  7. int task1(int *cnt)  
  8. {  
  9.         while(*cnt < 5)  
  10.         {  
  11.                 sleep(1);  
  12.                 (*cnt) ++;  
  13.                 printf("task1 cnt = %d./n", *cnt);  
  14.   
  15.         }  
  16.   
  17.         return (*cnt);  
  18.   
  19. }  
  20.   
  21. int task2(int* cnt)  
  22. {  
  23.         while(*cnt <5)  
  24.         {  
  25.                 sleep(2);  
  26.                 (*cnt) ++;  
  27.                 printf("task2 cnt = %d./n", *cnt);  
  28.         }  
  29.   
  30.         return (*cnt);  
  31.   
  32. }  
  33.   
  34. int  main(int argc, char* argv[])  
  35. {  
  36.         int result;  
  37.         int t1 = 0;  
  38.         int t2 = 0;  
  39.         int rt1, rt2;  
  40.   
  41.         pthread_t thread1, thread2;  
  42.   
  43.         /* create the first thread.*/  
  44.         result = pthread_create(&thread1, PTHREAD_CREATE_JOINABLE, (void*)task1,(void*)&t1 );  
  45.         if(result)  
  46.         {  
  47.                 perror("pthread_create: task1./n");  
  48.                 exit(EXIT_FAILURE);  
  49.         }  
  50.   
  51.         //created the second thread  
  52.         result = pthread_create(&thread2, PTHREAD_CREATE_JOINABLE,(void*)task2,(void*)&t2 );  
  53.         if(result)  
  54.         {  
  55.                 perror("pthread_create:task2./n");  
  56.                 exit(EXIT_FAILURE);  
  57.         }  
  58.   
  59.         pthread_join(thread1, (void *) &rt1);  
  60.         pthread_join(thread2, (void *) &rt2);  
  61.   
  62.         printf("total %d time./n", t1*t2);  
  63.         printf("return value of task1: %d/n", rt1);  
  64.         printf("return value of task2: %d./n", rt2);  
  65.   
  66.         exit(EXIT_SUCCESS);  
  67.   
  68. }  

2.编写makefile文件:

Code:
  1. EXEC = thread  
  2. OBJS = thread.o  
  3. SVR  = thread.c  
  4.   
  5. CC = arm-linux-gcc  
  6. CFLAGS = -o2 -Wall  
  7. DFLAGS += -lpthread  
  8.   
  9. all:$(EXEC)  
  10.   
  11. $(EXEC):$(OBJS)  
  12.         $(CC) $(DFLAGS) -o $@ $^  
  13. %.o:%.c  
  14.         $(CC) $(CFLAGS) -o $@ -c $<  
  15.   
  16. clean:  
  17.         @rm -vf $(EXEC) *.o *~  

3.执行make,编译该文件

Code:
  1. [root@wzb test4]# ls  
  2. makefile  thread.c  
  3. [root@wzb test4]# make  
  4. arm-linux-gcc -o2 -Wall -o thread.o -c thread.c  
  5. arm-linux-gcc -lpthread -o thread thread.o  
  6. [root@wzb test4]#  

4.在超级终端,进行运行测试.

Code:
  1. /mnt/nfs/arm/project/test4 # ls  
  2. makefile  thread    thread.c  thread.o  
  3. /mnt/nfs/arm/project/test4 # ./thread  
  4. task1 cnt = 1.  
  5. task2 cnt = 1.  
  6. task1 cnt = 2.  
  7. task1 cnt = 3.  
  8. task2 cnt = 2.  
  9. task1 cnt = 4.  
  10. task1 cnt = 5.  
  11. task2 cnt = 3.  
  12. task2 cnt = 4.  
  13. task2 cnt = 5.  
  14. total 25 time.  
  15. return value of task1: 5  
  16. return value of task2: 5.  
  17. /mnt/nfs/arm/project/test4 #  

测试成功!

注意: 该程序用到多线程库,编译时的连接线程库的选项  -lpthread,是必不可少的!

不然编译通不过! 所以的makefile中的 DFLAGS +=  -lpthread

呵呵,有学了的编译连接库的选项: -l    。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值