[英]Linux, C: terminate multple threads after some seconds (timer?)
Linux, C. I created multiple threads to run workloads, and I want to signal those threads to stop/terminate after a specified seconds/time out. Linux,C。我创建了多个线程来运行工作负载,我希望在指定的秒/超时后发出停止/终止这些线程的信号。 How do I implement this by 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);
}
thanks! 谢谢!
Not sure if there's a portable way to create a timer event, but if main
doesn't have anything else to do, it could simply call sleep
to waste time. 不确定是否有一种可移植的方式来创建一个计时器事件,但如果
main
没有其他任何事情要做,它可能只是简单地调用sleep
来浪费时间。
As for signaling the threads, you have two choices: cooperative termination or non-cooperative termination. 至于信令线程,您有两种选择:协作终止或非协作终止。 With cooperative termination, the thread must periodically check a flag to see if it's supposed to terminate.
通过协作终止,线程必须定期检查标志以查看它是否应该终止。 With non-cooperative termination, you call
pthread_cancel
to end the thread. 使用非协作终止,您可以调用
pthread_cancel
来结束线程。 (See the man page for pthread_cancel
for information about additional functions that can be used to gracefully end the thread.) (有关可以用于正常结束线程的其他函数的信息,请参阅
pthread_cancel
的手册页。)
I find cooperative termination easier to implement. 我发现合作终止更容易实现。 Here's an example:
这是一个例子:
#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" );
}
First, you need to create and start the time. 首先,您需要创建并开始时间。 In the example below the timer will invoke a "stop" function, that will be responsible for interrupting your threads.
在下面的示例中,计时器将调用“stop”函数,该函数将负责中断您的线程。 In a way, it is one of the simplest options.
在某种程度上,它是最简单的选择之一。
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
....
}
Now, you need to decide how you want to stop your threads. 现在,您需要决定如何停止线程。 One way is to use some shared variable to indicate the exit condition:
一种方法是使用一些共享变量来指示退出条件:
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.