C语言使用多线程分析和例子 | StriveZs的博客

C语言使用多线程分析和例子

因为最近在进行操作系统实验,涉及到了C语言多线程的实现,因此就写一片博文来讲一下。同样这个对于C++也适用。 首先要引用pthread.h 头文件。   声明一个线程变量: 格式:pthread_t 线程名(ps:这里也可以是一个线程组)   建立一个线程: 格式:pthread_create() pthread_create(pthread_t *restrict tidp,const pthread_attr_t *restrict_attr,void*(*start_rtn)(void*),void restrict arg) 四个参数: 第一个参数为指向线程标识符的指针。 第二个参数用来设置线程属性。 第三个参数是线程运行函数的起始地址。 最后一个参数是运行函数的参数。   pthread_join用来等待一个线程的结束。 格式:pthread_join void a  这个随意定义的 pthread_join (你定义的线程名,&a);   线程的终止: pthread_exit(); 用来终止当前进程。   下面给出一些常用的操作函数: 常用的同步函数:   接下来是应用实例: 我是以生产者消费者问题为实例,这里我是创建了多个生产者和消费者线程,让他们同时发生。   特别注意在Linux系统下编译可能会出现: 需要 在编译时使用命令: gcc a.c -o b -lpthread 这样就可以编译成功了。() End!

StriveZs wechat
Hobby lead  creation, technology change world.
  • Post author: StriveZs
  • Post link: 571.html
  • Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 3.0 unless stating additionally.