| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1500 人关注过本帖
标题:用多线程写一个alarm,
只看楼主 加入收藏
花脸
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:9
帖 子:788
专家分:907
注 册:2017-1-4
结帖率:95.37%
收藏
已结贴  问题点数:20 回复次数:3 
用多线程写一个alarm,
#include <iostream>
#include <ctime>
#include <cstdio>
#include "errors.h"
#define DEBUG
using namespace std;

typedef struct alarm_tag
{
    struct alarm_tag *link;
    int seconds;
    time_t time;        //seconds from epoch(公元,纪年)
    char message[64];
}alarm_t;

pthread_mutex_t alarm_mutex=PTHREAD_MUTEX_INITIALIZER;
alarm_t *alarm_list=NULL;

//The alarm thread's start routine.
void *alarm_thread(void *arg)
{
    alarm_t *alarm;
    int sleep_time;
    time_t now;
    int status;
        
    while(true)
    {
        status=pthread_mutex_lock(&alarm_mutex);//下面要对数据进行操作,加锁
        if(status!=0)
            err_abort(status,"Lock mutex");
    //    cout<<"thread 2:"<<alarm_list<<endl;
   
        alarm=alarm_list;
        if(alarm==NULL)//此时无要处理的message
            sleep_time=1;
        else        //alarm_lists是aralm_thread与main的共享数据
        {
            alarm_list=alarm->link;
            now=time(NULL);
            if(alarm->time<=now)
                sleep_time=0;
            else
                sleep_time=alarm->time-now;
               
            #ifdef DEBUG
                cout<<"waiting alarm->time:"<<alarm->time<<
                     "    sleep_time:"<<sleep_time<<
                     "     alarm->message:"<<alarm->message<<endl;
            #endif
        }
        
        status=pthread_mutex_unlock(&alarm_mutex);//操作完毕,解锁
        if(status!=0)
            err_abort(status,"Unlock mutex");
   
        if(sleep_time>0)
            sleep(sleep_time);
        else
            sched_yield();
        
        if(alarm!=NULL)
        {
            cout<<"aralm not empty! alarm->seconds:"<<alarm->seconds<<
                  "  alarm->message:"<<alarm->message<<endl;
            free(alarm);
            alarm=NULL;
        }
    }   
}

int main()
{
    int status;
    char line[128];
    alarm_t *alarm,**last,*next;
    pthread_t thread;
   
    status=pthread_create(&thread,NULL,alarm_thread,NULL);
    if(status!=0)
        err_abort(status,"Create alarm thread");
   
    while(true)
    {
        if(fgets(line,sizeof(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->seconds,alarm->message)<2)//^忽略\n,不读入字符串
        {
            fprintf(stderr,"Bad command\n");
            free(alarm);
            alarm=NULL;
        }
        else
        {
            status=pthread_mutex_lock(&alarm_mutex);//要操作数据,加锁
            if(status!=0)
                err_abort(status,"Lock mutex");
            
            alarm->time=time(NULL)+alarm->seconds;
        //    cout<<"thread 1:"<<alarm_list<<endl;
            
            last=&alarm_list;        //last存放的 alarm_list的地址,alarm_list的内容为空
            next=*last;                //next指向当前结点也就是alarm_list的内容,第一次内容为空
            //cout<<"next:"<<next<<endl;
            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;//last改变相当于aralm_list改变
                alarm->link=NULL;
            }
        
            #ifdef DEBUG
                cout<<"List: ";
                for(next=alarm_list;next!=NULL;next=next->link)
                    cout<<"next->time:"<<next->time<<
                        "    next->time-time(NULL):"<<next->time-time(NULL)<<
                            "   next->message:"<<next->message<<endl;
            #endif
            
            status=pthread_mutex_unlock(&alarm_mutex);//操作完毕,解锁  
            if(status!=0)
                err_abort(status,"Unlock mutex");
        }
    }
    return 0;
}
搜索更多相关主题的帖子: time message NULL status next 
2018-06-27 22:58
幻紫灵心
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:山咔咔里面
等 级:贵宾
威 望:53
帖 子:396
专家分:2640
注 册:2018-3-30
收藏
得分:10 

saber,别哭.
2018-06-27 23:23
星泪成寒
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:6
帖 子:77
专家分:549
注 册:2013-5-19
收藏
得分:10 
程序代码:
#include <signal.h> /* only timer_create need this header */
#include <time.h>

int timer_create(clockid_t clockid, struct sigevent *restrict evp, timer_t *restrict timerid);
int timer_delete(timer_t timerid);

int timer_gettime(timer_t timerid, struct itimerspec *value);
int timer_settime(timer_t timerid, int flags, const struct itimerspec *restrict new_value, struct itimerspec *restrict old_value);

int timer_getoverrun(timer_t timerid); 

2018-06-28 13:47
花脸
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:9
帖 子:788
专家分:907
注 册:2017-1-4
收藏
得分:0 
回复 3楼 星泪成寒
???
2018-06-28 15:55
快速回复:用多线程写一个alarm,
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.041529 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved