zoukankan      html  css  js  c++  java
  • 线程同步

    有时候我们需要用到线程同步来控制线程运行顺序。

    /***
    synchro.c
    ***/
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    #include<pthread.h>
    #include<errno.h>
    
    void *func(void *arg)
    {
        int threadno = *(int *)arg;
        int i = 0;
        for(; i < 10; i++)
        {
            printf("%d thread%d
    ",threadno,i);
            sleep(1);
        }
        return NULL;
    }
    
    int main()
    {
        pthread_t t1,t2;
        int i = 1,j = 2;
        pthread_create(&t1,NULL,func,&i);
        pthread_create(&t2,NULL,func,&j);
        
        pthread_join(t1,NULL);    
        pthread_join(t2,NULL);
    
        printf("main exit
    ");
        return EXIT_SUCCESS;
    }

    exbot@ubuntu:~/wangqinghe/thread/20190729$ ./synchro

    2 thread0

    1 thread0

    2 thread1

    1 thread1

    2 thread2

    1 thread2

    2 thread3

    1 thread3

    2 thread4

    1 thread4

    2 thread5

    1 thread5

    2 thread6

    1 thread6

    2 thread7

    1 thread7

    2 thread8

    1 thread8

    2 thread9

    1 thread9

    main exit

    线程1和线程2互相抢占资源。

    用互斥的方式来控制只有一个线程来使用资源。

    /***
    lock.c
    ***/
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    #include<pthread.h>
    #include<errno.h>
    
    pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
    
    void *func(void *arg)
    {
    
        pthread_mutex_lock(&mutex);    
    
        int threadno = *(int *)arg;
        int i = 0;
        for(; i < 10; i++)
        {
            printf("%d thread%d
    ",threadno,i);
            sleep(1);
        }
        pthread_mutex_unlock(&mutex);
        return NULL;
    }
    
    int main()
    {
        pthread_t t1,t2;
        int i = 1,j = 2;
        pthread_create(&t1,NULL,func,&i);
        pthread_create(&t2,NULL,func,&j);
        
        pthread_join(t1,NULL);    
        pthread_join(t2,NULL);
    
        printf("main exit
    ");
        return EXIT_SUCCESS;
    }

    运行结果:

    exbot@ubuntu:~/wangqinghe/thread/20190729$ ./synchro1

    2 thread0

    2 thread1

    2 thread2

    2 thread3

    2 thread4

    2 thread5

    2 thread6

    2 thread7

    2 thread8

    2 thread9

    1 thread0

    1 thread1

    1 thread2

    1 thread3

    1 thread4

    1 thread5

    1 thread6

    1 thread7

    1 thread8

    1 thread9

    main exit

    线程2先进入后一直运行到任务结束,对mutex解锁后线程1才进入。

    PTHREAD_MUTEX_INITIALIZER 是初始化一个快速锁的宏定义。

    Pthread_mutex_t = PTHREAD_MUTEX_INITIALIZER

    加锁解锁函数:

    int pthread_mutex_lock(&mutex);

    int pthread_mutex_unlock(&mutex);

  • 相关阅读:
    170815、redis3.0安装配置
    170814、Java使用gzip压缩文件、还原文件
    170811、Java获取jdk系统环境变量
    170810、spring+springmvc+Interceptor+jwt+redis实现sso单点登录
    加密概述
    软件测试入门第02天:基础理论知识
    软件测试入门第01天:综述
    【心路历程】永远热泪盈眶
    Linux部署Django:报错 nohup: ignoring input and appending output to ‘nohup.out’
    【WEB】jQuery 判断复选框是否选中
  • 原文地址:https://www.cnblogs.com/wanghao-boke/p/11264658.html
Copyright © 2011-2022 走看看