【问题标题】:C++ "deque iterator not dereferencable" multithreading. WaitForSingleObject() lets the thread throughC++“deque iterator not dereferencable”多线程。 WaitForSingleObject() 让线程通过
【发布时间】:2017-09-28 04:56:00
【问题描述】:

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

当未设置事件 notfull 时,在 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");
}

【问题讨论】:

标签: c++ multithreading stack waitforsingleobject


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-10
    • 2012-11-09
    • 1970-01-01
    • 1970-01-01
    • 2013-06-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多