Standards / Extensions | C or C++ | Dependencies |
---|---|---|
POSIX.4a |
both | POSIX(ON) |
#define _OPEN_THREADS
#include <pthread.h>
#include <signal.h>
int pthread_kill(pthread_t thread, int sig);
#define _UNIX03_THREADS
#include <signal.h>
int pthread_kill(pthread_t thread, int sig);
Directs a signal sig to the thread thread. The value of sig must be either 0 or one of the symbols defined in signal.h. (See Table 1 for a list of signals.) If sig is 0, pthread_kill() performs error checking but does not send a signal.
pthread_t is the data type used to uniquely identify a thread. It is returned by pthread_create() and used by the application in function calls that require a thread identifier.
Special behavior for C++: If a thread is sent a signal using pthread_kill() and that thread does not handle the signal, then destructors for local objects may not be executed.
If successful, pthread_kill() returns 0.
Special behavior for Single UNIX Specification, Version 3: If unsuccessful, pthread_kill() returns an error number to indicate the error.
⁄* CELEBP35 *⁄
#define _OPEN_THREADS
#include <errno.h>
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
void *threadfunc(void *parm)
{
int threadnum;
int *tnum;
sigset_t set;
tnum = parm;
threadnum = *tnum;
printf("Thread %d executing\n", threadnum);
sigemptyset(&set);
if(sigaddset(&set, SIGUSR1) == -1) {
perror("Sigaddset error");
pthread_exit((void *)1);
}
if(sigwait(&set) != SIGUSR1) {
perror("Sigwait error");
pthread_exit((void *)2);
}
pthread_exit((void *)0);
}
main() {
int status;
int threadparm = 1;
pthread_t threadid;
int thread_stat;
status = pthread_create( &threadid,
NULL,
threadfunc,
(void *)&threadparm);
if ( status < 0) {
perror("pthread_create failed");
exit(1);
}
sleep(5);
status = pthread_kill( threadid, SIGUSR1);
if ( status < 0)
perror("pthread_kill failed");
status = pthread_join( threadid, (void *)&thread_stat);
if ( status < 0)
perror("pthread_join failed");
exit(0);
}