This short program deliberately kills a thread after pthread_join
so it may cause SEGV on some targets.
gcc -pthread thread1.c
#include <pthread.h> | |
#include <signal.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <string.h> | |
#define N 5 | |
void *thread_function(void *arg) { | |
int i; | |
for (i = 0; i < N; i++) { | |
printf("Hello!\n"); | |
sleep(1); | |
} | |
return NULL; | |
} | |
int main(void) { | |
pthread_t th; | |
int rc = 0; | |
if (pthread_create(&th, NULL, thread_function, NULL)) { | |
printf("Error creating thread.\n"); | |
abort(); | |
} | |
if (pthread_join(th, NULL)) { | |
printf("Error joining thread."); | |
abort(); | |
} | |
if ((rc = pthread_kill(th, SIGTERM)) != 0) { | |
printf("Error in pthread_kill; errno:%x, message:%s\n", rc, strerror(rc)); | |
abort(); | |
} | |
exit(0); | |
} |