• 用线程做一个火车票购票系统(可以根据需要选择线程个数)


    线程的概述

    进程:是系统中程序运行和资源分配的最基本单元,每个进程都有自己独立的存储空间(代码区,数据区,堆栈区)

    线程:通常叫做轻量级的进程,拥有自己的执行序列,是进程基本单元,每个进程都至少拥有一个线程也就是main线程;

    线程:共享的是进程的存储空间(代码区,数据区),除栈区数据以外,用的都是进程的空间;

    线程与进程的区别:

    线程:执行开销小,占用CPU的资源少,线程之间切换快,但是不利于资源管理;

    进程:恰好相反,可移植性强;

    注意:如果是线程相关函数的代码进行编译的时候,后面要加选项:-lpthread

    进程与线程之间对比:

    创建                退出               等待退出

    进程:     fork()                exit()              wait()或waitpid()

    线程:   pthread_creat         pthread_exit             pthread_join 

    #include<stdio.h>
    #include<stdlib.h>
    #include<pthread.h>
    int all=50;
    int k=1;
    pthread_mutex_t lock;
    pthread_cond_t cond;
    void rountine_func(void*arg)
    {
    printf("clean up! ");
    }
    void *pth_fun1()
    {
    int a=0;
    pthread_cleanup_push(rountine_func,NULL);
    while(1)
    {
    sleep(1);
    pthread_mutex_lock(&lock);
    if(all<=0)
    {
    pthread_mutex_unlock(&lock);
    pthread_cond_signal(&cond);
    if(k<1)
    {
    pthread_exit(0);
    }
    k--;
    }
    all--;
    a++;
    printf("线程1已购票%d ",a);
    pthread_mutex_unlock(&lock);
    }
    pthread_cleanup_pop(1);
    }
    void *pth_fun2()
    {
    int a=0;
    pthread_cleanup_push(rountine_func,NULL);
    while(1)
    {
    sleep(1);
    pthread_mutex_lock(&lock);
    if(all<=0)
    {
    pthread_mutex_unlock(&lock);
    pthread_cond_signal(&cond);
    if(k<=0)
    {
    pthread_exit(0);
    }
    k--;
    }
    all--;
    a++;
    printf("线程2已购票:%d ",a);
    pthread_mutex_unlock(&lock);
    }
    pthread_cleanup_pop(1);
    }
    void *pth_fun3()
    {
    pthread_cleanup_push(rountine_func,NULL);
    pthread_mutex_lock(&lock);
    pthread_cond_wait(&cond,&lock);
    all+=50;
    printf("站票开始出售:票数为%d ",all);
    pthread_mutex_unlock(&lock);
    pthread_exit(NULL);
    pthread_cleanup_pop(1);
    }
    void *pth_fun4()
    {
    int a=0;
    pthread_cleanup_push(rountine_func,NULL);
    while(1)
    {
    sleep(1);
    if(all!=50)
    {
    pthread_mutex_lock(&lock);
    if(a==20)
    {
    pthread_mutex_unlock(&lock);
    pthread_exit(0);
    }
    all++;
    a++;
    printf("线程4已退票:%d ",a);
    pthread_mutex_unlock(&lock);
    }
    else
    printf(" ");
    }
    pthread_cleanup_pop(1);
    }
    int main()
    {
    pthread_mutex_init(&lock,NULL);
    pthread_t thread1;
    if(pthread_create(&thread1,NULL,pth_fun1,NULL)==-1)
    {
    printf("create error! ");
    exit(1);
    }
    pthread_t thread2;
    if(pthread_create(&thread2,NULL,pth_fun2,NULL)==-1)
    {
    printf("create error! ");
    exit(1);
    }
    pthread_t thread3;
    if(pthread_create(&thread3,NULL,pth_fun3,NULL)==-1)
    {
    printf("create error! ");
    exit(1);
    }
    pthread_t thread4;
    if(pthread_create(&thread4,NULL,pth_fun4,NULL)==-1)
    {
    printf("create error! ");
    exit(1);
    }
    pthread_cleanup_push(rountine_func,NULL);
    pthread_cond_init(&cond,NULL);
    pthread_mutex_init(&lock,NULL);
    while(1)
    {
    if(all<=0)
    {
    pthread_exit(0);
    }
    printf("剩余票数:%d ",all);
    sleep(1);
    }
    pthread_mutex_destory(&lock);
    pthread_cond_destroy(&cond);
    pthread_cleanup_pop(1);
    return 0;
    }



  • 相关阅读:
    存图方式---邻接表&邻接矩阵&前向星
    Bellman-Ford算法的改进---SPFA算法
    POJ-2240 Arbitrage---判断正环+枚举
    POJ-1860 Currency Exchange---Bellman-Ford判断正环
    单源最短路径---Bellman-Ford算法
    ZOJ-2750 Idiomatic Phrases Game---Dijk最短路
    POJ-1135 Domino Effect---最短路Dijk
    单源最短路径---Dijkstra算法
    POJ-1679 The Unique MST---判断最小生成树是否唯一
    POJ-2031 Building a Space Station---MST + 空间距离
  • 原文地址:https://www.cnblogs.com/eastofeden/p/7375868.html
Copyright © 2020-2023  润新知