线程同步的方法有哪些?Linux下实现线程同步的三种方法( 四 )

< thiz->end_time)67{68sem_wait (&thiz->s2);69printf ("pthread1: pthread1 get the lock./n");70sem_post (&thiz->s1);71printf ("pthread1: pthread1 unlock/n");72sleep (1);73}74return;75}76static void* pthread_func_2 (PrivInfo* thiz)77{78return_if_fail (thiz != NULL);79while (time (NULL) < thiz->end_time)80{81sem_wait (&thiz->s1);82printf ("pthread2: pthread2 get the unlock./n");83sem_post (&thiz->s2);84printf ("pthread2: pthread2 unlock./n");85sleep (1);86}87return;88}复制代码#include #include #include #include #include #include #define return_if_fail(p) if((p) == 0){printf ("[%s]:func error!/n", __func__);return;}typedef struct _PrivInfo{sem_t s1;sem_t s2;time_t end_time;}PrivInfo;static void info_init (PrivInfo* thiz);static void info_destroy (PrivInfo* thiz);static void* pthread_func_1 (PrivInfo* thiz);static void* pthread_func_2 (PrivInfo* thiz);int main (int argc, char** argv){pthread_t pt_1 = 0;pthread_t pt_2 = 0;int ret = 0;PrivInfo* thiz = NULL;thiz = (PrivInfo* )malloc (sizeof (PrivInfo));if (thiz == NULL){printf ("[%s]: Failed to malloc priv./n");return -1;}info_init (thiz);ret = pthread_create (&pt_1, NULL, (void*)pthread_func_1, thiz);if (ret != 0){perror ("pthread_1_create:");}ret = pthread_create (&pt_2, NULL, (void*)pthread_func_2, thiz);if (ret != 0){perror ("pthread_2_create:");}pthread_join (pt_1, NULL);pthread_join (pt_2, NULL);info_destroy (thiz);return 0;}static void info_init (PrivInfo* thiz){return_if_fail (thiz != NULL);thiz->end_time = time(NULL)10;sem_init (&thiz->s1, 0, 1);sem_init (&thiz->s2, 0, 0);return;}static void info_destroy (PrivInfo* thiz){return_if_fail (thiz != NULL);sem_destroy (&thiz->s1);sem_destroy (&thiz->s2);free (thiz);thiz = NULL;return;}static void* pthread_func_1 (PrivInfo* thiz){return_if_fail(thiz != NULL);while (time(NULL) < thiz->end_time){sem_wait (&thiz->s2);printf ("pthread1: pthread1 get the lock./n");sem_post (&thiz->s1);printf ("pthread1: pthread1 unlock/n");sleep (1);}return;}static void* pthread_func_2 (PrivInfo* thiz){return_if_fail (thiz != NULL);while (time (NULL) < thiz->end_time){sem_wait (&thiz->s1);printf ("pthread2: pthread2 get the unlock./n");sem_post (&thiz->s2);printf ("pthread2: pthread2 unlock./n");sleep (1);}return;}【线程同步的方法有哪些?Linux下实现线程同步的三种方法】以上便是Linux下实现线程同步常用的三种方法,大家都知道,线程的最大的亮点便是资源共享性,而资源共享中的线程同步问题却是一大难点,希望小编的归纳能够对大家有所帮助!

推荐阅读