Linux:多线程不同步导致错误示例

97 篇文章 7 订阅

Linux:多线程不同步导致错误示例

多个线程都可以看到同一个对象并有对其操作时涉及到同步问题。

不同步现象:

Counter.h

#ifndef _COUNTER_H
#define _COUNTER_H

class Counter {
public:
    Counter();
    Counter(int);
    virtual ~Counter();
    int getCnt();
    void setCnt(int);
    void incCnt();
private:
    int mCnt;
};

#endif

Counter.C


#include "Counter.h"

Counter::Counter()
{ mCnt = 0; }

Counter::~Counter()
{}

int Counter::getCnt()
{ return mCnt; }

void Counter::setCnt(int cnt)
{ mCnt = cnt; }

void Counter::incCnt()
{
    mCnt = mCnt + 1;
}

main.C


#include <iostream>
#include <pthread.h>
#include "Counter.h"

using namespace std;

static Counter *pCnt = NULL;

void *threadRunA(void *)
{
    cout<<"start threadRunA==>"<<endl;
    usleep(200000);
    int cnts = 10000000;
    while (cnts--) {
        pCnt->incCnt();
    }
    cout<<"A:"<<pCnt->getCnt()<<endl;
}

void *threadRunB(void *)
{
    cout<<"start threadRunB==>"<<endl;
    usleep(200000);
    int cnts = 10000000;
    while (cnts--) {
        pCnt->incCnt();
    }
    cout<<"B:"<<pCnt->getCnt()<<endl;
}

int main()
{
    pCnt = new Counter();

    pthread_t tidA;
    pthread_t tidB;
    pthread_create(&tidA, NULL, threadRunA, NULL);
    cout<<"create thread-A OK!!!"<<endl;
    pthread_create(&tidB, NULL, threadRunB, NULL);
    cout<<"create thread-B OK!!!"<<endl;
    while (1) {
        // do nothing
    };
    return 0;
}

编译链接:

[jiang@eb50 44]$ g++ -o main main.C Counter.C -lpthread
[jiang@eb50 44]$ ./main
create thread-A OK!!!
create thread-B OK!!!
start threadRunB==>
start threadRunA==>
A:10830472
B:11699219CTRL+C)
[jiang@eb50 44]$ 

看到了吧?结果并不是20000000.

多次运行看看:

[jiang@eb50 44]$ ./main
create thread-A OK!!!
create thread-B OK!!!
start threadRunB==>
start threadRunA==>
A:11059743
B:12086821

[jiang@eb50 44]$ ./main
create thread-A OK!!!
create thread-B OK!!!
start threadRunA==>
start threadRunB==>
A:10721130
B:12083639

可见由于没有对Counter pCnt指向的对象进行同步,导致结果错误.

本例主要用于示范错误,关于为什么错误(时序、操作系统调度切换时间片等造成)请参阅其他资料.

如何解决?加锁.

Counter.h

#ifndef _COUNTER_H
#define _COUNTER_H

#include <pthread.h>

class Counter {
public:
    Counter();
    Counter(int);
    virtual ~Counter();
    int getCnt();
    void setCnt(int);
    void incCnt();
private:
    int mCnt;
    pthread_mutex_t mutex;
};

#endif

Counter.C

#include "Counter.h"

Counter::Counter()
{
    mCnt = 0;
    pthread_mutex_init(&mutex, NULL);
}

Counter::~Counter()
{
    pthread_mutex_destroy(&mutex);
}

int Counter::getCnt()
{ return mCnt; }

void Counter::setCnt(int cnt)
{ mCnt = cnt; }

void Counter::incCnt()
{
    pthread_mutex_lock(&mutex);
    mCnt = mCnt + 1;
    pthread_mutex_unlock(&mutex);
}
#include <iostream>
#include <pthread.h>
#include "Counter.h"

using namespace std;

static Counter *pCnt = NULL;

void *threadRunA(void *)
{
    cout<<"start threadRunA==>"<<endl;
    usleep(200000);
    int cnts = 10000000;
    while (cnts--) {
        pCnt->incCnt();
    }
    cout<<"A:"<<pCnt->getCnt()<<endl;
}

void *threadRunB(void *)
{
    cout<<"start threadRunB==>"<<endl;
    usleep(200000);
    int cnts = 10000000;
    while (cnts--) {
        pCnt->incCnt();
    }
    cout<<"B:"<<pCnt->getCnt()<<endl;
}

int main()
{
    pCnt = new Counter();

    pthread_t tidA;
    pthread_t tidB;
    pthread_create(&tidA, NULL, threadRunA, NULL);
    cout<<"create thread-A OK!!!"<<endl;
    pthread_create(&tidB, NULL, threadRunB, NULL);
    cout<<"create thread-B OK!!!"<<endl;
    while (1) {
        // do nothing
    };
    return 0;
}

编译链接:

g++ -o main main.C Counter.C -lpthread

运行:

[jiang@eb50 44]$ ./main
create thread-A OK!!!
create thread-B OK!!!
start threadRunA==>
start threadRunB==>
A:18687418
B:20000000

[jiang@eb50 44]$ vi main.C 
[jiang@eb50 44]$ ./main
create thread-A OK!!!
create thread-B OK!!!
start threadRunB==>
start threadRunA==>
B:17590204
A:20000000

咦,看起来还是不对呀?并不是,这里是正确的。

有可能存在A或者B先做完inc运算,然后就回先输出那个时刻的值,然后剩余的那个把剩下的做完,刚好是20000000.

一定要注意一点,是在inc的时候出现的错误,对i加一不是一个原子操作,可能在i=i+1时进行了多次的时间片切换,导致错误.

只需要在i++的时候注意加锁即可.保证i++是在critical section中,不会存在race condition.

噢,说个题外话:

一条c/c++语句不是原子的,那么一条汇编语句是原子的吗?

答案应该是“不是”.

汇编指令也也只是在描述CPU的行为,而没有具体到每一个细节步骤。

操作是否是原子的,不是由汇编指令决定的,而是由CPU如何处理这些指令决定的。

(资料来源:知乎某答主)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值