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系统下,启动一个新的进程必须分配给它独立的地址空间,建立众多的数据表来维 ...
随机推荐
- mac下iterm配色、半透明与样式设置
mac下iterm配色.半透明与样式设置 * {display: table-row!important} .MJXp-surd {vertical-align: top} .MJXp-surd &g ...
- linux下如何设置日志级别?
答: echo $level > /proc/sys/kernel/printk 如: echo 8 4 1 7 > /proc/sys/kernel/printk
- linux里面源码安装imagemagick库
在搞树莓派的时候想搞一下树莓派中摄像头获取图像之后传给安卓,安卓进行展示. 恰好用到了imagemagick这个库,我就像正常一样进行安装,sudo apt-get install Imagick 但 ...
- C之指针的加法
#include<stdio.h> #include<stdlib.h> main() { //char arr [] = {'H','e','l','l','o'}; int ...
- vue 高德地图
index.html <link rel="stylesheet" href="http://cache.amap.com/lbs/static/main1119. ...
- 003-多线程-JUC线程池-几种特殊的ThreadPoolExecutor【newFixedThreadPool、newCachedThreadPool、newSingleThreadExecutor、newScheduledThreadPool】
一.概述 在java doc中,并不提倡我们直接使用ThreadPoolExecutor,而是使用Executors类中提供的几个静态方法来创建线程池: 以下方法是Executors下的静态方法,Ex ...
- haproxy报错解决
.有一次访问出现 错误 http://192.168.0.200:10080 haproxy service unavailable no server is avaible to handle th ...
- WordPress的默认循环
WordPress的默认循环是相对我们上一篇的WordPress自定义循环而言的,默认循环是根据链接结构的来查询数据的. 我们知道WordPress模板文件就是根据文件名来找寻模板的,这里的链接结构也 ...
- 客户端热更新框架之UI热更框架设计(下)
上一篇笔者介绍了关于什么是热更新,为什么需要热更新的技术文章.本篇就专门针对UI框架的热更新功能实现部分展开讨论,讨论的重点是热更新如何与UI框架进行结合? 现在笔者把设计“UI热更新框架”的整体设计 ...
- 前端HTML介绍,标签介绍,基础选择器,CSS引入方法
1. HTML 1.1 前端: 所有用户能看到的界面网页.pc端的应用exe.移动端应用app.微信小程序.手环的时间界面html5为基础的前端:网页.app.微信小程序 1.2 前端三剑客: 1.h ...