[英]Multi-threaded environment, other threads never get access to critical section
Right now I am stuck, here is what I'm trying to do: 现在我被困住了,这是我想做的事情:
At a certain point in the function (that all threads will call), there will be a Sleep(time) function, which makes the thread sleep, the function does not return. 在函数的某个点(所有线程都会调用),将有一个Sleep(time)函数,该函数使线程进入睡眠状态,该函数不会返回。 This is where I want another thread - thread 2, to access and use the function, modifying the global variable, until it sleeps, so thread 3 can access the function, ect...
我在这里希望另一个线程-线程2访问并使用该函数,修改全局变量,直到它休眠,以便线程3可以访问该函数,等等。
The "critical section" function which accesses the global variable has an unspecified access time, it is never the same. 访问全局变量的“临界区”功能具有未指定的访问时间,从不相同。
Initially, in main I call 最初,我主要打电话给
InitializeCriticalSection(&m_stCriticalSection);
What I attempted was, when this function is called, I immediately call 我尝试的是,当调用此函数时,我立即调用
EnterCriticalSection(&m_stCriticalSection);
I then modify global variables, ect, then before the Sleep(time) I call 然后,我在调用Sleep(time)之前修改全局变量ect
LeaveCriticalSection(&m_stCriticalSection);
Problem with this is, the other threads don't EVER get access to the function, even though I leave it. 问题是,即使我离开了,其他线程也永远无法访问该函数。 Is there a way for my other threads to continuously, or even every 5 seconds, get access to the critical section?
有没有办法让我的其他线程连续甚至每5秒访问一次关键部分? Could my implementation be better?
我的实现会更好吗?
So here's what I have now 所以这就是我现在所拥有的
void function() // all our threads will access this
{
EnterCriticalSection(&obj)
// manipulate global data
LeaveCriticalSection(&obj)
Sleep(long time) // another thread SHOULD NOW have access to this section!
return true;
}
Is there any possibility that a process is failing in the "// manipulate global data" section? “ //操作全局数据”部分中的进程是否有可能失败?
If so, then LeaveCriticalSection(&obj) would never be called. 如果是这样,则将永远不会调用LeaveCriticalSection(&obj)。
Have you tried putting debug logs right before LeaveCriticalSection(&obj)? 您是否尝试过将调试日志放在LeaveCriticalSection(&obj)之前?
I think, it's more errorprone to use scoped-helpers which help you agains situation like 'some exception occurs after EnterCriticalSection() and before LeaveCriticalSection() so that LeaveCriticalSection() really never happens'. 我认为,使用作用域范围内的帮助程序更容易出错,它可以再次帮助您解决诸如“在EnterCriticalSection()之后和LeaveCriticalSection()之前发生某些异常,从而使LeaveCriticalSection()实际上永远不会发生”的情况。
You could do some wrapper (see above) around CS with some trace. 您可以围绕CS做一些包装(见上文),并进行一些跟踪。
Also, I assume that it would be easy to collect the application dump and see via WinDbg current thread state & cs state. 另外,我认为很容易收集应用程序转储并通过WinDbg查看当前线程状态和CS状态。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.