c++ - CreateThread 的 threadProc 竞争条件

标签 c++ multithreading thread-safety

我需要产生 4 个线程,它们基本上做同样的事情,但每个线程有不同的变量。所以我调用了::CreateThread 4 次,将相同的 threadProc 和“this”作为参数。现在在 threadProc 中,我需要选择要使用的正确变量。我有一个对象 vector ,我在每次调用 CreateThread 后立即将对象插入其中。

// at this point myVec has say, 2 items
HANDLE hThread = ::CreateThread( NULL, NULL, threadProc, (LPVOID)this, NULL, NULL );
myVecObj.threadHandle = hThread;
myVec.push_back(myVecObj); // myVec.Size = 3 now

DWORD CALLBACK myClass::threadProc(LPVOID lpContext)
{
     myClass *pMyClass = (myClass *)lpContext;
     int vecCount = pMyClass->myVec.size; // Is this 3??
     char * whatINeed = (char*)pMyClass->myVec[vecCount-1].whatINeed;
}

我的疑虑/问题是 threadProc 触发的速度有多快 - 它能否击败对 myVec.push_back() 的调用?这是我在这里介绍的竞争条件吗?我试图假设当每个 threadProc 启动时(它们在不同的时间启动,而不是一个接一个地启动),我可以安全地获取类 vector 中的最后一个对象。

最佳答案

I need to spawn 4 threads, that basically do the same thing but with a different variable each. So I call ::CreateThread 4 times, giving the same threadProc and this as a parameter.

Now in threadProc, I need to pick the right variable to work with.

为什么不向线程传递一个指向它需要操作的实际对象的指针?

I have a vector of objects, and I push into it the object immediately after each of the CreateThread call.

这是错误的处理方式。是的,这是一种竞争条件。不仅因为显而易见的原因——线程可能在对象被推送之前开始运行——而且还因为 任何 推送到 vector 中都可能重新分配 vector 的内部数组,这对线程来说是非常糟糕的已经在 vector 中获得了指向其数据的指针。数据将在线程背后的内存中移动。

要解决这个问题,您需要:

  1. 首先将所有对象插入 vector ,然后启动您的线程。您可以将指向每个 vector 元素的指针传递给其各自的线程。但是,出于上述原因,只有当任何线程正在运行时您不再修改 vector 时,这才有效。

  2. 首先以挂起状态启动线程,然后在将所有对象插入 vector 后恢复它们。这也要求您不再修改 vector 。这也意味着您必须向每个线程传递一个 vector 元素的索引,而不是向它传递一个指向该元素的指针。

  3. 完全摆脱 vector (或至少将其更改为保存对象指针而不是实际对象)。使用 new 动态分配您的对象,并根据需要将这些指针传递给每个线程(并可选择传递给 vector )。让每个线程在退出前删除它的对象(并可选择将其从 vector 中删除,并进行适当的同步)。

My doubt/question is how fast does the threadProc fire

这完全取决于操作系统调度程序来决定。

could it beat the call to myVec.push_back()?

是的,这是可能的。

Is this a race condition that I'm introducing here?

是的。

I'm trying to make the assumption

不要做假设!

that when each threadProc starts (they start at different times, not one after the other), I can safely take the last object in the class' vector.

这不是一个安全的假设。

关于c++ - CreateThread 的 threadProc 竞争条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51886427/

相关文章:

Java 线程同时递增和递减 int

c++ - 适用于 PostgreSQL 的优秀 C/C++ 连接器库

c++ - 生成自动变灰的makefile - eclipse

android - 永远运行线程+在android中更新用户界面

c# - 如何暂停创建新线程直到旧线程结束

c++ - C++ 标准库如何保证避免数据竞争?

c# - 如何使类线程安全

c++ - 将二进制对象读/写为十六进制?

c++ - 在 C++ 中将结构内的数组归零

ruby-on-rails - Ruby on Rails 同时发出多个 HTTP 请求?