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);

  • 相关阅读:
    生成EXCEL报表
    数据库操作
    类似于qq魔法表情的窗口
    关于c#如何遍历listbox里ValueMember属性的值
    ExecuteNonQuery()
    startsWith()
    2019HDU多校第七场 HDU6646 A + B = C 【模拟】
    2019HDU多校第七场 HDU6651 Final Exam
    2019HDU多校第七场 HDU6656 Kejin Player H 【期望递归】
    P1962 斐波那契数列 【矩阵快速幂】
  • 原文地址:https://www.cnblogs.com/wanghao-boke/p/11264658.html
Copyright © 2011-2022 走看看