Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Conditional variables of unix thread synchronization and test examples of mutexes

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

# include # define satisfy true# define unsatisfy false//pthread_mutex_t mut_loc = PTHREAD_MUTEX_INITIALIZER;// is statically initialized to PTHREAD_MUTEX_INITIALIZER as a constant, and pthread_mutex_t mut_loc;pthread_cond_t thd_con;struct timespec tsp;bool condition = unsatisfy is assigned globally Void maketimeout (struct timespec * tsp, int add_sec) / / sets the timeout function, the current time plus the waiting time, because pthread_cond_timedwait only knows the current time format {struct timeval now;gettimeofday (& now,NULL); / / gets the current time tsp- > tv_sec = now.tv_sec;tsp- > tv_nsec = now.tv_usec * 1000th TSP-> tv_sec + = add_sec; / / wait 20 seconds} void thread0 (void) {int ret If (0! = pthread_mutex_lock (& mut_loc)) / / Lock {perror ("pthread_mutex_lock_0\ n"); return;} if (condition = = unsatisfy) {fputs ("condition is not met, wait for condition again!! \ n ", stdout); maketimeout (& tsp, 2); / / set the timeout time 2 seconds / / wait condition, and unlock, the thread goes to the waiting queue, and if the condition is met, the signal will be locked. / / Thread is in a mode called non-contention. Since the condition is not met, it will not compete with other threads for locks until the condition is met. Only when the condition is met will it compete for ret = pthread_cond_timedwait (& thd_con,&mut_loc,&tsp); if (ETIMEDOUT = = ret) {fputs ("until the timeout condition is not met, re-wait for the condition!! \ n ", stdout);} else if (0 = = ret) {fputs (" Thread 0 gets the condition while waiting! \ n ", stdout);} else {perror (" other error for pthread_cond_timedwait\ n "); pthread_exit ((void *) 1);} if (condition = = satisfy) {fputs (" 0 _ condition satisfied!! \ n ", stdout); condition = unsatisfy;}} else if (condition = = satisfy) {fputs (" 1 _ condition satisfied!! \ n ", stdout); condition = unsatisfy;} else {perror (" error condition\ n "); pthread_exit ((void *) 1);} if (0! = pthread_mutex_unlock (& mut_loc)) {perror (" pthread_mutex_lock_0\ n "); return;} pthread_exit ((void *) 0);} void thread1 (void) {int ret;ret = pthread_mutex_trylock (& mut_loc); if (EBUSY = ret) {fputs (" the lock is owned by thread 0! \ n ", stdout);} else if (0 = = ret) {if (0! = pthread_cond_signal (& thd_con)) {perror (" pthread_cond_signal\ n "); pthread_exit ((void *) 1);} condition = satisfy;fputs (" Thread 1 satisfies the condition\ n ", stdout); if (0! = pthread_mutex_unlock (& mut_loc)) {perror (" pthread_mutex_lock_1\ n "); pthread_exit ((void *) 1) }} else {perror ("other errors for pthread_mutex_lock_1\ n"); pthread_exit ((void *) 1);} pthread_exit ((void *) 0);} int main (int argc, char* argv []) {pthread_t thd0, thd1;if (0! = pthread_mutex_init (& mut_loc,NULL)) / / pthread_mutex_init is paired with pthread_mutex_destroy because it is dynamic even if malloc is used to generate {perror ("pthread_mutex_init\ n") Exit (1);} if (0! = pthread_cond_init (& thd_con,NULL)) / / pthread_cond_init is paired with pthread_cond_destroy because it dynamically uses malloc to generate {perror ("pthread_cond_init\ n"); exit (1);} if (0! = pthread_create (& thd0,NULL, (void*) thread0,NULL) / / create thread 0 {perror ("pthread_create_0\ n"); exit (1);} sleep (1) / / Let thread 0 execute if (0! = pthread_create (& thd1,NULL, (void*) thread1,NULL) / / create thread 1 {perror ("pthread_create_0\ n"); exit (1);} if (0! = pthread_join (thd1,NULL)) / / if thread card separation property is not available, if thread 1 does not exit, it is in blocking state {perror ("pthread_join_0\ n"); exit (1) } if (0! = pthread_join (thd0,NULL)) / / if thread card separation attribute this function is not available, if thread 0 does not exit, it is in blocking state {perror ("pthread_join_1\ n"); exit (1);} if (0! = pthread_cond_destroy (& thd_con)) / / paired with pthread_cond_init using {perror ("pthread_cond_destory\ n"); exit (1) } if (0! = pthread_mutex_destroy (& mut_loc)) / / pair with pthread_mutex_init using {perror ("pthread_mutex_init\ n"); exit (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.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report