如何在使用C+中的pthread终止进程的一个线程时保持进程的活动状态+;? 我已经用C++中的p螺纹编写了3个线程的程序,我希望在线程1被杀死的时候保持进程。这是我的节目: #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; }

如何在使用C+中的pthread终止进程的一个线程时保持进程的活动状态+;? 我已经用C++中的p螺纹编写了3个线程的程序,我希望在线程1被杀死的时候保持进程。这是我的节目: #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; },c++,multithreading,pthreads,C++,Multithreading,Pthreads,这意味着当thread1被终止时,整个进程都将被终止。当进程的一个线程被终止时,有没有办法使进程保持活动状态?我需要它,因为我想使用进程的其他线程,但是任何线程都会被杀死。 请注意,我不想使用异常处理来避免终止线程。您可能希望使用同步机制,例如信号量(使用sem_wait()、sem_post()),它在多个线程之间共享以引起延迟 另一种方法是在func()中使用while(1)和sleep(),但在唤醒时会消耗CPU周期。我通过忽略FPE信号解决了问题。这是我的新计划: #include &l

这意味着当thread1被终止时,整个进程都将被终止。当进程的一个线程被终止时,有没有办法使进程保持活动状态?我需要它,因为我想使用进程的其他线程,但是任何线程都会被杀死。

请注意,我不想使用异常处理来避免终止线程。

您可能希望使用同步机制,例如信号量(使用sem_wait()、sem_post()),它在多个线程之间共享以引起延迟


另一种方法是在func()中使用while(1)和sleep(),但在唤醒时会消耗CPU周期。

我通过忽略FPE信号解决了问题。这是我的新计划:

#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;
}

这意味着当thread1被终止时,整个进程将不会终止,下一个线程可以运行它们的函数。

我认为您的方式是错误的。首先使用+C++代替C++。请澄清您的问题。@SamMokari感谢您的关注。为什么我必须使用++c?!!我需要一种在不终止进程的情况下终止线程的方法。因为我想使用进程中的其他线程,但是它们中的任何线程都会被杀死。++c对于实现您的目标的帮助与使用++g编译一样。无论如何,不是。不同类型的异常。您可以在FPE信号上使用信号处理程序,但不会得到想要的结果。你不是想用这个来结束一个线程,是吗?@user4581301不,我想使用进程的其他线程,但是它的任何线程都会被杀死。所以我想要一种在进程的任何线程被终止时不会终止进程的方法。
#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;
}
start thread 0
end thread 0
start thread 1
start thread 2
end thread 2