简体   繁体   English

线程同步和中止

[英]Thread synchronization and aborting

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 线程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");
    }
  }
}

Thread 2 - display results thread (C#) 线程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);
        }

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); 我也尝试使用DisplayThread.Interrupt,但它始终在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.. // edit我现在甚至不确定使用“ Lock1”字符串的技巧是否真的有效并且将锁放置在同一对象上。

A nice example of producer / consumer synchronization using Monitors you can find on MSDN (Example 2) . 您可以在MSDN上找到使用Monitors进行生产者/消费者同步的一个很好的示例(示例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. 示例提供了完整的源代码,因此我没有在此处发布。

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 the Pulse . 否则,如果您在开始等待之前先Pulse ,则会错过Pulse Generally the pattern looks like: 通常,模式如下:

Thread A: 线程A:

 lock(x)
 {
    ... work ....
    while(!some_condition)
      Monitor.Wait(x)
 }

Thread B: 线程B:

lock(x)
{
   ... other work ...
   some_condition = true;
   Monitor.Pulse(x)
}

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. 通过在锁定状态下操作并检查some_condition,我们确保无论何时发生脉冲(无论在A中开始等待之前还是之后),A始终可以做出适当的反应,而不必永远等待已经出现的脉冲。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM