C++的多线程互斥锁实战

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chengqiuming/article/details/89279614

一 互斥与同步的基本函数二 用互斥锁的多线程累加

函数

说明

pthread_mutex_init()

互斥锁的初始化

pthread_mutex_lock()

锁定互斥锁,如果尝试锁定已经被上锁的互斥锁则阻塞至可用为止

pthread_mutex_trylock()

非阻塞的锁定互斥锁

pthread_mutex_unlock()

释放互斥锁

pthread_mutex_destory()

互斥锁销毁函数

二 用互斥锁的多线程累加

1 代码

#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/time.h>
#include <string.h>
#include <cstdlib>
int gcn = 0;

pthread_mutex_t mutex;
    
void *thread_1(void *arg) {
    int j;
    for (j = 0; j < 10000000; j++) {
        pthread_mutex_lock(&mutex);  
        gcn++;
        pthread_mutex_unlock(&mutex);  
    }  
    pthread_exit((void *)0);
}

void *thread_2(void *arg) {
    int j;
    for (j = 0; j < 10000000; j++) {
        pthread_mutex_lock(&mutex);  
        gcn++;
        pthread_mutex_unlock(&mutex);    //解锁
    }  
    pthread_exit((void *)0);
}
int main(void)
{
    int j,err;
    pthread_t th1, th2;
     
    pthread_mutex_init(&mutex, NULL); //初始化互斥锁
    for (j = 0; j < 10; j++)
    {
        err = pthread_create(&th1, NULL, thread_1, (void *)0);
        if (err != 0) {
            printf("create new thread error:%s\n", strerror(err));
            exit(0);
        }  
        err = pthread_create(&th2, NULL, thread_2, (void *)0);
        if (err != 0) {
            printf("create new thread error:%s\n", strerror(err));
            exit(0);
        }  
           
        err = pthread_join(th1, NULL);
        if (err != 0) {
            printf("wait thread done error:%s\n", strerror(err));
            exit(1);
        }
        err = pthread_join(th2, NULL);
        if (err != 0) {
            printf("wait thread done error:%s\n", strerror(err));
            exit(1);
        }
        printf("gcn=%d\n", gcn);
        gcn = 0;
    }
    pthread_mutex_destroy(&mutex); //销毁互斥锁

    return 0;
}

2 运行

[root@localhost test]# g++ -o test test.cpp -lpthread
[root@localhost test]# ./test
gcn=20000000
gcn=20000000
gcn=20000000
gcn=20000000
gcn=20000000
gcn=20000000
gcn=20000000
gcn=20000000
gcn=20000000
gcn=20000000

三 用互斥锁的进行同步的卖货程序

1 代码

#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
int a = 200; //当前货物价值
int b = 100; //当前现金
pthread_mutex_t lock; //定义一个全局的互斥锁

void* ThreadA(void*) //伙计卖货线程
{
    
    while (1)
    {
        pthread_mutex_lock(&lock);          //上锁
        a -= 50; //卖出价值50元的货物
        b += 50;//收回50元钱
        pthread_mutex_unlock(&lock);    //解锁
    }

}
void* ThreadB(void*) //老板对账线程
{
    while (1)
    {
        pthread_mutex_lock(&lock);  //上锁
        printf("%d\n", a + b);
        pthread_mutex_unlock(&lock);  //解锁
        sleep(1);    
    }
}
int main()
{
    pthread_t tida, tidb;
    pthread_mutex_init(&lock, NULL); //初始化互斥锁
    pthread_create(&tida, NULL, ThreadA, NULL); //创建伙计卖货线程
    pthread_create(&tidb, NULL, ThreadB, NULL); //创建老板对账线程
    pthread_join(tida, NULL);
    pthread_join(tidb, NULL);
    
    pthread_mutex_destroy(&lock); //销毁互斥锁
    
    return 1;
}

2 运行

[root@localhost test]# g++ -o test test.cpp -lpthread
[root@localhost test]# ./test
300
300
300
300
300
300
^C

3 说明

这个例子加了互斥锁,从中发现,老板每次对账输出的记过都是300,这是因为伙计卖货和收钱的过程没有被打断。

猜你喜欢

转载自blog.csdn.net/chengqiuming/article/details/89279614