[英]Linux, C: terminate multple threads after some seconds (timer?)
Linux,C。我创建了多个线程来运行工作负载,我希望在指定的秒/超时后发出停止/终止这些线程的信号。 我如何用C实现这个?
void *do_function(void *ptr)
{
//calculating, dothe workload here;
}
int run(struct calculate_node *node)
{
pthread_t threads[MAX_NUM_THREADS];
for (t = 0; t < node->max_threads; t++) {
rc = pthread_create(&threads[t], NULL, do_function, (void*)node);
if(rc) return -1;
}
//how do I create timer here to fire to signal those threads to exit after specified seconds?
for (t = 0; t < node->max_threads; t++) {
pthread_join(threads[t], NULL);
}
free(threads);
}
谢谢!
不确定是否有一种可移植的方式来创建一个计时器事件,但如果main
没有其他任何事情要做,它可能只是简单地调用sleep
来浪费时间。
至于信令线程,您有两种选择:协作终止或非协作终止。 通过协作终止,线程必须定期检查标志以查看它是否应该终止。 使用非协作终止,您可以调用pthread_cancel
来结束线程。 (有关可以用于正常结束线程的其他函数的信息,请参阅pthread_cancel
的手册页。)
我发现合作终止更容易实现。 这是一个例子:
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
static int QuitFlag = 0;
static pthread_mutex_t QuitMutex = PTHREAD_MUTEX_INITIALIZER;
void setQuitFlag( void )
{
pthread_mutex_lock( &QuitMutex );
QuitFlag = 1;
pthread_mutex_unlock( &QuitMutex );
}
int shouldQuit( void )
{
int temp;
pthread_mutex_lock( &QuitMutex );
temp = QuitFlag;
pthread_mutex_unlock( &QuitMutex );
return temp;
}
void *somefunc( void *arg )
{
while ( !shouldQuit() )
{
fprintf( stderr, "still running...\n");
sleep( 2 );
}
fprintf( stderr, "quitting now...\n" );
return( NULL );
}
int main( void )
{
pthread_t threadID;
if ( pthread_create( &threadID, NULL, somefunc, NULL) != 0 )
{
perror( "create" );
return 1;
}
sleep( 5 );
setQuitFlag();
pthread_join( threadID, NULL );
fprintf( stderr, "end of main\n" );
}
首先,您需要创建并开始时间。 在下面的示例中,计时器将调用“stop”函数,该函数将负责中断您的线程。 在某种程度上,它是最简单的选择之一。
void *notify_stop(void *); // Prototype
int run(struct calculate_node *node)
{
...
struct sigevent sev;
timer_t timer;
struct itimerspec tspec = { 0 };
sev.sigev_notify = SIGEV_THREAD; // type of timer event
sev.sigev_notify_function = notify_stop; // call function
timer_create(CLOCK_MONOTONIC, &sevent, &timer);
tspec.it_interval.tv_sec = 5; // set time interval
tspec.it_interval.tv_nsec = 0;
tspec.it_value.tv_sec = 0;
tspec.it_value.tv_nsec = 0;
timer_set(timer, &tspec, NULL); // start timer
....
}
现在,您需要决定如何停止线程。 一种方法是使用一些共享变量来指示退出条件:
volatile bool do_stop = false;
void *do_function(void *ptr)
{
while (!do_stop)
{
//calculating, dothe workload here;
}
}
void *notify_stop(void *)
{
do_stop = true; // just set the variable to let other threads
// read it and stop
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.