繁体   English   中英

C ++“不可取消deque迭代器”多线程。 WaitForSingleObject()使线程通过

[英]C++ “deque iterator not dereferencable” multithreading. WaitForSingleObject() lets the thread through

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

未设置事件不完整时,在Push()中调用WaitForSingleObject()时相同。

在那里你可以看到它

在Microsoft VS RC 2017中工作且未设置字符集。

我不知道该如何工作(等待无限时间推送/弹出某物)。

   #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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM