这篇文章主要介绍了linux下c语言的多线程编程,需要的朋友可以参考下
我们在写linux的服务的时候,经常会用到linux的多线程技术以提高程序性能
多线程的一些小知识:
一个应用程序可以启动若干个线程。
线程(Lightweight Process,LWP),是程序执行的最小单元。
立即学习“C语言免费学习笔记(深入)”;
一般一个最简单的程序最少会有一个线程,就是程序本身,也就是主函数(单线程的进程可以简单的认为只有一个线程的进程)
一个线程阻塞并不会影响到另外一个线程。
多线程的进程可以尽可能的利用系统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);</errno.h></string.h></stdlib.h></stdio.h></pthread.h>
在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
函数调用返回结果
问题:为什么调用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<p>函数执行结果</p> <p style="text-align: center"><img alt="" src="https://img.php.cn/upload/article/000/000/194/761d48b2b485187975e893de94d24c61-1.jpg"></p> <p>我们主函数一直在等待创建的线程执行完,并且得到了线程执行结束的返回值</p> <p><span style="color: #ff0000"><strong>3线程终止</strong></span></p> <p>进程终止时exit()函数,那么线程终止是什么呢?</p> <p>线程终止的三种情况:</p> <p>线程只是从启动函数中返回,返回值是线程的退出码。</p> <p>线程可以被同一进程中的其他线程取消。</p> <p>线程调用pthread_exit。</p> <p class="jb51code"><br></p> <pre class="brush:cpp;">#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);</errno.h></string.h></stdlib.h></stdio.h></pthread.h>
pthread_exit函数的参数就跟正常线程结束return的使用时一样的,都会被等待它结束的主线程获取到。
函数运行结果:
4线程分离
int pthread_detach(pthread_t th);
pthread_detach函数使线程处于被分离状态。
如果不等待一个线程,同时对线程的返回值不感兴趣,可以设置这个线程为被分离状态,让系统在线程退出的时候自动回收它所占用的资源。
一个线程不能自己调用pthread_detach改变自己为被分离状态,只能由其他线程调用pthread_detach。
5线程取消
int pthread_cancel(pthread_t th);
pthread_cancel函数允许一个线程取消th指定的另一个线程。
函数成功,返回0,否则返回非0。
#include<pthread.h> #include<stdio.h> #include<stdlib.h> #include<string.h> #include<errno.h> void * func1(void * arg) { while(1) { printf("fun run...n"); sleep(1); } return NULL; } int main() { pthread_t t1; if(pthread_create(&t1,NULL,func1,NULL)!=0) { printf("thread_create Failed:%sn",strerror(errno)); return -1; } sleep(5); pthread_cancel(t1); pthread_join(t1,NULL); return EXIT_SUCCESS; }</errno.h></string.h></stdlib.h></stdio.h></pthread.h>
函数执行结果:
上面我们说过创建一个线程函数pthread_create的第二个参数,用来决定创建线程的一些初始化状态,这里我们 举个例子,改线程一创建就是分离状态的线程(
上面介绍了pthread_detach函数的概念,可以通过pthread_attr_t在创建线程的时候就指定线程属性为detach,而不用创建以后再去修改线程属性。
)
先上一段代码:
#include<pthread.h> #include<stdio.h> #include<stdlib.h> #include<string.h> #include<errno.h> void * func(void * arg) { int i=0; for(;i<p>pthread_attr_t就是我们要传入的参数的结构体,一般申明的步骤有</p> <p>1,申明一个pthread_attr_t对象</p> <p>2,函数pthread_attr_init初始化attr结构。</p> <p>3,设置线程的一些属性,比如pthread_attr_setdetachstate函数就是设置该线程创建的时候为正常状态还是分离状态。</p> <p>4,函数pthread_attr_destroy释放attr内存空间</p> <p>pthread_attr_setdetachstate把线程属性设置为下面两个合法值之一:</p> <table border="0" style="font-size: 14px; max-width: 850px; border-top: silver 1px solid; font-family: Verdana, Arial, Helvetica, sans-serif; border-right: silver 1px solid; white-space: normal; word-spacing: 0px; border-collapse: collapse; border-bottom: silver 1px solid; text-transform: none; font-weight: normal; color: rgb(51,51,51); padding-bottom: 0px; font-style: normal; padding-top: 0px; padding-left: 0px; margin: 0px; border-spacing: 0px; border-left: silver 1px solid; orphans: 2; widows: 2; letter-spacing: normal; padding-right: 0px; background-color: rgb(255,255,255); text-indent: 0px; font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px"><tbody style="padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; padding-right: 0px"> <tr style="padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; padding-right: 0px" class="firstRow"> <td class="oa1" width="423" style="border-top: silver 1px solid; border-right: silver 1px solid; border-collapse: collapse; border-bottom: silver 1px solid; padding-bottom: 3px; padding-top: 3px; padding-left: 3px; margin: 0px; border-left: silver 1px solid; padding-right: 3px"><p style="padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 10px auto; padding-right: 0px; text-indent: 0px">值</p></td> <td class="oa1" width="355" style="border-top: silver 1px solid; border-right: silver 1px solid; border-collapse: collapse; border-bottom: silver 1px solid; padding-bottom: 3px; padding-top: 3px; padding-left: 3px; margin: 0px; border-left: silver 1px solid; padding-right: 3px"><p style="padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 10px auto; padding-right: 0px; text-indent: 0px">说明</p></td> </tr> <tr style="padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; padding-right: 0px"> <td class="oa2" width="423" style="border-top: silver 1px solid; border-right: silver 1px solid; border-collapse: collapse; border-bottom: silver 1px solid; padding-bottom: 3px; padding-top: 3px; padding-left: 3px; margin: 0px; border-left: silver 1px solid; padding-right: 3px"><p style="padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 10px auto; padding-right: 0px; text-indent: 0px">PTHREAD_CREATE_DETACHED</p></td> <td class="oa2" width="355" style="border-top: silver 1px solid; border-right: silver 1px solid; border-collapse: collapse; border-bottom: silver 1px solid; padding-bottom: 3px; padding-top: 3px; padding-left: 3px; margin: 0px; border-left: silver 1px solid; padding-right: 3px"><p style="padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 10px auto; padding-right: 0px; text-indent: 0px">设置线程为分离状态</p></td> </tr> <tr style="padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; padding-right: 0px"> <td class="oa3" width="423" style="border-top: silver 1px solid; border-right: silver 1px solid; border-collapse: collapse; border-bottom: silver 1px solid; padding-bottom: 3px; padding-top: 3px; padding-left: 3px; margin: 0px; border-left: silver 1px solid; padding-right: 3px"><p style="padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 10px auto; padding-right: 0px; text-indent: 0px">PTHREAD_CREATE_JOINABLE</p></td> <td class="oa3" width="355" style="border-top: silver 1px solid; border-right: silver 1px solid; border-collapse: collapse; border-bottom: silver 1px solid; padding-bottom: 3px; padding-top: 3px; padding-left: 3px; margin: 0px; border-left: silver 1px solid; padding-right: 3px"><p style="padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 10px auto; padding-right: 0px; text-indent: 0px">设置线程为正常状态</p></td> </tr> </tbody></table> <p>上面函数运行结果:</p> <p style="text-align: center"><img alt="" src="https://img.php.cn/upload/article/000/000/194/761d48b2b485187975e893de94d24c61-4.jpg"></p> <p>因为线程是个分离状态的,所以pthread_join挂起会失效,主线程很快运行结束,程序也就结束了,创建的线程还没来得及运行</p> <p><strong>线程同步</strong></p> <p>有时候我们多个线程处理订单扣减库存会遇到这样的问题,两个线程同时进入一段代码先查询库存,两个都查出来为还剩一件库存,第一个线程用掉这个库存后,将库存变为0,但是第二个线程刚才也查出来为1了,所以他还认为有库存,</p> <p>这个时候操作就会引发我们想不到的意外,库存变为负数了!!所以这个时候就需要使用线程的同步!!</p> <p>先上一段代码看看效果:</p> <p class="jb51code"><br></p> <pre class="brush:bash;">#include<pthread.h> #include<stdio.h> #include<pthread.h> #include<stdio.h> #include<stdlib.h> #include<string.h> #include<errno.h> void * func(void * arg) { int threadno =*(int*)arg; int i=0; for(;i<p>函数运行结果:</p> <p style="text-align: center"><img alt="" src="https://img.php.cn/upload/article/000/000/194/c696db446e357ad9a31396f99e5f3206-5.jpg"></p> <p>可以看到两个线程是没有规律的争相处理的,如果这段代码是扣减库存就完蛋啦!,所以我们要对这段代码进行加锁,同一时刻只能有一个线程进入操作!</p> <p>先上代码:</p> <p class="jb51code"><br></p> <pre class="brush:cpp;">#include<pthread.h> #include<stdio.h> #include<stdlib.h> #include<string.h> #include<errno.h> pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; void * func(void * arg) { pthread_mutex_lock(&mutex);//对mutex加锁,其他线程进入后将会挂起,知道这个锁被解锁 int threadno =*(int*)arg; int i=0; for(;i<p>函数运行结果:</p> <p style="text-align: center"><img alt="" src="https://img.php.cn/upload/article/000/000/194/c696db446e357ad9a31396f99e5f3206-6.jpg"></p> <p>可以看到第二个线程先进入后一直运行结束,对mutex解锁后,第一个线程才能进方法里面运行!否则会挂起,一直等到锁被解锁!</p> <p>PTHREAD_MUTEX_INITIALIZER是初始化一个快速锁的宏定义。<br></p> <p class="jb51code"><br></p> <pre class="brush:cpp;">pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
加锁解锁函数:
int pthread_mutex_lock(pthread_mutex_t *mutex); int pthread_mutex_unlock(pthread_mutex_t *mutex);
总结