linux pthread多线程编程模板
pthread_create() 创建线程,pthread_join()让线程一直运行下去。
链接时要加上-lpthread选项。
pthread_create中, 第三个参数为线程函数,定义如下:
void * heartbeat_thread()
{
...
}
下面是main.c :
#include <pthread.h> pthread_t thread[MAX_THREAD_NUM];
pthread_mutex_t cache_mutex;
pthread_mutex_t variable_mutex;
pthread_mutex_t time_mutex;
pthread_mutex_t msg_mutex; void thread_create(void)
{
int temp;
memset(thread, , sizeof(pthread_t) * MAX_THREAD_NUM);
if((temp = pthread_create(&thread[], NULL, heartbeat_thread, NULL)) != )
printf("heartbeat thread create failed\n");
if((temp = pthread_create(&thread[], NULL, timesync_thread, NULL)) != )
printf("tiemsync thread create failed\n");
if((temp = pthread_create(&thread[], NULL, udpclient_thread, NULL)) != )
printf("udpclient thread create failed\n");
if((temp = pthread_create(&thread[], NULL, udpserver_thread, NULL)) != )
printf("udpserver thread create failed\n");
if((temp = pthread_create(&thread[], NULL, msgdisplay_thread, NULL)) != )
printf("msgdisplay thread create failed\n");
} void thread_wait(void)
{
if(thread[] !=)
{
pthread_join(thread[],NULL);
printf("heartbeat thread finish\n");
}
if(thread[] !=)
{
pthread_join(thread[],NULL);
printf("tiemsync thread finish\n");
}
if(thread[] !=)
{
pthread_join(thread[],NULL);
printf("udpclient thread finish\n");
}
if(thread[] !=)
{
pthread_join(thread[],NULL);
printf("udpserver thread finish\n");
}
if(thread[] !=)
{
pthread_join(thread[],NULL);
printf("msgdisplay thread finish\n");
}
} int main(void)
{
pthread_mutex_init(&cache_mutex,NULL);
pthread_mutex_init(&variable_mutex,NULL);
pthread_mutex_init(&time_mutex,NULL);
pthread_mutex_init(&msg_mutex,NULL); thread_create();
thread_wait(); pthread_mutex_destroy(&msg_mutex);
pthread_mutex_destroy(&time_mutex);
pthread_mutex_destroy(&variable_mutex);
pthread_mutex_destroy(&cache_mutex);
return ;
}
linux pthread多线程编程模板的更多相关文章
- [转]Linux 的多线程编程的高效开发经验
Linux 平台上的多线程程序开发相对应其他平台(比如 Windows)的多线程 API 有一些细微和隐晦的差别.不注意这些 Linux 上的一些开发陷阱,常常会导致程序问题不穷,死锁不断.本文中我们 ...
- 转载自~浮云比翼:Step by Step:Linux C多线程编程入门(基本API及多线程的同步与互斥)
Step by Step:Linux C多线程编程入门(基本API及多线程的同步与互斥) 介绍:什么是线程,线程的优点是什么 线程在Unix系统下,通常被称为轻量级的进程,线程虽然不是进程,但却可 ...
- pthread多线程编程的学习小结
pthread多线程编程的学习小结 pthread 同步3种方法: 1 mutex 2 条件变量 3 读写锁:支持多个线程同时读,或者一个线程写 程序员必上的开发者服务平台 —— DevSt ...
- Linux 的多线程编程的高效开发经验(转)
http://www.ibm.com/developerworks/cn/linux/l-cn-mthreadps/ 背景 Linux 平台上的多线程程序开发相对应其他平台(比如 Windows)的多 ...
- Linux 的多线程编程的高效开发经验
http://www.ibm.com/developerworks/cn/linux/l-cn-mthreadps/ 背景 Linux 平台上的多线程程序开发相对应其他平台(比如 Windows)的多 ...
- Linux下多线程编程遇到的一些问题
今天在学习了Linux的多线程编程的基础的知识点.于是就试着做了一个简单的Demo.本以为会得到预期的结果.不成想却遇到了意想不到的问题. 代码展示 我的C 代码很简单,就是一个简单的示例程序,如下: ...
- clone的fork与pthread_create创建线程有何不同&pthread多线程编程的学习小结(转)
进程是一个指令执行流及其执行环境,其执行环境是一个系统资源的集合,这些资源在Linux中被抽 象成各种数据对象:进程控制块.虚存空间.文件系统,文件I/O.信号处理函数.所以创建一个进程的 过程就是这 ...
- VC++6.0 下配置 pthread库2010年12月12日 星期日 13:14VC下的pthread多线程编程 转载
VC++6.0 下配置 pthread库2010年12月12日 星期日 13:14VC下的pthread多线程编程 转载 #include <stdio.h>#include &l ...
- Linux下多线程编程
一.为什么要引入线程? 使用多线程的理由之一是和进程相比,它是一种非常"节俭"的多任务操作方式.在Linux系统下,启动一个新的进程必须分配给它独立的地址空间,建立众多的数据表来维 ...
随机推荐
- php中_initialize()函数与 __construct()函数的区别说明
_initialize()方法是在任何方法执行之前,都要执行的,当然也包括 __construct构造函数. 也就是说如果存在_initialize()函数,调用对象的任何方法都会导致_initial ...
- [java]借助java.io.File类,完成文件批量改名
列出某目录下的所有文件,将文件改名,借助java.io.File一个类就可以完成.大家可以发现,代码中的具体方法都是File一个类的. 另外要注意的是,File的全路径名,路径是一致的,父路径其实是文 ...
- 4 个独特的 Linux 终端模拟器(转)
4 个独特的 Linux 终端模拟器 译自:https://www.linux.com/blog/learn/2018/12/4-unique-terminals-linux作者: Jack Wall ...
- Hexo博客skapp主题部署填坑指南
相信大家都很喜欢 hexo skapp 的主题,由于作者采用结巴分词,加上需要依赖各种各样的环境 所以可能大家踩过很多坑,也许每个人踩得坑不一样,这里使用 Docker 容器 centos 来部署, ...
- 阶段5 3.微服务项目【学成在线】_day03 CMS页面管理开发_05-新增页面-服务端-接口测试
其从程序swaggerUI测试 pageName和别名都是 test003 { "dataUrl": "string", "htmlFileId&qu ...
- 阶段5 3.微服务项目【学成在线】_day02 CMS前端开发_11-webpack研究-npm和cnpm安装配置
node.js安装完成后,就自动安装了webpack. npm -v:查看npm安装的版本 当前安装目录默认的包 在node.js的目录下创建两个文件夹 这样路径就被修改成功了 cnpm npm in ...
- jemter 90%line的解释
假如: 有10个数: 1.2.3.4.5.6.7.8.9.10 按由大到小将其排列. 求它的第90%百分位,也就是第9个数刚好是9 ,那么他的90%Line 就是9 . 另一组数: 2.2.1. ...
- Crontab的格式说明
第1列分钟1-59第2列小时1-23(0表示子夜)第3列日1-31第4列月1-12第5列星期0-6(0表示星期天)第6列要运行的命令 下面是crontab的格式:分 时 日 月 星期 要运行的命令 这 ...
- ProbCog mlnlearn的探索
which mlnlearn: /home/panjinbo/Desktop/MLN/probcog/Toolbox/ProbCog-1.1/apps/mlnlearn ==> /home/pa ...
- 在win10上使用premake工具和vs2017编译运行Box2D源码和Testbed
1.从github上下载Box2D源码的zip包 2.解压缩zip包 3.从premake网站下载premake5工具,解压后得到premake5.exe 4.将premake5.exe拷贝到Box2 ...