【发布时间】:2015-08-18 18:20:04
【问题描述】:
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);
}
谢谢!
【问题讨论】:
标签: c linux multithreading timer