VC++线程死锁

发布于 2024-10-18 08:50:58 字数 2260 浏览 0 评论 0原文

以下程序陷入死锁。谁能告诉我为什么吗?

#include<cstdlib>
#include<windows.h>
#include<iostream>
using namespace std;

class CircularQueue
{
public:
    CircularQueue(int s)
    {
        size = s;
        array = (int*)malloc(sizeof(int) * size);
        head = tail = -1;
        InitializeCriticalSection(&critical_section);
    }

    CircularQueue(void)
    {
        size = default_size;
        array = (int*)malloc(sizeof(int) * size);
        head = tail = -1;
        InitializeCriticalSection(&critical_section);
    }

    void initialize(int s)
    {
        EnterCriticalSection(&critical_section);

        size = s;
        array = (int*)realloc(array, sizeof(int) * size);
        head = tail = -1;

        LeaveCriticalSection(&critical_section);
    }

    void enqueue(int n)
    {
        EnterCriticalSection(&critical_section);

        tail = (tail + 1) % size;
        array[tail] = n;

        LeaveCriticalSection(&critical_section);
    }

    int dequeue(void)
    {
        EnterCriticalSection(&critical_section);

        head = (head + 1) % size;
        return array[head];

        LeaveCriticalSection(&critical_section);
    }

private:
    int *array;
    int size;
    int head, tail;
    CRITICAL_SECTION critical_section;
    bool initialized;
    static const int default_size = 10;
};

DWORD WINAPI thread1(LPVOID param)
{
    CircularQueue* cqueue = (CircularQueue*)param;
    cqueue->enqueue(2);
    cout << cqueue->dequeue() << endl;

    return 0;
}

DWORD WINAPI thread2(LPVOID param)
{
    CircularQueue* cqueue = (CircularQueue*)param;
    cqueue->enqueue(3);
    cout << cqueue->dequeue() << endl;

    return 0;
}

int main(void)
{
    HANDLE thread1_handle;
    HANDLE thread2_handle;

    CircularQueue cqueue;
    HANDLE array[2];

    thread1_handle = CreateThread(NULL, 0, thread1, &cqueue, 0, NULL);

    thread2_handle = CreateThread(NULL, 0, thread2, &cqueue, 0, NULL);

    array[0] = thread1_handle;
    array[1] = thread2_handle;

    WaitForMultipleObjects(1, array, TRUE, INFINITE);

    CloseHandle(thread1_handle);
    CloseHandle(thread2_handle);

    printf("end\n");
    return 0;
}

The following program goes into a deadlock. Can anyone please tell me why?

#include<cstdlib>
#include<windows.h>
#include<iostream>
using namespace std;

class CircularQueue
{
public:
    CircularQueue(int s)
    {
        size = s;
        array = (int*)malloc(sizeof(int) * size);
        head = tail = -1;
        InitializeCriticalSection(&critical_section);
    }

    CircularQueue(void)
    {
        size = default_size;
        array = (int*)malloc(sizeof(int) * size);
        head = tail = -1;
        InitializeCriticalSection(&critical_section);
    }

    void initialize(int s)
    {
        EnterCriticalSection(&critical_section);

        size = s;
        array = (int*)realloc(array, sizeof(int) * size);
        head = tail = -1;

        LeaveCriticalSection(&critical_section);
    }

    void enqueue(int n)
    {
        EnterCriticalSection(&critical_section);

        tail = (tail + 1) % size;
        array[tail] = n;

        LeaveCriticalSection(&critical_section);
    }

    int dequeue(void)
    {
        EnterCriticalSection(&critical_section);

        head = (head + 1) % size;
        return array[head];

        LeaveCriticalSection(&critical_section);
    }

private:
    int *array;
    int size;
    int head, tail;
    CRITICAL_SECTION critical_section;
    bool initialized;
    static const int default_size = 10;
};

DWORD WINAPI thread1(LPVOID param)
{
    CircularQueue* cqueue = (CircularQueue*)param;
    cqueue->enqueue(2);
    cout << cqueue->dequeue() << endl;

    return 0;
}

DWORD WINAPI thread2(LPVOID param)
{
    CircularQueue* cqueue = (CircularQueue*)param;
    cqueue->enqueue(3);
    cout << cqueue->dequeue() << endl;

    return 0;
}

int main(void)
{
    HANDLE thread1_handle;
    HANDLE thread2_handle;

    CircularQueue cqueue;
    HANDLE array[2];

    thread1_handle = CreateThread(NULL, 0, thread1, &cqueue, 0, NULL);

    thread2_handle = CreateThread(NULL, 0, thread2, &cqueue, 0, NULL);

    array[0] = thread1_handle;
    array[1] = thread2_handle;

    WaitForMultipleObjects(1, array, TRUE, INFINITE);

    CloseHandle(thread1_handle);
    CloseHandle(thread2_handle);

    printf("end\n");
    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

摇划花蜜的午后 2024-10-25 08:50:58

dequeue() 中,在 LeaveCriticalSection() 调用之前有一个 return 语句。如果您的编译器警告调得更高,它可能会告诉您这一点!

In dequeue(), you have a return statement before the LeaveCriticalSection() call. If you had compiler warnings turned up higher, it would probably have told you about this!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文