线程之间的同步使用临界区 [英] Synchronization between threads using Critical Section

查看:195
本文介绍了线程之间的同步使用临界区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个线程,ThreadA和ThreadsB-Z。



ThreadA始终处于关键部分,将数据弹出队列并在套接字上发送。



当ThreadB到ThreadZ的任何线程想要进入关键部分时,它希望ThreadA才能离开关键部分。然后进入关键部分,将一些数据推入队列并离开关键部分。



我在这里有两个问题:


  1. ThreadB-Z(谁想要进入关键部分)如何
    告诉ThreadA在需要
    访问关键部分时离开关键部分。 >
  2. 我尝试使用SetEvent或PostThreadMessage的想法告诉
    threadA离开关键的部分,但是我无法处理
    任何事件或线程消息,因为ThreadA不断弹出
    数据从队列中使用while(1)并且没有消息循环或
    WaitforSingleObject()类型事件来处理事件或线程消息
    :(

我很喜欢这种情况,任何帮助/建议都是值得的,谢谢提前。

解决方案

这里的真正问题是ThreadA始终处于关键部分。 ThreadA 不应该锁定关键部分inde有限。



在C#中,您的 ThreadA 的代码如下所示:

 消息消息; 

//仅修改队列所需的时间才锁定
lock(_messageQueueLock){
message = _messageQueue.Dequeue();
}

//使用消息

我的看法,使用以下一般经验法则应该遵循使用关键部分:




  • 输入并退出使用关键部分来保护资源/资产(例如共享内存,队列,列表等)。 )

  • 避免使用关键部分来保护功能/方法调用。如果您正在从关键部分进行函数/方法调用,则会增加创建死锁的可能性。

  • 避免尝试在关键部分内锁定关键部分。

  • 在关键部分,避免尝试访问外部资源(例如执行数据库SQL查询)。

  • 如果有意义的话,我通常会对关键部分使用以下命名约定:如果被保护的资产称为 messageQueue ...然后我将命名关键部分 messageQueueLock



来自Microsoft的好消息:当您使用任何类型的多线程时,您可能会将自己暴露在非常严重和复杂的错误中。[SOURCE: MSDN]


I have multiple threads, ThreadA and ThreadsB-Z.

ThreadA is always in critical section, popping the data out of queue and sending it on socket.

When any thread from ThreadB to ThreadZ want to enter the critical section, it wants ThreadA to leave critical section only then. It then enters the critical Section, push some data into queue and leaves critical section.

I have two problems here:

  1. How would ThreadB-Z (whoever wants to enter the Critical Section) tell ThreadA to leave critical section when it wants to access the critical section.
  2. I tried with the idea of SetEvent or PostThreadMessage to tell threadA to leave the critical Section, but I am unable to handle any Event or Thread Message since ThreadA is continuously popping data out of the queue using while(1) and there is no message loop or WaitforSingleObject() type thing to handle Events or Thread Messages :(

I am like stuck in this situation. Any help/suggestion is welcome. Thanks in advance.

解决方案

The real problem here is "ThreadA is always in critical section". ThreadA should not be locking the critical section indefinitely.

In C#, your code for ThreadA would look something like:

Message message;

// Only lock critical section for the time it takes to modify the queue
lock(_messageQueueLock){
   message = _messageQueue.Dequeue();
}

// do something with the message

In my opinion, the following general rules of thumb should be observed when using critical sections:

  • Enter and exit the critical section as quickly as possible (i.e. minimize the amount of code in the critical section).
  • Use critical sections to protect a resource/asset (e.g. shared memory, queue, list, etc.)
  • Avoid using critical sections to protect function/method calls. If you are making function/method calls from within a critical section... you are increasing the probability of creating deadlocks.
  • Avoid trying to lock a critical section from within a critical section. Failing to do so may result in potential deadlocks within your application.
  • Avoid trying to access an external resource (e.g. executing a database SQL query) while in a critical section.
  • Where it makes sense, I generally like to use the following naming convention for critical sections: if the asset that is being protected is called messageQueue... then I would name the critical section messageQueueLock

Great quote from Microsoft: "When you use multithreading of any sort, you potentially expose yourself to very serious and complex bugs" [SOURCE: MSDN]

这篇关于线程之间的同步使用临界区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆