多线程和关键部分使用 - C ++ [英] Multithreading and Critical Sections Use - C++

查看:150
本文介绍了多线程和关键部分使用 - C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于正确使用多线程应用程序中的关键段,我有点困惑。在我的应用程序中有几个对象(一些循环缓冲区和一个串行端口对象)在线程之间共享。这些对象的访问总是放在关键部分,还是只在特定时间?我怀疑只有在某些时候,因为当我试图包装每个使用 EnterCriticalSection / LeaveCriticalSection 成为死锁状态。任何洞察你可能会感激。感谢。

I'm a little confused as to the proper use of critical sections in multithreaded applications. In my application there are several objects (some circular buffers and a serial port object) that are shared among threads. Should access of these objects always be placed within critical sections, or only at certain times? I suspect only at certain times because when I attempted to wrap each use with an EnterCriticalSection / LeaveCriticalSection I ran into what seemed to be a deadlock condition. Any insight you may have would be appreciated. Thanks.

推荐答案

如果你跨线程共享资源,而其他线程在其他人写入时读取,总是。

If you share a resource across threads, and some of those threads read while others write, then it must be protected always.

如果不了解你的代码,很难给出更多的建议,但这里有一些要点。

It's hard to give any more advice without knowing more about your code, but here are some general points to keep in mind.

1)关键部分保护资源,而不是进程

1) Critical sections protect resources, not processes.

2)在所有主题中以相同的顺序输入/离开关键部分。如果线程A进入Foo,然后进入Bar,则线程B必须以相同的顺序输入Foo和Bar。如果你没有,你可以创造一个比赛。

2) Enter/leave critical sections in the same order across all threads. If thread A enters Foo, then enters Bar, then thread B must enter Foo and Bar in the same order. If you don't, you could create a race.

3)进入和离开必须以相反的顺序。例如,由于您输入Foo,然后输入Bar,您必须在离开Foo之前​​离开Bar。如果你不这样做,你可能会造成死锁。

3) Entering and leaving must be done in opposite order. Example, since you entered Foo then entered Bar, you must leave Bar before leaving Foo. If you don't do this, you could create a deadlock.

4)合理地保持锁定的最短时间。如果你在开始使用Bar之前完成Foo,在抓取Bar之前释放Foo。但你仍然必须从上面记住排序规则。在使用Foo和Bar的每个线程中,必须以相同的顺序获取和释放:

4) Keep locks for the shortest time period reasonably possible. If you're done with Foo before you start using Bar, release Foo before grabbing Bar. But you still have to keep the ordering rules in mind from above. In every thread that uses both Foo and Bar, you must acquire and release in the same order:

  Enter Foo
  Use Foo
  Leave Foo
  Enter Bar
  Use Bar
  Leave Bar

5)如果你只读99.9%的时间,写0.1%的时间,不要试图聪明。你仍然必须输入暴击秒,即使你只是阅读。这是因为你不想在写入过程中开始写入。

5) If you only read 99.9% of the time and write 0.1% of the time, don't try to be clever. You still have to enter the crit sec even when you're only reading. This is because you don't want a write to start when your'e in the middle of a read.

6)保持关键部分的粒度。每个关键部分应该保护一个资源,而不是多个资源。如果你让关键部分太大,你可以序列化你的应用程序或创建一个非常神秘的死锁或比赛。

6) Keep the critical sections granular. Each critical section should protect one resource, not multiple resources. If you make the critical sections too "big", you could serialize your application or create a very mysterious set of deadlocks or races.

这篇关于多线程和关键部分使用 - C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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