I have write a program with 3 threads using pthread in c++, and I want to keep alive the process while the thread 1 is killed. here is my program:
#include <unistd.h>
#include <pthread.h>
#include <iostream>
using namespace std;
int a = 0;
void *func (void *arg)
{
int c = a++;
cout << "start thread " << c << endl;
if (c == 1) //I wrote this to thread1 crashes and be killed.
{
int d = 0;
d = 10 / d;
}
cout << "end thread " << c << endl;
}
int main ()
{
pthread_t tid1, tid2, tid3;
pthread_create (&tid1, 0, func, 0);
sleep (1);
pthread_create (&tid2, 0, func, 0);
sleep (1);
pthread_create (&tid3, 0, func, 0);
sleep (10);
return 0;
}
when I run it with "g++ a.cpp -lpthread", the output is like this:
start thread 0
end thread 0
start thread 1
Floating point exception (core dumped)
that means when thread1 is killed, whole the process will be killed. Is there any way to keep alive the process while one of its threads is killed? I need that because I want to use other threads of the process, however anyone of the threads be killed.
note that I don't want to use exception handling to avoid killing the thread.