Linux sends a signal to a thread, and if there is no handler function for the signal, the program ends.
As in the following program, creating a thread that sends a signal to the main thread can cause the program to end immediately
#include <stdio.h>
#include <pthread.h>
pthread_t t;
void* run (void* Arg)
{while
(1)
{
printf ("hello\n");
}
}
Main ()
{
pthread_create (&t, 0, run, 0);
Pthread_kill (t,34);
while (1);
This requires that the use of Pthread_kill and sigwait to control the thread by waiting for the signal should be added a signal processing function to achieve the control of the thread
Improved control thread routines
#include <stdio.h>
#include <pthread.h>
#include <signal.h>
pthread_t t1, T2;
sigset_t sigs;
void handle (int s)
{
}
void* R1 (void* Arg)
{
int s;
while (1)
{
printf ("1\n");
Sigwait (&sigs, &s);
}
void* R2 (void* Arg)
{while
(1)
{sleep
(1);
printf ("2\n");
Pthread_kill (t1);
}
Main ()
{
signal (handle);
Sigemptyset (&sigs);
Sigaddset (&sigs);
Pthread_create (&t1, 0, R1, 0);
Pthread_create (&t2, 0, r2, 0);
while (1);
}
Part of the effect