Joel Yliluoma
2007-07-28 10:55:40 UTC
I am trying to terminate threads in a C++ program using POSIX threads.
I use pthread_cancel(), and the thread is configured with
PTHREAD_CANCEL_ENABLE and PTHREAD_CANCEL_ASYNCHRONOUS.
However, when I run this test program, it crashes randomly, saying
"terminate called without an active exception".
How do I fix it so that it does not crash?
I want to use PTHREAD_CANCEL_ASYNCHRONOUS, because the thread will
be doing a loop of cpu-intensive stuff and I don't want to insert
pthread_testcancel() in the loop.
If I replace the std::vector<> with a custom-built class that has
constructors and destructors, it will still crash. If I use a plain
old C array, it does not crash. However, in the actual program where
I need this solution, there are C++ objects and it cannot be easily
avoided.
#include <vector>
#include <stdio.h>
#include <pthread.h>
void* Runner(void*p)
{
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, 0);
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, 0);
const unsigned n=40000;
std::vector<int> temp(n);
for(unsigned c=0; c<5; ++c)
for(unsigned a=0; a<n; ++a)
temp[a]=n-a;
fprintf(stderr, "- Got %u\n", n);
return NULL;
}
int main(void)
{
const unsigned n = 5;
pthread_t threads[n];
for(;;)
{
fprintf(stderr, "Creating %u threads\n", n);
for(unsigned a=0; a<n; ++a)
pthread_create(&threads[a], NULL, Runner, NULL);
unsigned c = 2;
fprintf(stderr, "Cancelling thread %u\n", c);
pthread_cancel(threads[c]);
fprintf(stderr, "Joining %u threads\n", n);
for(unsigned a=0; a<n; ++a)
pthread_join(threads[a], NULL);
fprintf(stderr, "** Everything ok\n");
}
}
I use pthread_cancel(), and the thread is configured with
PTHREAD_CANCEL_ENABLE and PTHREAD_CANCEL_ASYNCHRONOUS.
However, when I run this test program, it crashes randomly, saying
"terminate called without an active exception".
How do I fix it so that it does not crash?
I want to use PTHREAD_CANCEL_ASYNCHRONOUS, because the thread will
be doing a loop of cpu-intensive stuff and I don't want to insert
pthread_testcancel() in the loop.
If I replace the std::vector<> with a custom-built class that has
constructors and destructors, it will still crash. If I use a plain
old C array, it does not crash. However, in the actual program where
I need this solution, there are C++ objects and it cannot be easily
avoided.
#include <vector>
#include <stdio.h>
#include <pthread.h>
void* Runner(void*p)
{
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, 0);
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, 0);
const unsigned n=40000;
std::vector<int> temp(n);
for(unsigned c=0; c<5; ++c)
for(unsigned a=0; a<n; ++a)
temp[a]=n-a;
fprintf(stderr, "- Got %u\n", n);
return NULL;
}
int main(void)
{
const unsigned n = 5;
pthread_t threads[n];
for(;;)
{
fprintf(stderr, "Creating %u threads\n", n);
for(unsigned a=0; a<n; ++a)
pthread_create(&threads[a], NULL, Runner, NULL);
unsigned c = 2;
fprintf(stderr, "Cancelling thread %u\n", c);
pthread_cancel(threads[c]);
fprintf(stderr, "Joining %u threads\n", n);
for(unsigned a=0; a<n; ++a)
pthread_join(threads[a], NULL);
fprintf(stderr, "** Everything ok\n");
}
}
--
Joel Yliluoma - http://bisqwit.iki.fi/
: comprehension = 1 / (2 ^ precision)
Joel Yliluoma - http://bisqwit.iki.fi/
: comprehension = 1 / (2 ^ precision)