当前位置: 技术问答>linux和unix
unix下的定时器问题,在线等待!跟贴有分!
来源: 互联网 发布时间:2015-04-16
本文导语: 我在solaris下用C++设计一个多线程的程序,这些线程必须每隔一段时间激发一次。而且不同线程唤醒的时间间隔可能不一样,如,每隔60秒激发一次线程1,每隔300秒激发一次线程2,怎么用定时器去实现这个功能?各位...
我在solaris下用C++设计一个多线程的程序,这些线程必须每隔一段时间激发一次。而且不同线程唤醒的时间间隔可能不一样,如,每隔60秒激发一次线程1,每隔300秒激发一次线程2,怎么用定时器去实现这个功能?各位兄弟姐妹帮帮忙阿,最好有源码,跟贴着有分,分数不够再补!
|
sem_t sem[N];
work_threadN
{
sem_wait(sem(N))
}
int time_limit[N] = {...};
time_thread
{
int n[N] = {0}, i;
while (1)
{
sleep(1);
for (i = 0; i time_limit[i])
{
n[i] = 0;
sem_post(sem[i]);
}
}
}
}
work_threadN
{
sem_wait(sem(N))
}
int time_limit[N] = {...};
time_thread
{
int n[N] = {0}, i;
while (1)
{
sleep(1);
for (i = 0; i time_limit[i])
{
n[i] = 0;
sem_post(sem[i]);
}
}
}
}
|
/*******************************************************************
*
* compile with -D_POSIX_PTHREAD_SEMANTICS switch;
* required by sigwait()
*
* sigint thread handles delivery of signal. uses sigwait() to wait
* for SIGINT signal.
*
********************************************************************/
#include
#include
#include
#include
#include
#include
#include
#include
#include
static void *threadTwo(void *);
static void *threadThree(void *);
static void *sigint(void *);
sigset_t signalSet;
sem_t sem1;
sem_t sem2;
int
main(void)
{
pthread_t t;
pthread_t t2;
pthread_t t3;
thr_setconcurrency(3);
sigfillset ( &signalSet );
/*
* Block signals in initial thread. New threads will
* inherit this signal mask.
*/
pthread_sigmask ( SIG_BLOCK, &signalSet, NULL );
printf("Creating threadsn");
/* POSIX thread create arguments:
* thr_id, attr, strt_func, arg
*/
sem_init( &sem1 , 0 , 0 );
sem_init( &sem2 , 0 , 0 );
pthread_create(&t, NULL, sigint, NULL);
pthread_create(&t2, NULL, threadTwo, NULL);
pthread_create(&t3, NULL, threadThree, NULL);
printf("##################n");
printf("press CTRL-C to deliver SIGINT to exit.n");
printf("##################n");
pthread_join( t , NULL );
pthread_join( t2 , NULL );
pthread_join( t3 , NULL );
//thr_exit((void *)0);
}
static void *
threadTwo(void *arg)
{
printf("hello world, from threadTwo [tid: %d]n",
pthread_self());
while(1){
sem_wait(&sem2);
printf("Thread two running :%d - %dn",
pthread_self() , time(NULL));
fflush(stdout);
}
}
static void *
threadThree(void *arg)
{
printf("hello world, from threadThree [tid: %d]n",
pthread_self());
while(1){
sem_wait(&sem1);
printf("Thread three running :%d - %dn",
pthread_self() , time(NULL));
fflush(stdout);
}
}
void *
sigint(void *arg)
{
int sig;
int err;
printf("thread counter [tid: %d] awaiting SIGALRMn",
pthread_self());
sigaddset( &signalSet , SIGALRM );
sigaddset( &signalSet , SIGINT );
/*****************************************************
*Attention:you maybe need to modify the code below to be fit for your requestment.
*****************************************************/
while(1){
alarm(5);
err = sigwait ( &signalSet, &sig );
if(sig==SIGINT)break;
sem_post(&sem1);
alarm(5);
err = sigwait ( &signalSet, &sig );
if(sig==SIGINT)break;
sem_post(&sem2);
}
printf("Counter thread exit.n");
exit(0);
}
原理就是先屏蔽所有信号,然后在专用信号处理线程中用sigwait等待你想要的信号。
*
* compile with -D_POSIX_PTHREAD_SEMANTICS switch;
* required by sigwait()
*
* sigint thread handles delivery of signal. uses sigwait() to wait
* for SIGINT signal.
*
********************************************************************/
#include
#include
#include
#include
#include
#include
#include
#include
#include
static void *threadTwo(void *);
static void *threadThree(void *);
static void *sigint(void *);
sigset_t signalSet;
sem_t sem1;
sem_t sem2;
int
main(void)
{
pthread_t t;
pthread_t t2;
pthread_t t3;
thr_setconcurrency(3);
sigfillset ( &signalSet );
/*
* Block signals in initial thread. New threads will
* inherit this signal mask.
*/
pthread_sigmask ( SIG_BLOCK, &signalSet, NULL );
printf("Creating threadsn");
/* POSIX thread create arguments:
* thr_id, attr, strt_func, arg
*/
sem_init( &sem1 , 0 , 0 );
sem_init( &sem2 , 0 , 0 );
pthread_create(&t, NULL, sigint, NULL);
pthread_create(&t2, NULL, threadTwo, NULL);
pthread_create(&t3, NULL, threadThree, NULL);
printf("##################n");
printf("press CTRL-C to deliver SIGINT to exit.n");
printf("##################n");
pthread_join( t , NULL );
pthread_join( t2 , NULL );
pthread_join( t3 , NULL );
//thr_exit((void *)0);
}
static void *
threadTwo(void *arg)
{
printf("hello world, from threadTwo [tid: %d]n",
pthread_self());
while(1){
sem_wait(&sem2);
printf("Thread two running :%d - %dn",
pthread_self() , time(NULL));
fflush(stdout);
}
}
static void *
threadThree(void *arg)
{
printf("hello world, from threadThree [tid: %d]n",
pthread_self());
while(1){
sem_wait(&sem1);
printf("Thread three running :%d - %dn",
pthread_self() , time(NULL));
fflush(stdout);
}
}
void *
sigint(void *arg)
{
int sig;
int err;
printf("thread counter [tid: %d] awaiting SIGALRMn",
pthread_self());
sigaddset( &signalSet , SIGALRM );
sigaddset( &signalSet , SIGINT );
/*****************************************************
*Attention:you maybe need to modify the code below to be fit for your requestment.
*****************************************************/
while(1){
alarm(5);
err = sigwait ( &signalSet, &sig );
if(sig==SIGINT)break;
sem_post(&sem1);
alarm(5);
err = sigwait ( &signalSet, &sig );
if(sig==SIGINT)break;
sem_post(&sem2);
}
printf("Counter thread exit.n");
exit(0);
}
原理就是先屏蔽所有信号,然后在专用信号处理线程中用sigwait等待你想要的信号。