资源简介
linux c 开发中多线程的典型实例,可在linux平台直接运行,通过实践熟悉理解多线程工作原理。
代码片段和文件信息
#include
#include
#include “errors.h“
typedef struct alarm_tag
{
struct alarm_tag *link;
int seconds;
time_t time;
char message[64];
}alarm_t;
pthread_mutex_t alarm_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t alarm_cond = PTHREAD_COND_INITIALIZER;
alarm_t *alarm_list = NULL;
time_t current_alarm = 0;
void alarm_insert(alarm_t *alarm)
{
int status;
alarm_t **last*next;
last = &alarm_list;
next = *last;
while(next != NULL)
{
if(next->time >= alarm->time)
{
alarm->link = next;
*last = alarm;
break;
}
last = &next->link;
next = next->link;
}
if(next == NULL)
{
*last = alarm;
alarm->link = NULL;
}
#ifdef DEBUG
printf(“[list: “);
for(next = alarm_list;next != NULL;next = next->link)
printf(“%d(%d)[\“%s\“]“next->timenext->time-time(NULL)next->message);
printf(“]\n“);
#endif
if(current_alarm == 0 || alarm->time < current_alarm)
{
current_alarm = alarm->time;
status = pthread_cond_signal(&alarm_cond);
if(status != 0)
err_abort(status“Signal cond“);
}
}
void *alarm_thread(void *arg)
{
alarm_t *alarm;
struct timespec cond_time;
time_t now;
int statusexpired;
status = pthread_mutex_lock(&alarm_mutex);
if(status != 0)
err_abort(status“Lock mutex“);
while(1)
{
current_alarm = 0;
while(alarm_list == NULL)
{
status = pthread_cond_wait(&alarm_cond&alarm_mutex);
if(status != 0)
err_abort(status“Wait on cond“);
}
alarm = alarm_list;
alarm_list = alarm->link;
now = time(NULL);
expired = 0;
if(alarm->time > now)
{
#ifdef DEBUG
printf(“[waiting: %d(%d)\“%s\“]\n“alarm->timealarm->time-time(NULL)alarm->message);
#endif
cond_time.tv_sec = alarm->time;
cond_time.tv_nsec = 0;
current_alarm = alarm->time;
while(current_alarm == alarm->time)
{
status = pthread_cond_timedwait(&alarm_cond&alarm_mutex&cond_time);
if(status == ETIMEDOUT)
{
expired = 1;
break;
}
if(status != 0)
err_abort(status“Cond timedwait“);
}
if(!expired)
alarm_insert(alarm);
}
else
expired = 1;
if(expired)
{
printf(“(%d) %s\n“alarm->secondsalarm->message);
free(alarm);
}
}
}
int main(int argcchar *argv[])
{
int status;
char line[128];
alarm_t *alarm;
pthread_t thread;
status = pthread_create(&threadNULLalarm_threadNULL);
if(status != 0)
err_abort(status“Create alarm thread“);
while(1)
{
printf(“Alarm> “);
if(fgets(linesizeof(line)stdin) == NULL)
exit(0);
if(strlen(line) <= 1)
continue;
alarm = (alarm_t *)malloc(sizeof(alarm_t));
if(alarm == NULL)
{
errno_abort(“Allocate alarm“);
}
if(sscanf(line“%d %64[^\n]“&alarm->secondsalarm->message) < 2)
{
fprintf(stderr“Bad command\n“);
free(alarm);
}
else
{
status = pthread_mutex_lock(&alarm_mutex);
if(status != 0)
err_abort(
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2012-08-01 15:23 linux c 多线程\
文件 3215 2012-08-01 15:22 linux c 多线程\alarm_cond.c
文件 469 2012-08-01 15:23 linux c 多线程\errors.h
相关资源
- 巴枪/数据采集器CASIO DT900/930/940中文操
- TextCNN在文本分类的应用.pptx
- colossal cave adventure
- ISO14230 标准协议
- 双向全桥DC-DC双重移相控制 28335程序
- vcf解析工具--安卓通讯录备份文件解析
- SecretView星号密码查看器
- 基于linux和gtk的mp3播放器
- 知名的斯坦福兔子的三维点云数据,
- VC树形控件 自己封装的树形控件拓展
- GPStrackingGeneral1009A.slx
- 基于DSP_TMS320C5402的FIR数字滤波器设计
- UC浏览器缓存视频文件合并工具
- 手机音频流程包括mic的偏置电路及其
- 计算机组成原理期末考试试题及答案
- Hawths tools很好用的ArcGIS取样地小工具
- xcode 简易计算器
- PageRank算法的mapreduce实现
- AUTO_CAD.rar
- Excel md5加密宏
- PCIEX16 金手指allegroPCB封装
- Centos7搭建ELK+filebeat.docx
- EXCEL实现投入产出表RAS法.docx
- 串口转发工具配合虚拟串口即可实现
- 文档管理系统支持全文索引源码
- ATCFWCHG.COM_CF卡 本地磁盘模式转换工具
- 雷凌rt2870 802.11n USB Wireless LAN Card驱动
- MX OPC Server 6.10 安装包链接网盘
- 变频恒压供水一拖二PLC程序解析
- vivado15.2激活license
评论
共有 条评论