i have basic problem of handling signal in multi-threaded process.
in code, create 1 sub-thread main thread, listen sigalrm
later trigger main thread (using other function timer_create
gives me same result, please don't focus on this).
the problem is, instead of catching signal, whole process terminated strange "alarm clock" output on console.
this code:
#include <iostream> #include <sys/time.h> #include <unistd.h> #include <csignal> using namespace std; void* run_something(void* args){ //unblock sigalrm catched sigset_t sig; sigemptyset(&sig); sigaddset(&sig, sigalrm); sigprocmask(sig_unblock, &sig, null); //tried pthread_sigmask //wait sigalrm int catchedsig; sigwait(&sig, &catchedsig); cout<<"in sub-thread, sigalrm catched, return"<<endl; } int main(int argc, char** argv){ //block sigalrm in main thread sigset_t sig; sigemptyset(&sig); sigaddset(&sig, sigalrm); sigprocmask(sig_block, &sig, null); //create new thread pthread_t thread; pthread_attr_t attr; pthread_attr_init(&attr); pthread_create(&thread, &attr, run_something, null); //trigger sigarlm after 2s alarm(2); //tried timer_create/sigevent //wait cout<<"in main thread, waiting sub-thread terminate"<<endl; pthread_join(thread, null); cout<<"in main thread, terminating"<<endl; return exit_success; }
expected result
- in main thread, waiting sub-thread terminate
- in sub-thread,
sigalrm
getting caught, return - in main thread, terminating
observed result
- in main thread, waiting sub-thread terminate
- alarm clock
additional info: i'm using g++ (debian 5.4.0-4) 5.4.0 20160609.
your run_something
thread unblocks sigalrm before calling sigwait
signal, this undefined behavior. sigwait
removes signal set of pending (i.e., blocked) signals.
don't unblock in thread , you'll see behavior expect.
Comments
Post a Comment