一定时间后杀死pthread [英] pthread kill after a certain time duration

查看:76
本文介绍了一定时间后杀死pthread的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用VS2005 C ++,并且对此感到陌生.

I'm working with VS2005 C++ and I'm BRAND NEW to it.

我有一个循环,它使用以下语句创建多个线程-

I have a loop that creates several threads using the following statement -

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

我想在某个持续时间(例如5分钟)后终止所有线程.我如何拥有一个在此持续时间后杀死所有线程的计时器?

I want to terminate all threads after a certain duration (say 5 minutes). 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]);
}

无论如何,我不知道如何在VS 2005下使用pthread =)

Anyway, I don't know how to use pthread under VS 2005 =)

是的,重要的一点-要杀死,您的线程应该到达取消点.某些POSIX函数可能是取消点,但最好在线程中调用pthread_testcancel()(因为它们工作约数分钟,所以我认为这是某种循环,只需检查每次迭代即可)

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)

这篇关于一定时间后杀死pthread的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆