简体   繁体   English

一定时间后杀死pthread

[英]pthread kill after a certain time duration

I'm working with VS2005 C++ and I'm BRAND NEW to it. 我正在使用VS2005 C ++,对它来说是全新的。

I have a loop that creates several threads using the following statement - 我有一个循环,使用以下语句创建多个线程-

rc = pthread_create(&thread[i], NULL, &Function, (void *)threadID);

I want to terminate all threads after a certain duration (say 5 minutes). 我想在某个持续时间(例如5分钟)后终止所有线程。 How do I have a timer that kills all threads after this duration? 我如何拥有一个在此持续时间后杀死所有线程的计时器?

I believe, something like that: 我相信,是这样的:

pthread_t tid[thread_num] = {};
for(size_t i = 0 ; i < threads_num; ++i){
    pthread_create(&tid[i], NULL, func, arg);
}
sleep(300);
for(size_t i = 0 ; i < threads_num; ++i){
    pthread_cancel(tid[i]);
}

Anyway, I don't know how to use pthread under VS 2005 =) 无论如何,我不知道如何在VS 2005下使用pthread =)

Yes, one important point - to be killed, your threads should reach a cancellation point. 是的,重要的一点-要杀死,您的线程应该到达取消点。 Some POSIX functions could be cancellation points, but it's better to call pthread_testcancel() in your thread (as they work about minutes, I think it's some kind of loop, just check that every iteration) 某些POSIX函数可能是取消点,但最好在线程中调用pthread_testcancel()(由于它们工作约数分钟,我认为这是某种循环,只需检查每次迭代即可)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM