linux下c语言的多线程编程分享

—-想了解linux下c语言的多线程编程分享的全部内容且更多的C语言教程关注<计算机技术网(www.ctvol.com)!!>

我们在写linux的服务的时候,经常会用到linux的多线程技术以提高程序性能 

多线程的一些小知识:

一个应用程序可以启动若干个线程。

线程(Lightweight Process,LWP),是程序执行的最小单元。

一般一个最简单的程序最少会有一个线程,就是程序本身,也就是主函数(单线程的进程可以简单的认为只有一个线程的进程)

 一个线程阻塞并不会影响到另外一个线程。

多线程的进程可以尽可能的利用系统CPU资源。

1创建线程

先上一段在一个进程中创建一个线程的简单的代码,然后慢慢深入。

  #include<pthread.h>  #include<stdio.h>  #include<stdlib.h>  #include<string.h>  #include<errno.h>  void * func(void * arg)  {   printf("func run...n");   return NULL;  }  int main()  {   pthread_t t1;   int err = pthread_create(&t1,NULL,func,NULL);   if(err!=0)   {    printf("thread_create Failed:%sn",strerror(errno));   }else{    printf("thread_create successn");   }   sleep(1);   return EXIT_SUCCESS;  }  int pthread_create(pthread_t *thread,const pthread_attr_t *attr, void *(*start_routine)(void*), void *arg);

在main函数里面我们调用上面的函数进行创建一个线程。

函数参数:

  第一个参数:pthread_t代表创建线程的唯一标识,是一个结构体,需要我们创建好后,将这个结构体的指针传递过去。

  第二个参数:pthread_attr_t,代表创建这个线程的一些配置,比如分配栈的大小等等。。一般我们可以填NULL,代表默认的创建线程的配置

  第三个参数:代表一个函数的地址,创建线程时,会调用这个函数,函数的返回值是void*,函数的参数也是void*,一般格式就像void * func(void * arg){}

  第四个参数:代表调用第三个函数传递的参数

函数返回值:

  函数成功返回0,如果不等于0则代表函数调用失败,此时通过strerror(errno)可以打印出具体的错误。

  注意:每个线程都拥有一份errno副本,不同的线程拥有不同的errno

最后通过gcc编译

  gcc 1createthread.c -c -o 1createthread.o  gcc 1createthread.o -o thr1 -lpthread

编译的时候需要加上-lpthread 用来链接libpthread.so动态库,不然会提示找不到function

函数调用返回结果

linux下c语言的多线程编程

问题:为什么调用sleep函数

答:可能新创建的线程还没运行到打印的方法主线程就结束了,而主线程结束,所有线程都会结束了。

2线程挂起

有时候我们在一个线程中创建了另外一个线程,主线程要等到创建的线程返回了,获取该线程的返回值后主线程才退出。这个时候就需要用到线程挂起。

  int pthread_join(pthread_t th, void **thr_return);。

pthread_join函数用于挂起当前线程,直至th指定的线程终止为止。

  #include<pthread.h>  #include<stdio.h>  #include<stdlib.h>  #include<string.h>  #include<errno.h>  void * func(void * arg)  {   int i=0;   for(;i<5;i++)   {    printf("func run%dn",i);    sleep(1);   }   int * p = (int *)malloc(sizeof(int));   *p=11;   return p;  }  int main()  {   pthread_t t1,t2;   int err = pthread_create(&t1,NULL,func,NULL);   if(err!=0)   {    printf("thread_create Failed:%sn",strerror(errno));   }else{    printf("thread_create successn");   }   void *p=NULL;   pthread_join(t1,&p);   printf("线程退出:code=%dn",*(int*)p);   return EXIT_SUCCESS;  }

函数执行结果

linux下c语言的多线程编程

我们主函数一直在等待创建的线程执行完,并且得到了线程执行结束的返回值

3线程终止

进程终止时exit()函数,那么线程终止是什么呢?

线程终止的三种情况:

线程只是从启动函数中返回,返回值是线程的退出码。

线程可以被同一进程中的其他线程取消。

线程调用pthread_exit。

  #include<pthread.h>  #include<stdio.h>  #include<stdlib.h>  #include<string.h>  #include<errno.h>  void * func(void * arg)  {   int i=0;   while(1)   {    if(i==10)    {     int * p = (int *)malloc(sizeof(int));     *p=11;     pthread_exit(p);    }    printf("fun run %dn",i++);    sleep(1);   }   return NULL;  }  int main()  {   pthread_t t1,t2;   int err = pthread_create(&t1,NULL,func,NULL);   if(err!=0)   {    printf("thread_create Failed:%sn",strerror(errno));   }else{    printf("thread_create successn");   }   void *p=NULL;   pthread_join(t1,&p);   printf("线程退出:code=%d",*(int*)p);   return EXIT_SUCCESS;  }  void pthread_exit(void *arg);

pthread_exit函数的参数就跟正常线程结束return的使用时一样的,都会被等待它结束的主线程获取到。

函数运行结果:

linux下c语言的多线程编程

4线程分离

  int pthread_detach(pthread_t th);

pthread_detach函数使线程处于被分离状态。

如果不等待一个线程,同时对线程的返回值不感兴趣,可以设置这个线程为被分离状态,让系统在线程退出的时候自动回收它所占用的资源。

一个线程不能自己调用pthread_detach改变自己为被分离状态,只能由其他线程调用pthread_detach。

5线程取消

  int pthread_cancel(pthread_t th);

pthread_cancel函数允许一个线程取消th指定的另一个线程。

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/c-cdevelopment/488590.html

(0)
上一篇 2020年11月12日
下一篇 2020年11月12日

精彩推荐