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

    原子操作

    互斥mutex相互排斥的意思,它是一种锁或者信号灯。

    互斥用来保护多个线程共享的数据和结构,不会被同时修改,一个互斥锁只能有两种状态:

    locked 枷锁

    unlocked 解锁

    加锁后互斥不让其他线程访问。

    任何时刻只能有一个线程来掌握某个互斥上锁。

    一个线程如果试图在一个已经加锁的互斥上再加锁,这个线程会被挂起,知道加锁的线程释放掉

    互斥锁为止。

    pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

    int pthread_mutex_lock(pthread_mutex_t *mutex);//加锁

    int pthread_mutex_unlock(pthread_mutex_t *mutex);//解锁

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

    ////////////////////////////////////////////////////////////////////////////////////////////////////


    //最恰当的互斥用法
    /*
    ============================================================================
    Name : thread.c
    Author : zhujy
    Version :
    Copyright : Your copyright notice
    Description : Hello World in C, Ansi-style
    ============================================================================
    */

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

    pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;//初始化了一个MUTEX锁

    int count = 0;

    void *func1(void *arg)
    {

    int *a = (int *) arg;
    printf("thread%d start ", *a);
    int i;
    for (i = 0; i < 10; i++)
    {
    printf("thread%d is running ", *a);
    sleep(1);
    pthread_mutex_lock(&mutex);//给mutex加锁,这是一条原子操作,不可能出现两个线程同时执行这个代码
    count++;//这段代码受到保护,永远只有一个线程可以操作
    pthread_mutex_unlock(&mutex);//给mutex解锁
    }
    printf("thread%d end ", *a);

    pthread_exit(NULL);
    }

    int main(int arg, char * args[])
    {
    printf("process start ");
    pthread_t thr_d1, thr_d2;
    int i[2];
    i[0] = 1;
    i[1] = 2;


    pthread_create(&thr_d1, NULL, func1, &i[0]);
    pthread_create(&thr_d2, NULL, func1, &i[1]);
    pthread_join(thr_d1, NULL);
    pthread_join(thr_d2, NULL);

    printf("process end ");
    return 0;
    }

    ////////////////////////////////////////////////////////////////////////////////////////////////////

    /互斥锁的例子

    pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;//初始化了一个MUTEX锁

    void *func1(void *arg)
    {
    pthread_mutex_lock(&mutex);//给mutex加锁,这是一条原子操作,不可能出现两个线程同时执行这个代码  是一个队列结果锁几次就必须解锁几次。
    int *a = (int *) arg;
    printf("thread%d start ", *a);
    int i;
    for (i = 0; i < 10; i++)
    {
    printf("thread%d is running ", *a);
    sleep(1);
    }
    printf("thread%d end ", *a);
    pthread_mutex_unlock(&mutex);//给mutex解锁
    pthread_exit(NULL);
    }

    int main(int arg, char * args[])
    {
    printf("process start ");
    pthread_t thr_d1, thr_d2;
    int i[2];
    i[0] = 1;
    i[1] = 2;


    pthread_create(&thr_d1, NULL, func1, &i[0]);
    pthread_create(&thr_d2, NULL, func1, &i[1]);
    pthread_join(thr_d1, NULL);
    pthread_join(thr_d2, NULL);

    printf("process end ");
    return 0;
    }

  • 相关阅读:
    [Leetcode] Maximum Gap
    [Leetcode] Reverse Integer
    [Leetcode] Pow(x, n)
    Theano2.1.21-基础知识之theano中多核的支持
    Theano2.1.3-基础知识之更多的例子
    Theano2.1.17-基础知识之剖析theano的函数
    Theano2.1.16-基础知识之调试:常见的问题解答
    Theano2.1.15-基础知识之theano如何处理shapre信息
    Theano2.1.14-基础知识之理解为了速度和正确性的内存别名
    Theano2.1.13-基础知识之PyCUDA、CUDAMat、Gnumpy的兼容
  • 原文地址:https://www.cnblogs.com/yuankaituo/p/4373056.html
Copyright © 2011-2022 走看看