一个线程应该向上计数,另一个线程向下计数。输出会有所不同,但通常可以很好地打印countUp()部分,然后在countDown()部分中存在一些缺陷。最初,我确定问题是两个线程都在争用cout资源。我认为这可以通过shared_print()和互斥锁解决。但是,输出仍然会变化,并且坦率地说,我并没有把这个并发/多线程处理的事情摆在脑后。我的主要问题是,是否有可能生成相同的输出,还是总会以某种方式变化?提前致谢。

mutex mu; 
int counter= 0; 
 
int main() 
{ 
    
    thread t1(countUp);     //t1 starts running 
    thread t2(countDown);   //t2 starts running 
    t1.join(); 
    t2.join(); 
} 
 
//Counts up from 0 to 20 
void countUp() 
{ 
    while(counter < 20) 
    { 
        counter++; 
        shared_print(string("Counting Up: "), counter); 
        //counter++; 
    } 
} 
 
//Counts down from 20 to 0 
void countDown() 
{ 
    
   while(counter > 0) 
   { 
        
       //counter--; 
       shared_print(string("Counting Down: "), counter); 
       counter--; 
   } 
} 
 
//Prints both of the counts 
void shared_print(string description, int number) 
{ 
    lock_guard<std::mutex> guard(mu); 
    //mu.lock(); 
    cout << description << number << endl; 
    //mu.unlock(); 
} 
输出与此不同:
Counting Up: 1 
Counting Up: 2 
Counting Up: 3 
Counting Up: 4 
      . 
      . 
      . 
Counting Up: 20 
为此(在倒数开始时,请注意用1代替20):
Counting Up: 1 
Counting Up: 2 
Counting Up: 3 
Counting Up: 4 
Counting Up: 5 
Counting Up: 6 
Counting Up: 7 
Counting Up: 8 
Counting Up: 9 
Counting Up: 10 
Counting Up: 11 
Counting Up: 12 
Counting Up: 13 
Counting Up: 14 
Counting Up: 15 
Counting Up: 16 
Counting Up: 17 
Counting Up: 18 
Counting Up: 19 
Counting Up: 20 
Counting Down: 1 
Counting Down: 19 
Counting Down: 18 
Counting Down: 17 
Counting Down: 16 
Counting Down: 15 
Counting Down: 14 
Counting Down: 13 
Counting Down: 12 
Counting Down: 11 
Counting Down: 10 
Counting Down: 9 
Counting Down: 8 
Counting Down: 7 
Counting Down: 6 
Counting Down: 5 
Counting Down: 4 
Counting Down: 3 
Counting Down: 2 
Counting Down: 1 

请您参考如下方法:

使用Condition变量等待,等待操作自动释放互斥量。

     mutex mu; 
        atomic<int> counter = 0; 
        condition_variable m_cvO; 
         
       //Counts up from 0 to 20 
void countUp() 
{ 
    unique_lock<mutex> lck(mu); 
    m_cvO.wait(lck, [&]() { 
        return counter < 20; 
        }); 
    while (counter < 20) 
    { 
        counter++; 
        shared_print(string("Counting Up: "), counter); 
        //counter++; 
    } 
    m_cvO.notify_all(); 
    
} 
     
       //Counts down from 20 to 0 
    void countDown() 
    { 
        unique_lock<mutex> lck(mu); 
        m_cvO.wait(lck, [&]() { 
            return counter > 0; 
            }); 
        while (counter > 0) 
        { 
     
            //counter--; 
            shared_print(string("Counting Down: "), counter); 
            counter--; 
        } 
        m_cvO.notify_all(); 
        
    } 
     
    //Prints both of the counts 
    void shared_print(string description, int number) 
    { 
       
        //mu.lock(); 
        cout << description << number << endl; 
        //mu.unlock(); 
    } 


评论关闭
IT序号网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!