I want to broadcast a signal from one thread to all other threads in a process. The threads receiving that signal should handle the signal in a signal handler. How can I achieve this?
I tried the following code, but it exits by printing User defined signal 1. What's going on?
#include <stdio.h>
#include <signal.h>
#include <sys/types.h>
#include <pthread.h>
const int NTHREADS = 4;
long prev_fact, i;
void SIGhandler(int);
void SIGhandler(int sig)
{
printf("\nThread %lx Received a SIGUSR1.\n", pthread_self());
}
void* tfunc( void* arg )
{
printf( "Thread %lx executing...\n", pthread_self() );
while( 1 )
;
}
int main()
{
int i;
pthread_t t[NTHREADS];
for( i = 0; i < NTHREADS; i++ )
pthread_create( &t[i], NULL, tfunc, NULL );
for( i = 0; i < NTHREADS; i++ )
pthread_kill( t[i], SIGUSR1 );
for( i = 0; i < NTHREADS; ++i)
pthread_join(t[i], NULL);
return 0;
}