gpt4 book ai didi

windows - 简单的多线程互斥示例是不正确的

转载 作者:可可西里 更新时间:2023-11-01 11:21:35 24 4
gpt4 key购买 nike

我希望以随机顺序获得从 0 到 4 的数字,但相反,我有一些不同步的困惑

我做错了什么?

#include <iostream>
#include <windows.h>
#include <process.h>

using namespace std;

void addQuery(void *v );

HANDLE ghMutex;

int main()
{
HANDLE hs[5];
ghMutex = CreateMutex( NULL, FALSE, NULL);
for(int i=0; i<5; ++i)
{
hs[i] = (HANDLE)_beginthread(addQuery, 0, (void *)&i);
if (hs[i] == NULL)
{
printf("error\n"); return -1;
}
}

printf("WaitForMultipleObjects return: %d error: %d\n",
(DWORD)WaitForMultipleObjects(5, hs, TRUE, INFINITE), GetLastError());


return 0;
}

void addQuery(void *v )
{
int t = *((int*)v);
WaitForSingleObject(ghMutex, INFINITE);

cout << t << endl;

ReleaseMutex(ghMutex);
_endthread();
}

最佳答案

您必须在锁 读取和写入共享变量。您正在锁外读取它,从而使锁变得无关紧要。

但这还不够,因为您的共享变量是一个循环变量,您在没有锁保护的情况下写入该循环变量。一个更好的例子会像这样运行:

#include <iostream>
#include <windows.h>
#include <process.h>

using namespace std;

void addQuery(void *v );

HANDLE ghMutex;
int counter = 0;

int main()
{
HANDLE hs[5];
ghMutex = CreateMutex( NULL, FALSE, NULL);
for(int i=0; i<5; ++i)
{
hs[i] = (HANDLE)_beginthread(addQuery, 0, NULL);
if (hs[i] == NULL)
{
printf("error\n"); return -1;
}
}

printf("WaitForMultipleObjects return: %d error: %d\n",
(DWORD)WaitForMultipleObjects(5, hs, TRUE, INFINITE), GetLastError());


return 0;
}

void addQuery(void *v)
{
WaitForSingleObject(ghMutex, INFINITE);

cout << counter << endl;
counter++;

ReleaseMutex(ghMutex);
_endthread();
}

如果可以,请使用临界区而不是互斥锁,因为它们更易于使用且效率更高。但它们具有相同的语义,因为它们只保护锁定 block 内的代码。

注意:Jerry 指出了一些其他问题,但我主要关注高级 trheading 和序列化问题。

关于windows - 简单的多线程互斥示例是不正确的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7841357/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com