• 线程的条件变量实例


    情景1:

    Jack开着一辆出租车来到一个网站停车。看见没人就走了。过段时间。Susan来到网站准备乘车。可是没有来,于是就等着。过了一会Mike开着车来到了这个网站,Sunsan就上了Mike的车走了。如图所看到的:


    程序实现该情景:

    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <pthread.h>
    
    pthread_cond_t taxicond = PTHREAD_COND_INITIALIZER;
    pthread_mutex_t taximutex = PTHREAD_MUTEX_INITIALIZER;
    
    void *traveler_arrive(void *name)
    {
        char *p = (char *)name;
    
        printf ("Travelr: %s need a taxi now!
    ", p);
        pthread_mutex_lock(&taximutex);
        pthread_cond_wait(&taxicond, &taximutex);
        pthread_mutex_unlock(&taximutex);
        printf ("traveler: %s now got a taxi!
    ", p);
        pthread_exit(NULL);
    }
    
    void *taxi_arrive(void *name)
    {
        char *p = (char *)name;
        printf ("Taxi: %s arrives.
    ", p);
        pthread_cond_signal(&taxicond);
        pthread_exit(NULL);
    }
    
    int main (int argc, char **argv)
    {
        char *name;
        pthread_t thread;
        pthread_attr_t threadattr;
        pthread_attr_init(&threadattr);
    
        name = "Jack";
        pthread_create(&thread, &threadattr, taxi_arrive, name);
        sleep(1);
        name = "Susan";
        pthread_create(&thread, &threadattr, traveler_arrive, name);
        sleep(1);
        name = "Mike";
        pthread_create(&thread, &threadattr, taxi_arrive, name);
        sleep(1);
    
        return 0;
    } /* ----- End of main() ----- */

    情景2:

    Jack开着一辆出租车来到一个网站停车,看见没人就等着。过段时间,Susan来到网站准备乘车看见了Jack的出租车,于是就上去了。过了一会Mike开着车来到了这个网站,看见没人救等着。

    如图所看到的:


    程序实现该情景:

    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <pthread.h>
    
    int travelercount = 0;
    pthread_cond_t taxicond = PTHREAD_COND_INITIALIZER;
    pthread_mutex_t taximutex = PTHREAD_MUTEX_INITIALIZER;
    
    void *traveler_arrive(void *name)
    {
        char *p = (char *)name;
    
        pthread_mutex_lock(&taximutex);
    
        printf ("traveler: %s need a taxi now!
    ", p);
        travelercount++;
        pthread_cond_wait(&taxicond, &taximutex);
                
        pthread_mutex_unlock(&taximutex);
        printf ("traveler: %s now got a taxi!
    ", p);
        pthread_exit(NULL);
    }
    
    void *taxi_arrive(void *name)
    {
        char *p = (char *)name;
        printf ("Taxi: %s arrives.
    ", p);
        for(;;){
            if(travelercount){
                pthread_cond_signal(&taxicond);
                travelercount--;
                break;
            }
        }
    
        pthread_exit(NULL);
    }
    
    int main (int argc, char **argv)
    {
        char *name;
        pthread_t thread;
        pthread_attr_t threadattr;
        pthread_attr_init(&threadattr);
    
        name = "Jack";
        pthread_create(&thread, &threadattr, taxi_arrive, name);
        sleep(1);
        name = "Susan";
        pthread_create(&thread, &threadattr, traveler_arrive, name);
        sleep(3);
        name = "Mike";
        pthread_create(&thread, &threadattr, taxi_arrive, name);
        sleep(4);
    
        return 0;
    } /* ----- End of main() ----- */

    author: fulinux

    E-mail: fulinux@sina.com

    blog: blog.csdn.net/fulinus








  • 相关阅读:
    【转】主从同步出现一下错误:Slave_IO_Running: Connecting
    解决Mysql的主从数据库没有同步的两种方法
    xargs命令详解,xargs与管道的区别
    常用的排序算法的时间复杂度和空间复杂度
    IP 地址分类(A、B、C、D、E类)
    SNMP 协议介绍 转载
    TCP首部的TimeStamp时间戳选项 转载
    TCP SACK 介绍 转载
    HTTP header 介绍 转载
    CRT小键盘输入乱码
  • 原文地址:https://www.cnblogs.com/clnchanpin/p/6910099.html
Copyright © 2020-2023  润新知