gpt4 book ai didi

c++ - 结构的互锁交换

转载 作者:可可西里 更新时间:2023-11-01 13:25:44 25 4
gpt4 key购买 nike

我想使用 WinAPI 中的 InterlockedExchange 来使用线程的无锁同步。
目前我有这样的类(class)。

struct DataExchange
{
volatile LONG m_value;
void SetValue(LONG newVal)
{
InterlockedExchange(&m_value, newVal);
}

LONG GetValue()
{
LONG workVal=0;
InterlockedExchange(&workVal, m_value);
return workVal;
}
};

一个线程可以设置一个新值,另一个线程可以读取这个值。
现在我要做的是将 LONG 值更改为 struct。在 WinAPI 中有什么方法可以复制 struct 无锁?

最佳答案

不,除非您可以将您的结构放入 32 位,在这种情况下您可以继续使用 InterlockedExchange。

关于c++ - 结构的互锁交换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6109810/

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