I am studying thread in APUE book 2e
I thought pthread_cleanup_pop functions is for setting the pushed function by pthread_cleanup_push() to be executed or not. So i
did I get pthread_cleanup_pop wrong?
No, you didn't.
The reason your code executed cleanup handlers, is that your control never reaches pthread_cleanup_pop(0)
. Instead, you always execute return
in thr_fn1
, and pthread_exit
in thr_fn2
.
Try creating the threads with pthread_create(&tid1, NULL, thr_fn1, (void *)0);
instead of 1
. When I do that, I get (expected):
thread 1 start
thread 1 push complete
thread 2 start
thread 2 push complete
thread 1 exit code 1
thread 2 exit code 2