将 printf 语句移动到“计数”变量的递增/递减下方时出现奇怪的线程行为

问题描述 投票:0回答:1

我想做的是启动 2 个线程,在条件变量和唯一锁的帮助下,一个将共享资源递增 1,另一个将共享资源递减 1。我对使用线程很陌生(我昨天开始使用,但我不知道发生了什么)。 在 func1 和 func2 中,当您注释 printf 或将其移至 count++ 或 count-- 之上时,程序将在第 49 次迭代(即 i=48)处停止。

这里是完整的代码:

#include <iostream>
#include <thread>
#include <mutex>
#include <chrono>
#include<condition_variable>

using namespace std;
int count = 0;
mutex lock1;
condition_variable c1;
int resource;
void func1()
{
    for(int i = 0; i < 50; i++)
    {
//        lock1.lock();
        unique_lock <mutex> lk1(lock1);

        if(count == 0)
        {
//            cout<<"thread 1 is working on resource: "<<resource<<" i: "<<i<<"\n";
            printf("\nthread 1 is working on resource: %d and i: %d", resource, i);
            resource++;
//            cout<<"thread 1 work completed, new value: "<<resource<<"\n";
            printf("\nthread 1 work completed, new value: %d and i: %d", resource, i);
            c1.notify_one();
            lk1.unlock();
//            printf("\nvalue of count before incr %d", count);
            count++;
            printf("\nvalue of count %d", count);
//            cout<<"count while exec for t1: "<<count<<endl;
        }
        else
        {
            c1.wait(lk1);
//            cout<<"count while waiting for t1: "<<count<<endl;
        }
    }
}

void func2()
{
    for(int i = 0; i < 50; i++)
    {
//        lock1.lock();
        unique_lock <mutex> lk1(lock1);
        if(count == 1)
        {
//            cout<<"thread 2 is working on resource: "<<resource<<" i: "<<i<<"\n";
            printf("\nthread 2 is working on resource: %d and i: %d", resource, i);
            resource--;
//            cout<<"thread 2 work completed, new value: "<<resource<<"\n";
            printf("\nthread 2 work completed, new value: %d and i: %d", resource, i);
            c1.notify_one();
            lk1.unlock();
//            printf("\nvalue of count before decr %d", count);
            count--;
            *printf("\nvalue of count %d", count);*
//            cout<<"count while exec for t2: "<<count<<endl;
        }
        else
        {
            c1.wait(lk1);
//            cout<<"count while waiting for t2: "<<count<<endl;
        }
    }
}
int main()
{
    auto start = chrono::high_resolution_clock::now();
    thread worker1(func1);
    thread worker2(func2);
    cout<<"\nthread started"<<endl;
    worker1.join();
    worker2.join();
    cout<<"\nthread completed"<<endl;
    auto end = chrono::high_resolution_clock::now();
    auto duration = chrono::duration_cast<chrono::milliseconds>(end - start);
    cout<<duration.count();
}

我不知道为什么 printf 的位置作为程序运行能力的枢纽,我写它只是为了通过在每一步打印 count 的值然后程序来查看为什么程序没有完全执行完全神奇地执行。抱歉,如果问题是由极其微不足道的事情引起的,请将其归咎于我留下来完成所有工作的 2 个神经元(一次只有 2 个线程,哈哈)。

c++ multithreading thread-safety stdthread
1个回答
1
投票
            lk1.unlock();
            count++;

两个执行线程都有相同的代码。两个执行线程释放

lock1
互斥并更新
count
没有适当的同步,没有持有互斥锁。这是未定义的行为。您不能期望从显示的程序中获得任何特定结果。

© www.soinside.com 2019 - 2024. All rights reserved.