Linux线程通讯code example

 

条件变量与互斥锁、信号量的区别:
1.互斥锁必须总是由给它上锁的线程解锁,信号量的挂出即不必由执行过它的等待操作的同一进程执行。一个线程可以等待某个给定信号灯,而另一个线程可以挂出该信号灯。
2.互斥锁要么锁住,要么被解开(二值状态,类型二值信号量)。
3.由于信号量有一个与之关联的状态(它的计数值),信号量挂出操作总是被记住。然而当向一个条件变量发送信号时,如果没有线程等待在该条件变量上,那么该信号将丢失。
4.互斥锁是为了上锁而设计的,条件变量是为了等待而设计的,信号灯即可用于上锁,也可用于等待,因而可能导致更多的开销和更高的复杂性。

摘自:http://www.cnblogs.com/feisky/archive/2010/03/08/1680950.html 

 

以下内容摘自:http://blog.csdn.net/iw1210/article/details/8509629

1、互斥锁使用例子:

 

class ThreadMutex
{
public:
ThreadMutex()
{
    pthread_mutex_init(&m_mtx, NULL);
}

~ThreadMutex()
{
    pthread_mutex_destroy(&m_mtx);
}

inline void lock()
{
    pthread_mutex_lock(&m_mtx);
}

inline void unlock()
{
    pthread_mutex_unlock(&m_mtx);
}

private:
    pthread_mutex_t m_mtx;
};

 

2、条件变量使用例子:

 

#include <stdio.h>
#include <pthread.h>

pthread_mutex_t mutex;
pthread_cond_t    cond;

void *thread1(void *arg) 
{
    pthread_cleanup_push(pthread_mutex_unlock, &mutex);
    
    while (1) 
    {
        printf("thread1 is running\n");
        
        pthread_mutex_lock(&mutex);
        pthread_cond_wait(&cond, &mutex);
        printf("thread1 applied the condition\n");
        pthread_mutex_unlock(&mutex);
        sleep(4);
    }

    pthread_cleanup_pop(0);
}


void *thread2(void *arg) 
{
    while (1) 
    {
        printf("thread2 is running\n");
        
        pthread_mutex_lock(&mutex);
        pthread_cond_wait(&cond, &mutex);
        printf("thread2 applied the condition\n");
        pthread_mutex_unlock(&mutex);
        sleep(1);
    }
}

int main() 
{
    pthread_t thid1, thid2;
    
    printf("condition variable study!\n");
    pthread_mutex_init(&mutex, NULL);
    pthread_cond_init(&cond, NULL);

    pthread_create(&thid1, NULL, (void *) thread1, NULL);
    pthread_create(&thid2, NULL, (void *) thread2, NULL);
    
    do 
    {
        pthread_cond_signal(&cond);
    } while (1);
    
    pthread_exit(0);
    return 0;
}

 

3、信号量使用例子:

 

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>
#include <errno.h>

#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* pthread_func_1 (PrivInfo* prifo);
static void* pthread_func_2 (PrivInfo* prifo);

static void info_init (PrivInfo* prifo)
{
    return_if_fail (prifo != NULL);
    prifo->end_time = time(NULL) + 10;
    sem_init (&prifo->s1, 0, 1);
    sem_init (&prifo->s2, 0, 0);
}
static void info_destroy (PrivInfo* prifo)
{
    return_if_fail (prifo != NULL);
    sem_destroy (&prifo->s1);
    sem_destroy (&prifo->s2);
    free (prifo);
    prifo = NULL;
}

int main (int argc, char** argv)
{
    pthread_t pt_1 = 0;
    pthread_t pt_2 = 0;
    int ret = 0;
    PrivInfo* prifo = NULL;
    
    prifo = (PrivInfo* )malloc (sizeof (PrivInfo));
    if (prifo == NULL)
    {
        printf ("Failed to malloc priv.\n");
        return -1;
    }

    info_init(prifo);
    
    ret = pthread_create(&pt_1, NULL, (void*)pthread_func_1, prifo);
    if (ret != 0)
    {
        perror ("pthread_1_create:");
    }

    ret = pthread_create(&pt_2, NULL, (void*)pthread_func_2, prifo);
    if (ret != 0)
    {
        perror ("pthread_2_create:");
    }

    pthread_join (pt_1, NULL);
    pthread_join (pt_2, NULL);
    info_destroy (prifo);
    return 0;
}



static void* pthread_func_1 (PrivInfo* prifo)
{
    return_if_fail (prifo != NULL);
    while (time(NULL) < prifo->end_time)
    {
        sem_wait (&prifo->s2);
        printf ("pthread1: pthread1 get the lock.\n");
        sem_post (&prifo->s1);
        printf ("pthread1: pthread1 unlock\n");
        sleep (1);
    }
    return;
}

static void* pthread_func_2 (PrivInfo* prifo)
{
    return_if_fail (prifo != NULL);
    while (time (NULL) < prifo->end_time)
    {
        sem_wait (&prifo->s1);
        printf ("pthread2: pthread2 get the unlock.\n");
        sem_post (&prifo->s2);
        printf ("pthread2: pthread2 unlock.\n");
        sleep (1);
    }
    return;
}

 

posted on 2017-07-15 15:16  Waaaaaall-E  阅读(207)  评论(0编辑  收藏  举报

导航