簡體   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