In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
# if 0 cleans up the function pthread_cleanup_push () when the thread ends. Initialize pthread_cleanup_pop () should be executed as soon as possible when the thread starts. The function will be called automatically when the following three items are encountered, and there is no need for PC to execute this function. Call pthread_exit () 2. Respond to a cancellation request such as pthread_cancel () 3. If the pthread_cleanup_pop (arg) parameter arg is not 0, it will be automatically executed when the thread exits. If there are none of the above three cases, it will not be executed. # endif#include # include sem_t sm0,sm1,sm2,sm3; / / semaphores define semaphores as callbacks that control thread execution void ret_fn (void*str) / / thread exit calls {fputs (str,stdout);} void thread0 (void) {if (0! = sem_wait (& sm0)) / / semaphores block {perror ("sem_wait_0") } char * str = "thead0_return_FN is runing\ n"; pthread_cleanup_push (ret_fn,str); / / push the callback function into the stack fputs ("thread0 is runing\ n", stdout); pthread_cleanup_pop (0); / / unstack the callback function and automatically call it when a specific condition is encountered, and execute return;} void thread1 (void) {if (0! = sem_wait (& sm1)) {perror ("sem_wait_1") without the need for PC to refer to it. } char * str = "thead1_return_FN is runing\ n"; pthread_cleanup_push (ret_fn,str); fputs ("thread1 is runing\ n", stdout); pthread_exit ((void*) 1); pthread_cleanup_pop (0);} void thread2 (void) {if (0! = sem_wait (& sm2)) {perror ("sem_wait_2");} char * str = "thead2_return_FN is runing\ n"; pthread_cleanup_push (ret_fn,str) Fputs ("thread2 is runing\ n", stdout); pthread_cleanup_pop (1); fputs ("Thread ends before pthread_cleanup_pop\ n", stdout); / / Thread ends before pthread_cleanup_pop} void thread3 (void) {char * str = "thead3_return_FN is runing\ n"; pthread_cleanup_push (ret_fn,str); if (0! = sem_wait (& sm3)) {perror ("sem_wait_3") } fputs ("thread3 is runing\ n", stdout); usleep (100); / / enable the pthread_cancel in the main thread to execute pthread_cleanup_pop (0);} int main (void) {pthread_t thd0,thd1,thd2,thd3;if (0! = sem_init (& sm0,0,0)) / / initialize semaphore {perror ("sem_init_0"); exit (1);} if (0! = sem_init (& sm1,0,0)) {perror ("sem_init_0") Exit (1);} if (0! = sem_init (& sm2,0,0)) {perror ("sem_init_0"); exit (1);} if (0! = sem_init (& sm3,0,0)) {perror ("sem_init_0"); exit (1);} if (0! = pthread_create (& thd0,NULL, (void*) thread0,NULL)) / / create thread {perror ("pthread_create_0\ n"); exit (1) } if (0! = pthread_create (& thd1,NULL, (void*) thread1,NULL) {perror ("pthread_create_1\ n"); exit (1);} if (0! = pthread_create (& thd2,NULL, (void*) thread2,NULL)) {perror ("pthread_create_2\ n"); exit (1);} if (0! = pthread_create (& thd3,NULL, (void*) thread3,NULL)) {perror ("pthread_create_3\ n"); exit (1) } unsigned char status = 0xFramp / control repeat test mask while (1) {fputs ("Please enter test options: 0 (no callback function called), 1 (pthread_exit), 2 (pthread_cancel), 3 (parameter non-0 pthread_cleanup_pop)\ n", stdout); int in;scanf ("% d", & in) Switch (in) {case 0: {if (! (status & 0x1)) {fputs ("this project has been tested, please select another, thank you\ n", stdout); break;} if (0! = sem_post (& sm0)) / / activate semaphore to allow child thread THD0 to continue to execute {perror ("sem_post_0\ n");} status & = 0xescape break } case 1: {if (! (status & 0x2)) {fputs ("this project has been tested, please select other, thank you\ n", stdout); break;} if (0! = sem_post (& sm1)) {perror ("sem_post_1\ n");} status & = 0xc\ n;} case 2: {if (! (status & 0x4)) {fputs ("this project has been tested, please select other, thank you\ n", stdout); break } if (0! = sem_post (& sm2)) {perror ("sem_post_2\ n");} status & = 0xbpolitics;} case 3: {if (! (status & 0x8)) {fputs ("this project has been tested, please choose something else, thank you\ n", stdout); break;} if (0! = sem_post (& sm3)) {perror ("sem_post_3\ n");} pthread_cancel (thd3); status & = 0x7breakbreak;} default: } sleep (1);} return 0;}
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.