简体   繁体   English

多线程环境,其他线程永远无法访问关键部分

[英]Multi-threaded environment, other threads never get access to critical section

Right now I am stuck, here is what I'm trying to do: 现在我被困住了,这是我想做的事情:

  • Create 3 threads: 创建3个线程:
  • These 3 threads will access a shared global resource (a vector) 这3个线程将访问共享的全局资源(向量)

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.

相关问题 如何为多线程客户端访问多个线程上的相同套接字 - How to access same socket on multiple threads for multi-threaded client 在多线程环境中测量墙时间 - measuring wall time in a multi-threaded environment 这将在多线程环境中互不干扰吗? - Will this be a non-interference in a multi-threaded environment? 控制多线程应用程序中对输出的访问 - Controlling access to output in multi-threaded applications 具有3个线程的多线程程序,可打印数字序列 - Multi-threaded program with 3 threads that prints sequence of numbers 在多线程程序中禁用 boost::asio 上的线程是否安全? - Is it safe to disable threads on boost::asio in a multi-threaded program? 在多线程环境中使用MySQL C ++ Connector - Use of MySQL C++ Connector in multi-threaded environment 关于在多线程环境中使用静态const变量 - On the use of static const variable in multi-threaded environment 多线程环境中带有 shared_ptr 的 LRU - LRU with shared_ptr in multi-threaded environment 为什么使用全局变量会使多线程执行速度减慢 2 倍,而在其他环境中却使其速度加快 2 倍? - Why using global variabes makes the multi-threaded execution 2x slower while in other environment it makes it 2x faster?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM