[英]creating time specific threads
I have written a sample program to implement array of threads.There are two thread functions. 我编写了一个示例程序来实现线程数组。有两个线程函数。 Is there any way to define a fixed value of time (in seconds) after which all the threads will automatically stop? 有什么方法可以定义一个固定的时间值 (以秒为单位),之后所有线程将自动停止?
Sample program: 示例程序:
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
void * threadFunc1(void * arg)
{
int id = *((int *) arg);
printf("Inside threadfunc2 for thread %d\n",id)
while(1);
}
void * threadFunc2(void * arg)
{
int i= *((int *)arg);
printf("Inside threadfunc2 for thread %d\n",i)
while(1);
}
int main(void)
{
pthread_t thread[10];
for(int i=0;i<10;i++)
{
pthread_create(&thread[i],NULL,threadFunc1,(void*)&i );
pthread_create(&thread[i],NULL,threadFunc,(void*)&i );
}
for (i=0;i<total;i++)
{
pthread_join(thread[i],NULL);
}
return 0;
}
Instead of waiting for the threads with pthread_join
you could put your main
thread to sleep, eg with nanosleep
. 不用等待带有pthread_join
的线程,您可以让main
线程进入睡眠状态,例如,使用nanosleep
。 If you then quit your main
without joining, your whole process will be killed. 如果您随后在不加入的情况下退出了main
,则整个过程将被杀死。
No, there is not. 不,那里没有。 Threads do not 'automatically stop' 线程不会“自动停止”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.