Stream termination in C

I have a C program that calls threads.

iret1 = pthread_create( &thread1, NULL, readdata, NULL);
iret2 = pthread_create( &thread2, NULL, timer_func, NULL);
pthread_join(thread2, NULL);

Thread 2 returns after the execution of some function, after which I want to stop the execution of thread 1. How can I do this?

+5
source share
6 answers

You can stop the thread with pthread_cancel:

pthread_cancel(thread1);

And in readdata:

/* call this when you are not ready to cancel the thread */
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
...
/* call this when you are ready to cancel the thread */
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);

See the pthread_cancel page for more information . An example is given there.

pthread_cancel, , 1. IPC, , pipe .

+8

, , , . , , . , , .

, , ..

+2

Thread1 , , .

, . .

- :

thread1stop=TRUE; //Thread 1 has access to this boolean value
pthread_join(thread1, NULL);
+2

thread1, pthread_cancel(thread1).

0

tkill (tid, SIGTERM) - , , .

0

All Articles