导致生产者/消费者堆栈中的竞争状态
Cause of race condition in producer/consumer stack
我尝试创建一个基于通知事件的生产者-消费者堆栈,这将允许单个线程推送数据,另一个线程弹出数据。
当缓冲区满或空时,一个线程等待另一个线程,直到它能够继续。
我正在检测一个竞态条件(程序在我标记***ERROR HERE***
的地方中断),但我不明白为什么会发生。
在这个程序中size
怎么能比capacity
高?
#include <process.h>
#include <cstdlib>
#include <vector>
#include <windows.h>
template<typename T, typename Ax = std::allocator<T> >
class rwstack
{
// It is assumed that only ONE thread will push data
// and only ONE thread will pop data.
public:
typedef T value_type;
typedef Ax allocator_type;
typedef rwstack<value_type, allocator_type> this_type;
typedef std::vector<value_type, allocator_type> container_type;
private:
allocator_type allocator;
value_type *items;
size_t volatile count;
size_t const capacity;
HANDLE hEventNotEmpty, hEventNotFull;
rwstack(const this_type &other) { __debugbreak(); /*Don't allow*/ }
public:
rwstack(const size_t capacity = 4096)
: allocator(allocator_type()),
items(allocator.allocate(capacity, NULL)),
count(0), capacity(capacity),
hEventNotEmpty(CreateEvent(NULL, TRUE, FALSE, NULL)),
hEventNotFull(CreateEvent(NULL, TRUE, TRUE, NULL)) { }
virtual ~rwstack() // Not actually used in the example
{
CloseHandle(hEventNotEmpty);
CloseHandle(hEventNotFull);
for (size_t i = 0; i < count; i++)
{ allocator.destroy(&items[InterlockedDecrementSizeT(&count) - i]); }
allocator.deallocate(items, capacity);
}
value_type &push(const value_type &value)
{
const ULONG waitResult = WaitForSingleObject(hEventNotFull, INFINITE);
if (waitResult != WAIT_OBJECT_0) { __debugbreak(); }
const size_t newSize = InterlockedIncrementSizeT(&count);
try
{
if (newSize > capacity) { __debugbreak(); } // ****ERROR HERE****
if (newSize >= capacity) { ResetEvent(hEventNotFull); }
allocator.construct(&items[newSize - 1], value);
SetEvent(hEventNotEmpty);
return items[newSize - 1];
}
catch (...) { InterlockedDecrementSizeT(&count); throw; }
}
void pop(value_type *pValue = NULL)
{
const ULONG waitResult = WaitForSingleObject(hEventNotEmpty, INFINITE);
if (waitResult != WAIT_OBJECT_0) { __debugbreak(); }
const size_t newSize = InterlockedDecrementSizeT(&count);
try
{
if (newSize > capacity) { __debugbreak(); } // ****ERROR HERE****
if (newSize <= 0) { ResetEvent(hEventNotEmpty); }
if (pValue != NULL) { *pValue = items[newSize]; }
allocator.destroy(&items[newSize]);
SetEvent(hEventNotFull);
}
catch (...) { InterlockedIncrementSizeT(&count); throw; }
}
};
static size_t InterlockedIncrementSizeT(size_t volatile *p)
{
#if _M_X64
return InterlockedIncrement64(reinterpret_cast<long long volatile *>(p));
#elif _M_IX86
return InterlockedIncrement(reinterpret_cast<long volatile *>(p));
#endif
}
static size_t InterlockedDecrementSizeT(size_t volatile *p)
{
#if _M_X64
return InterlockedDecrement64(reinterpret_cast<long long volatile *>(p));
#elif _M_IX86
return InterlockedDecrement(reinterpret_cast<long volatile *>(p));
#endif
}
测试代码:
typedef rwstack<int> TTestStack;
void __cdecl testPush(void *context)
{
TTestStack::value_type v;
for (;;)
static_cast<TTestStack *>(context)->pop(&v);
}
void __cdecl testPop(void *context)
{
for (TTestStack::value_type v = 0; ; v++)
static_cast<TTestStack *>(context)->push(v);
}
int main()
{
TTestStack rw;
HANDLE hThreads[2] = {
reinterpret_cast<HANDLE>(_beginthread(&testPush, 0, &rw)),
reinterpret_cast<HANDLE>(_beginthread(&testPop, 0, &rw)),
};
const ULONG nThreads = sizeof(hThreads) / sizeof(*hThreads)
WaitForMultipleObjects(nThreads, hThreads, TRUE, INFINITE);
return 0;
}
没有锁定正确的操作
这里的关键是,当您在线程A中禁用hEventNotFull
事件时,您也在线程b中启用它。
线程并发运行
下面是发生的事情:
-
队列已满,为4096项
-
线程B获得锁并将计数减为4095。在决定是否启用
hEventNotFull
之前,您需要持有此锁,但是您会立即释放它。操作系统暂停线程B。 -
线程A获得锁并将count增加到4096。你需要持有这个锁,直到你决定是否重置
hEventNotFull
,但你立即释放它。 -
操作系统认为线程B比线程a更重要
-
所以你在线程A中调用
resetEvent
,然后在线程b中调用SetEvent
,最终结果是你将返回到线程A中执行,计数== 4096。
执行流程:
Thread B: Get count and decrement it to 4095. # Queue not full
Thread A: Get count and increment it to 4096. # Queue full
Thread A: ResetEvent on `hEventNotFull` # A thinks it will block since queue is full
Thread B: SetEvent on `hEventNotFull` # B is using stale info and unblocks A
相关文章:
- Constexpr替代了新的放置方式,可以让内存中的对象保持未初始化状态
- 我不断收到 [错误] ID 返回 1 退出状态错误,但看不到问题所在
- OSX MetalKit CVMetalTextureCacheCreateTextureFromImage失败,状态:
- std::future_error:无关联状态
- 如何避免LED在循环状态变化中闪烁?
- boost 是否有按特殊类型值编码状态"compact optional"?
- compare_exchange C++函数如何确定竞争条件?
- 为什么系统函数总是在C++中返回已转移的退出状态?
- C++ 中的编译错误:未定义对"主"的引用 collect2:错误:ld 返回 1 个退出状态
- 当可输入框在窗口中处于活动状态时获得通知的任何方法
- 如何将可变参数模板转换为多个单个模板?(C++竞争编程调试模板)
- 检查两个节点在子节点上是否具有相同状态的更优雅的方法
- 将有状态的 lambda 传递到 C 样式函数中,而无需上下文参数
- 在 nullptr 上调用无状态类的非静态成员函数是否合法?
- 编译问题:在函数"_start"中:未定义对"主"的引用 collect2:错误:ld 返回 1 个退出状态
- C++部分概念 id:显式模板规范顺序/第一个参数的特殊状态的原因是什么?
- 试图在崇高中奔跑. 错误 已发生: 收集2.exe: 错误: ld 返回 1 退出状态
- 导致生产者/消费者堆栈中的竞争状态
- 在N3600的“锁存器”样品中是否存在竞争状态?
- Valgrind在不应该报告竞争状态时报告竞争状态