In the previous article, semaphores have been used to achieve mutual exclusion between threads, achieving the effect of mutual exclusion locks. Today, this article describes how to use semaphores for synchronization.
Mutex synchronization of semaphores is implemented through the PV primitive. We can register two semaphores so that they can interact with each other to achieve synchronization. The following examples are easy to understand:
# Include <stdlib. h> <br/> # include <stdio. h> <br/> # include <unistd. h> <br/> # include <pthread. h> <br/> # include <semaphore. h> <br/> # include <errno. h> </P> <p> # define return_if_fail (p) if (p) = 0) {printf ("[% s]: func error! /N ", _ FUNC _); Return ;}</P> <p> typedef struct _ privinfo <br/>{< br/> sem_t S1; <br/> sem_t S2; <br/> time_t end_time; <br/>}privinfo; </P> <p> static void info_init (privinfo * thiz ); <br/> static void info_destroy (privinfo * thiz); <br/> static void * pthread_func_1 (privinfo * thiz ); <br/> static void * pthread_func_2 (privinfo * thiz); </P> <p> int main (INT argc, char ** argv) <br/>{< br/> pthread_t pt_1 = 0; <br/> pthread_t pt_2 = 0; <br/> int ret = 0; <br/> privinfo * thiz = NULL; </P> <p> thiz = (privinfo *) malloc (sizeof (privinfo); <br/> If (thiz = NULL) <br/>{< br/> printf ("[% s]: failed to malloc priv. /n "); <br/> return-1; <br/>}</P> <p> info_init (thiz ); </P> <p> ret = pthread_create (& pt_1, null, (void *) pthread_func_1, thiz); <br/> If (Ret! = 0) <br/>{< br/> perror ("pthread_1_create:"); <br/>}</P> <p> ret = pthread_create (& pt_2, null, (void *) pthread_func_2, thiz); <br/> If (Ret! = 0) <br/>{< br/> perror ("pthread_2_create:"); <br/>}</P> <p> pthread_join (pt_1, null ); <br/> pthread_join (pt_2, null); </P> <p> info_destroy (thiz); </P> <p> return 0; <br/>}</P> <p> static void info_init (privinfo * thiz) <br/>{< br/> return_if_fail (thiz! = NULL); </P> <p> thiz-> end_time = Time (null) + 10; </P> <p> sem_init (& thiz-> S1, 0, 1); <br/> sem_init (& thiz-> S2, 0, 0); </P> <p> return; <br/>}</P> <p> static void info_destroy (privinfo * thiz) <br/>{< br/> return_if_fail (thiz! = NULL); </P> <p> sem_destroy (& thiz-> S1); <br/> sem_destroy (& thiz-> S2 ); </P> <p> free (thiz); <br/> thiz = NULL; </P> <p> return; <br/>}</P> <p> static void * pthread_func_1 (privinfo * thiz) <br/>{< br/> return_if_fail (thiz! = NULL); </P> <p> while (Time (null) <thiz-> end_time) <br/>{< br/> sem_wait (& thiz-> S2); <br/> printf ("pthread1: pthread1 get the lock. /n "); </P> <p> sem_post (& thiz-> S1); <br/> printf (" pthread1: pthread1 unlock/N "); </P> <p> sleep (1); <br/>}</P> <p> return; <br/>}</P> <p> static void * pthread_func_2 (privinfo * thiz) <br/>{< br/> return_if_fail (thiz! = NULL); </P> <p> while (Time (null) <thiz-> end_time) <br/>{< br/> sem_wait (& thiz-> S1); <br/> printf ("pthread2: pthread2 get the unlock. /n "); </P> <p> sem_post (& thiz-> S2); <br/> printf (" pthread2: pthread2 unlock. /n "); </P> <p> sleep (1); <br/>}</P> <p> return; <br/>}< br/>
After the execution result, we can see that the function of thread 2 is executed first, and then the function of thread 1 is executed. Both of them implement synchronization. Even though I know these concepts before I went to college, I have never practiced them. So sometimes, after a long time, I will be vague or even forgotten. If my work remains in this status, that's terrible. Although the external technologies are constantly changing, the core technologies remain the same no matter how they change. Therefore, we must lay a good foundation and learn other new knowledge, at that time, it would be easier to learn new knowledge. In the next article, we will implement a classic instance to review the multi-thread learning during this period, that is, consumers and producers.
~~ End ~~