3

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.

  • I think you are in a wrong way. use ++c instead of c++ first. And please clarify your question. – Sam Mokari Mar 23 '17 at 04:50
  • @SamMokari thank you for your attention. why I must use ++c?!! I need a way to kill a thread without the process be killed. because I want to use other threads of the process, however anyone of them be killed. – majid zolfaghari Mar 23 '17 at 05:04
  • 1
    ++c would be about as helpful meeting your goal as compiling with ++g. – user4581301 Mar 23 '17 at 05:12
  • Anyway, no. Different kind of exception. You can use a signal handler on the FPE signal, but you won't get the result you want. You aren't trying to use this to end a thread, are you? – user4581301 Mar 23 '17 at 05:20
  • @user4581301 no, I want to use other threads of the process, however anyone of its threads be killed. so I want a way to process won't be killed while any of its threads are killed. – majid zolfaghari Mar 23 '17 at 05:51
  • Since you are using pthreads you are probably targeting Linux. On Linux the thread that was running when the signal is raised handles the signal, but I'm not sure if `pthread_self` is safe to call in a signal handler let alone `pthread_kill`. `pthread_cancel` is kinder and less likely to leave the program in a bad state than `pthread_kill`, but isn't that useful unless your thread has a decent cancellation point. If you're going to check "should I cancel?" every iteration, you might as well save yourself a lot of trouble, test if you're about to divide by zero and politely return on true. – user4581301 Mar 23 '17 at 17:42

2 Answers2

2

I solved my problem by ignoring the FPE signal. Here is my new program:

#include <unistd.h>
#include <pthread.h>
#include <iostream>
#include <signal.h>
using namespace std;

int a = 0;

void sig_func(int sig)
{
  //some code we want execute when signal recieves, or nothing.
}

void *func (void *arg)
{
  int c = a++;
  cout << "start thread " << c << endl;
  if (c == 1)
  {
    int d = 0;
    d = 10 / d;
  }
  cout << "end thread " << c << endl;
}

int main ()
{
  pthread_t tid1, tid2, tid3;
  signal(SIGFPE,sig_func);
  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;
}

and its output is like this:

start thread 0
end thread 0
start thread 1
start thread 2
end thread 2

that means when thread1 is killed, whole the process won't be killed and next threads could run their functions.

-1

You may want to use synchronization mechanisms such as semaphores (using sem_wait(), sem_post()) which is shared among multiple threads to induce delay.

Alternate way is to use while(1) and sleep() inside func(), but consumes CPU cycles when awake.

vivekn
  • 35
  • 6