C++ "deque iterator not dereferencable" 多线程。 WaitForSingleObject() 让线程通过

C++ "deque iterator not dereferencable" multithreading. WaitForSingleObject() lets the thread through

当我在我的 Pop() 函数中调用 WaitForSingleObject() 时,即使未设置事件 notempty 它也不会停止它,我不知道如何处理它。 它应该等待一个数字被推送,即使它没有被推送,它仍然应该等待无限长的时间。

当未设置事件 notfull 时在 Push() 中调用 WaitForSingleObject() 时相同。

在未设置字符集的情况下在 Microsoft VS RC 2017 中工作。

我不知道如何让它工作(等待无限时间push/pop)。

   #include <windows.h>
#include <conio.h>
#include <iostream>
#include <string>
#include <fstream>
#include <time.h>
#include <stack>
using namespace std;
class MonitorStack {
public:
    CRITICAL_SECTION cs_pop;
    CRITICAL_SECTION cs_push;
    stack<int> item;
    int MAXSIZE;
    HANDLE notempty, notfull;
    MonitorStack(int _size) {
        InitializeCriticalSection(&cs_push);
        InitializeCriticalSection(&cs_pop);
        MAXSIZE = _size;
        notempty = CreateEvent(NULL,TRUE,FALSE, "notempty");
        notfull= CreateEvent(NULL, TRUE, TRUE, "notfull");
        SetEvent(notfull);
    }
    MonitorStack() {
        InitializeCriticalSection(&cs_push);
        InitializeCriticalSection(&cs_pop);
        int _size = 0;
        MAXSIZE = _size;
        notempty = CreateEvent(NULL, FALSE, FALSE, "notempty");
        notfull = CreateEvent(NULL, FALSE, TRUE, "notfull");
        SetEvent(notfull);
    }
    ~MonitorStack() {
        MAXSIZE = -1;
    }
    void  Push(int element){
                if (item.size() >= MAXSIZE) {
                    ResetEvent(notfull);
                }
                EnterCriticalSection(&cs_push);
                WaitForSingleObject(notfull, INFINITY);
                    item.push(element);
                    cout << "pushed " << element << endl;
                    SetEvent(notempty);
                    if (item.size() == MAXSIZE) {
                        ResetEvent(notfull);
                    }
                LeaveCriticalSection(&cs_pop);
    }
    int  Pop(){
                if (item.size() == 0) {
                    ResetEvent(notempty);
                }
                EnterCriticalSection(&cs_pop);
                WaitForSingleObject(notempty, INFINITY);
                int num = item.top();
                cout << "popped " << num << endl;
                item.pop();
                SetEvent(notfull);
                if (item.size() == 0) {
                    ResetEvent(notempty);
                }
                LeaveCriticalSection(&cs_push);
                return num;
    }
};
MonitorStack item;
DWORD WINAPI consumerFunction(LPVOID data)
{
    int* info = (int*)data;
    for (int i = 0; i < *info; i++) {
        item.Pop();
    }
    return 0;
}
DWORD WINAPI producerFunction(LPVOID data)
{
    int* info = (int*)data;
    int num;
    srand(time(NULL));
    for (int i = 0; i < *info; i++) {
        num = (rand() % 10 + 1);
        item.Push(num);
    }
    return 0;
}
void main() {
    DWORD  IDThread;
    int cap;
    cout << "enter the capacity of the stack" << endl;
    cin >> cap;
    int producer, consumer;
    cout << "enter the producer and consumer count!" << endl;
    cin >> producer >> consumer;
    HANDLE  *consumerThread = new HANDLE [consumer];
    HANDLE *producerThread = new HANDLE[producer];
    cout << "enter now for each producer the amount of items to produce" << endl;
    int *producercount = new int[producer];
    int *consumercount = new int[consumer];
    item = MonitorStack(cap);
    for (int i = 0; i < producer; i++)
    {
        cin >> producercount[i];
    }
    cout << "enter now for each consumer the amount of items to consume" << endl;
    for (int i = 0; i < consumer; i++)
    {
        cin >> consumercount[i];
    }
    for (int i = 0; i < producer; i++)
    {
        producerThread[i] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)producerFunction, (void*)&(producercount[i]), 0, &IDThread);
    }
    for (int i = 0; i < consumer; i++)
    {
        consumerThread[i] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)consumerFunction, (void*)&(consumercount[i]), 0, &IDThread);
    }
    WaitForMultipleObjects(producer, producerThread, TRUE, INFINITE);
    WaitForMultipleObjects(consumer, consumerThread, TRUE, INFINITE);
    for (int i = 0; i < producer; i++)
    {
        CloseHandle(producerThread[i]);
    }
    for (int i = 0; i < consumer; i++)
    {
        CloseHandle(consumerThread[i]);
    }
    system("pause");
}

您对 WaitForSingleObject 的调用使用 INFINITY 作为超时参数,应该使用 INFINITE.