线程同步和中止
我在结束其中一个线程的工作时遇到了一个小问题。首先,这是应用程序的“布局”:
线程 1 - 工作线程 (C++/CLI) - 按预期运行和终止
for(...)
{
try
{
if(TabuStop) return;
System::Threading::Monitor::Enter("Lock1");
//some work, unmanaged code
}
finally
{
if(stop)
{
System::Threading::Monitor::Pulse("Lock1");
}
else
{
System::Threading::Monitor::Pulse("Lock1");
System::Threading::Monitor::Wait("Lock1");
}
}
}
线程 2 - 显示结果线程 (C#)
while (WorkerThread.IsAlive)
{
lock ("Lock1")
{
if (TabuEngine.TabuStop)
{
Monitor.Pulse("Lock1");
}
else
{
Dispatcher.BeginInvoke(RefreshAction);
Monitor.Pulse("Lock1");
Monitor.Wait("Lock1", 5000);
}
}
// Thread.Sleep(5000);
}
我正在尝试从应用程序主线程关闭整个程序像这样:
TabuEngine.TabuStop = true; //terminates nicely the worker thread and
if (DisplayThread.IsAlive)
{
DisplayThread.Abort();
}
我也尝试使用 DisplayThread.Interrupt,但它总是阻塞在 Monitor.Wait("Lock1", 5000); 上我无法摆脱它。这里有什么问题吗?我应该如何执行同步并让它完成它应该做的工作?
//编辑 我现在甚至不确定使用“Lock1”字符串的技巧是否真的有效并且锁被放置在同一个对象上。
I've got a little problem with ending the work of one of my threads. First things first so here's the app "layout":
Thread 1 - worker thread (C++/CLI) - runs and terminates as expected
for(...)
{
try
{
if(TabuStop) return;
System::Threading::Monitor::Enter("Lock1");
//some work, unmanaged code
}
finally
{
if(stop)
{
System::Threading::Monitor::Pulse("Lock1");
}
else
{
System::Threading::Monitor::Pulse("Lock1");
System::Threading::Monitor::Wait("Lock1");
}
}
}
Thread 2 - display results thread (C#)
while (WorkerThread.IsAlive)
{
lock ("Lock1")
{
if (TabuEngine.TabuStop)
{
Monitor.Pulse("Lock1");
}
else
{
Dispatcher.BeginInvoke(RefreshAction);
Monitor.Pulse("Lock1");
Monitor.Wait("Lock1", 5000);
}
}
// Thread.Sleep(5000);
}
I'm trying to shut the whole thing down from app main thread like this:
TabuEngine.TabuStop = true; //terminates nicely the worker thread and
if (DisplayThread.IsAlive)
{
DisplayThread.Abort();
}
I also tried using DisplayThread.Interrupt, but it always blocks on Monitor.Wait("Lock1", 5000); and I can't get rid of it. What is wrong here? How am I supposed to perform the synchronization and let it do the work that it is supposed to do?
//edit
I'm not even sure now if the trick with using "Lock1" string is really working and locks are placed on the same object..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用监视器进行生产者/消费者同步的一个很好的示例,您可以在 MSDN(示例 2)。
有两个线程(生产者和消费者,与您的情况类似),但同步是通过引入锁定共享资源的第三类来完成的。示例提供了完整的源代码,所以我没有将其发布在这里。
A nice example of producer / consumer synchronization using Monitors you can find on MSDN (Example 2).
There are two threads (producer and consumer, similar like in your case), but synchronization is done by introducing third class which locks shared resources. Example provides full source code, so I didn't post it here.
这些是监视器,而不是自动重置或手动重置事件。您需要一个条件来检查才能正确使用等待。否则,如果您在开始等待之前
Pulse
,您将错过Pulse
。一般来说,该模式如下所示:线程 A:
线程 B:
通过在持有锁的情况下操作和检查 some_condition,我们确保无论脉冲何时发生(无论是在 A 中开始等待之前还是之后),A 始终可以做出适当的反应,而不是永远等待已经到来的脉搏。
These are monitors, not auto reset or manual reset events. You need a condition to check to properly use wait. Otherwise, if you
Pulse
before you start waiting you will miss the thePulse
. Generally the pattern looks like:Thread A:
Thread B:
By manipulating and checking some_condition with the lock held, we ensure that that no matter when the pulse happens (either before we start waiting in A or afterwards) A can always react appropriately and not wait forever for a pulse that already came.