使用C ++中的线程交替打印奇数和偶数打印 [英] printing odd and even number printing alternately using threads in C++

查看:277
本文介绍了使用C ++中的线程交替打印奇数和偶数打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用线程进行奇数偶数打印我遇到了这个问题,讨论C ++中的解决方案。我可以想到使用2个二进制信号量奇偶信号量。偶数信号量初始化为1,奇数初始化为0.

Odd even number printing using thread I came across this question and wanted to discuss solution in C++ . What I can think of using 2 binary semaphores odd and even semaphore. even semaphore initialized to 1 and odd initialized to 0.

**T1 thread function** 
funOdd()
{  
  wait(even)  
  print odd;  
  signal(odd)  
}


**T2 thread function**
funEven()  
{  
  wait(odd)  
  print even  
  signal(even)  
}  

除此之外,如果我的功能只产生数字,并有一个第三线程T3将打印这些数字,那么什么应该是理想的设计?我使用一个数组,其中奇数将被放置在奇数位置,偶数将被放置在偶数位置。 T3将从这个数组中读取,这将避免任何线程安全在这个数组,如果T3没有找到任何索引,然后它会等待,直到索引填充。另一个解决方案可以是使用一个队列,它将有一个互斥,可以在插入时由T1和T2使用。

In addition to this if my functions are generating only number and there is a third thread T3 which is going to print those numbers then what should be ideal design ? I used an array where odd number will be placed at odd place and even number will be place at even position. T3 will read from this array this will avoid any thread saftey over this array and if T3 does not find any index then it will wait till that index gets populated. Another solution can be to use a queue which will have a mutex which can be used by T1 and T2 while insertion.

请评论这个解决方案,以及如何使它更有效率。

Please comment on this solution and how can i make it more efficient.

编辑以使问题更清楚:总体问题是我有两个生产者(T1,T2)和单个消费者我的生产者是相互依存的。

Edit to make problem much clear: Overall problem is that I have two producers (T1,T2) and a single consumer (T3), and my producers are interdependent.

推荐答案

解决方案基于C ++ 11关键代码部分aka mutex

Solution is based on C++11 critical code section aka mutex.

以下是工作代码,后面会有解释。

Here's the working code, followed by an explanation.

using namespace std;
#include <iostream>
#include <string>
#include <thread>
#include <mutex>

std::mutex mtx;

void oddAndEven(int n, int end);

int main()
{
std::thread odd(oddAndEven, 1, 10);
std::thread Even(oddAndEven, 2, 10);

odd.join();
Even.join();

return 0;
}



void oddAndEven(int n, int end){
int x = n;
for (; x < end;){
    mtx.lock();
    std::cout << n << " - " << x << endl;
    x += 2;
    mtx.unlock();
    std::this_thread::yield();
    continue;
 }
}



ie:



线程转到方法oddAndEven,起始号码为1,因此他是奇数。他是第一个获得 mtx.lock()的锁。

i.e:

Thread odd goes to method oddAndEven with starting number 1 thus he is the odd. He is the first to acquire the lock which is the mtx.lock().

> Even 尝试获取锁,但线程 odd 先获取它,以便线程等待。

Meanwhile, thread Even tries to acquire the lock too but thread odd acquired it first so thread Even waits.

回到线程 odd (具有锁定),他输出数字1并使用 mtx.unlock()释放锁定。此时,我们需要线程 Even 获取锁定并打印2,所以我们通过写 std :: this_thread :: yield()来通知线程。然后线程即使也是这样。

Back to thread odd (which has the lock), he prints the number 1 and releases the lock with mtx.unlock(). At this moment, we want thread Even to acquire lock and to print 2 so we notify thread Even by writing std::this_thread::yield(). Then thread Even does the same.

等等。

这篇关于使用C ++中的线程交替打印奇数和偶数打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆