我正在尝试模拟关于多线程的生产者-消费者模型。
我们假设要遵循三个规则:
现在我拥有的是
int
变量,用于存储存储桶中的产品数量const int
变量,在我的代码中其值为5。int
变量,其初始值为1 vector<HANDLE>
变量,并且将有一个函数调度这些暂停线程。结果是:有时它可以正常工作,但有时会陷入僵局。代码和结果如下:
代码:
#include <iostream>
#include <Windows.h>
#include <vector>
using namespace std;
// the count of product, the initial value is 0
int product_count = 0;
const int product_capacity = 5;
int mutex = 1;
vector<HANDLE> suspendedHandleVector;
HANDLE GetCurrentRealHandle() {
HANDLE realHandle = 0;
return OpenThread(THREAD_ALL_ACCESS, TRUE, GetCurrentThreadId());
}
void ThreadScheduling() {
if (suspendedHandleVector.size() > 0) {
HANDLE handle = suspendedHandleVector[0];
suspendedHandleVector.erase(suspendedHandleVector.begin());
ResumeThread(handle);
}
}
void P() {
--mutex;
if (mutex < 0) {
auto handle = GetCurrentRealHandle();
suspendedHandleVector.push_back(handle);
SuspendThread(handle);
}
}
void V() {
++mutex;
if (mutex >= 0) {
ThreadScheduling();
}
}
DWORD WINAPI ProducerThread(LPVOID param) {
while (true) {
P();
if (product_count == product_capacity) {
V();
continue;
}
++product_count;
cout << "I'm producer, and there are " << product_count << " products now" << endl;
V();
Sleep(100);
}
return 0;
}
DWORD WINAPI ConsumerThread(LPVOID param) {
while (true) {
P();
if (product_count == 0) {
V();
continue;
}
--product_count;
cout << "I'm consumer, and there are " << product_count << " products rest now" << endl;
V();
Sleep(150);
}
return 0;
}
void main() {
auto producer_handle = CreateThread(nullptr, 0, ProducerThread, nullptr, 0, nullptr);
auto consumer_handle = CreateThread(nullptr, 0, ConsumerThread, nullptr, 0, nullptr);
while (true) {
cout << suspendedHandleVector.size() << endl; // This is for debugging
Sleep(100);
}
}
按预期工作时的结果:
0
I'm producer, and there are 1 products now
I'm consumer, and there are 0 products rest now
0
I'm producer, and there are 1 products now
I'm consumer, and there are 0 products rest now
0
I'm producer, and there are 1 products now
0
I'm consumer, and there are 0 products rest now
I'm producer, and there are 1 products now
0
I'm producer, and there are 2 products now
I'm consumer, and there are 1 products rest now
这是一个预期的无限循环。产品数量将分别为4和5,因为我将生产者设置为比消费者多睡些时间。
但这是意外的结果:
I'm producer, and there are 5 products now
I'm consumer, and there are 4 products rest now
0
I'm consumer, and there are 4 products rest now
I'm producer, and there are 5 products now
0
0
I'm consumer, and there are 4 products rest now
I'm producer, and there are 5 products now
0
2
2
2
2
2
2
我们看到,挂起的线程向量的大小从0达到2,而省略了1。
这是两个线程在同一时间被唤醒然后发生冲突的巧合吗?
我认为我的代码有更多问题,需要您的帮助。
在测试过程中我也遇到了一个问题:如何获取线程,然后将其存储在向量中。
我不确定要使用OpenThread
来执行此操作。我的课程要求我使用系统调用,因此我不包括thread
头文件。
谢谢您的帮助!
答案 0 :(得分:0)
在生产者-消费者问题中,有三个参与者:生产者,消费者和用于存储生产数据的缓冲区。生产者和消费者通常生活在一个或多个线程中(多生产者单消费者单生产者多消费者多生产者多消费者)。
现在这种模式非常有用,因为它是避免显式同步的一种方法。
正如我所说,消费者和消费者通常生活在一个或多个线程中。缓冲区通常是一个同步队列(可以是锁定队列或无锁定)。非常简短的代码段:
SyncronizedQueue queue;
void consumer()
{
while(1) {
queue.push(std::rand());
}
}
void producer()
{
while(1)
{
auto product = queue.pop(); // blocks until there are elements in the
}
}
void main()
{
std::thread producerThread{producer};
std::thread consumerThread{consumer};
producerThread.join();
consumerThread.join();
}
因此,任务是编写队列,以便使用安全的push
元素,并在推送新元素时解锁pop
。
一种简单的实现方法是使用条件变量,以便pop
函数被阻塞,直到触发条件为止,而push方法将元素添加到队列中并触发条件。