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);
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]);
}
Anyway, I don't know how to use pthread under VS 2005 =)
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)