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系统下,启动一个新的进程必须分配给它独立的地址空间,建立众多的数据表来维 ...
随机推荐
- geth 新建账户
使用geth的account命令管理账户,例如创建新账户.更新账户密码.查询账户等: geth account <command> [options...] [arguments...] ...
- Oracle 11g win32位 window7下安装教程
1.首先是去http://www.oracle.com/technetwork/database/enterprise-edition/downloads/index.html官网下载oracle11 ...
- WIN7+Qt5.2.0连接oracle数据库的oci驱动的编译
一.前提安装 1.需要安装QT5.2.0,本介绍安装的是qt-windows-opensource-5.2.0-mingw48_opengl-x86-offline.exe: 本文安装目录:c:\Qt ...
- startuml 3 windows 破解教程
1.startuml 的下载地址:http://staruml.io/download 2.下载安装之后,安装node 服务.
- Memcached与Memcache区别
在写这篇文章之前一直对memcache .memcached模糊,相差一个字母,特此总结下: Memcache是什么? Memcache是一个自由和开放源代码.高性能.分配的内存对象缓存系统.用于加速 ...
- SQL-W3School-高级:SQL NULL 函数
ylbtech-SQL-W3School-高级:SQL NULL 函数 1.返回顶部 1. SQL ISNULL().NVL().IFNULL() 和 COALESCE() 函数 请看下面的 &quo ...
- npm install --save react-native-device-info报错
报错截图如下: 把react-native-device-info删了再添加还是这样,后面使用yarn就成功了. 然后发现会报错,这里估计是Xcode 9的bug,你只要确保以下显示就可以了.没有自己 ...
- 如何屏蔽掉烦人的www.google-analytics.com
有时候在开发的网站项目中会加载谷歌分析的js,并且加载的非常慢导致浏览器一直在转圈圈. 按下面的方法可屏蔽掉烦人的www.google-analytics.com 现在想只有屏蔽掉google-a ...
- 02.01Linux中软件的安装、环境搭建
图1 图2 图3 redis安装 图4 =====================linux下的软件的安装====================安装方式:Yum/rpm/源码安装yum:通过分析rp ...
- Odoo 13 released..
origin https://medium.com/@jc_57445/odoo-13-is-fantastic-f2b421696b49 Most striking changes The most ...