Good day.
I read the article Programming for Linux: threads , it has an example of the use of revocation points and macros pthread_cleanup_{push|pop}
. But when performing this example on my computer, it does not work exactly as it is written in the article. It says that the stream should display 4 lines and only then end, but for some reason it does not.
Here is an example:
#include <stdlib.h> #include <stdio.h> #include <errno.h> #include <pthread.h> #include <unistd.h> void exit_func(void * arg) { free(arg); printf("Freed the allocated memory.\n"); } void * thread_func(void *arg) { int i; void * mem; pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL); mem = malloc(1024); printf("Allocated some memory.\n"); pthread_cleanup_push(exit_func, mem); pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); for (i = 0; i < 4; i++) { sleep(1); printf("I'm still running!!!\n"); } pthread_cleanup_pop(1); } int main(int argc, char * argv[]) { pthread_t thread; pthread_create(&thread, NULL, thread_func, NULL); pthread_cancel(thread); pthread_join(thread, NULL); printf("Done.\n"); return EXIT_SUCCESS; }
What could be the problem? Please clarify.
P.S. OS ALTLinux 5.1. I compile this way: gcc main.cpp -lpthread -o threads