如何使一个应用程序的线程安全吗? [英] how to make an application thread safe?

查看:115
本文介绍了如何使一个应用程序的线程安全吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为线程安全,特别是,意味着它必须满足的需要多个线程访问相同的共享数据。但是,看来这个定义是远远不够的。

I thought thread safe, in particular, means it must satisfy the need for multiple threads to access the same shared data. But, it seems this definition is not enough.

谁能请列表输出到作出或照顾,使应用程序线程安全的事情。如果可能的话,给出的答案相对于C / C ++语言。

Can anyone please list out the things to be done or taken care of to make an application thread safe. If possible, give an answer with respect to C/C++ language.

推荐答案

有几种方法,其中一个功能是线程安全的。

There are several ways in which a function can be thread safe.

它可以折返。这意味着一个函数没有状态,不会触及任何全局或静态变量,因此它可以同时从多个线程调用。这个术语来自允许一个线程进入功能,而另一个线程已经在里面。

It can be reentrant. This means that a function has no state, and does not touch any global or static variables, so it can be called from multiple threads simultaneously. The term comes from allowing one thread to enter the function while another thread is already inside it.

它可以有一个临界区。这个词被周围很多抛出,但坦率地说我preFER 关键数据即可。一个关键部分出现的code触及跨多个线程共享数据的任何时间。所以我preFER把重点放在关键数据。

It can have a critical section. This term gets thrown around a lot, but frankly I prefer critical data. A critical section occurs any time your code touches data that is shared across multiple threads. So I prefer to put the focus on that critical data.

如果您使用的是<一个href=\"http://live.boost.org/doc/libs/1_37_0/doc/html/interprocess/synchronization_mechanisms.html\">mutex正确,可以同步访问关键数据,从线程不安全的修改切实保护。互斥和锁定是非常有用的,但与大国意味着巨大的责任。必须不相同的线程内锁定同一互斥两次(即自死锁)。如果您获得了多个互斥的,因为它会增加你的危险僵局,你一定要小心。你必须始终与互斥锁来保护您的数据。

If you use a mutex properly, you can synchronize access to the critical data, properly protecting from thread unsafe modifications. Mutexes and Locks are very useful, but with great power comes great responsibility. You must not lock the same mutex twice within the same thread (that is a self-deadlock). You must be careful if you acquire more than one mutex, as it increases your risk for deadlock. You must consistently protect your data with mutexes.

如果您的所有功能都是线程安全的,您的所有共享数据的妥善保护,应用程序应该是线程安全的。

If all of your functions are thread safe, and all of your shared data properly protected, your application should be thread safe.

疯狂埃迪说,这是一个巨大的课题。我建议你​​阅读了上提振线程,并相应地使用它们。

As Crazy Eddie said, this is a huge subject. I recommend reading up on boost threads, and using them accordingly.

低级别的警告:编译器可以重新排列语句,可以打破线程安全。随着多个内核,每个内核都有自己的缓存,你需要正确地同步缓存有线程安全。此外,即使编译器不重新排序报表时,硬件可能。因此,饱满,保证线程安全的今天实际上不可能。你可以得到的方式99.99%那儿虽然和工作正在与编译器厂商和CPU厂商做来解决这个挥之不去的警告。

low-level caveat: compilers can reorder statements, which can break thread safety. With multiple cores, each core has its own cache, and you need to properly sync the caches to have thread safety. Also, even if the compiler doesn't reorder statements, the hardware might. So, full, guaranteed thread safety isn't actually possible today. You can get 99.99% of the way there though, and work is being done with compiler vendors and cpu makers to fix this lingering caveat.

无论如何,如果你正在寻找一个清单,以使一个类线程安全的:

Anyway, if you're looking for a checklist to make a class thread-safe:


  • 标识跨线程共享的任何数据(如果你错过了,你不能保护它)

  • 创建一个成员的boost ::互斥m_mutex 并使用它,只要你尝试在共享的数据成员(理想情况下共享数据是私有的类,所以您可以访问更多一些,你正确地保护它)。

  • 清理全局。全局是坏反正,好运试图做任何线程安全的全局变量。

  • 谨防静态关键字。它实际上不是线程安全的。所以,如果你试图做一个单身,也不会工作的权利。

  • 当心双重检查锁范式。谁使用它大多数人弄错了一些微妙的方式,它是容易被低级别的警告破损。

  • Identify any data that is shared across threads (if you miss it, you can't protect it)
  • create a member boost::mutex m_mutex and use it whenever you try to access that shared member data (ideally the shared data is private to the class, so you can be more certain that you're protecting it properly).
  • clean up globals. Globals are bad anyways, and good luck trying to do anything thread-safe with globals.
  • Beware the static keyword. It's actually not thread safe. So if you're trying to do a singleton, it won't work right.
  • Beware the Double-Checked Lock Paradigm. Most people who use it get it wrong in some subtle ways, and it's prone to breakage by the low-level caveat.

这是一个不完整的清单。我将添加,如果我认为它更多的,但我希望它足以让你开始。

That's an incomplete checklist. I'll add more if I think of it, but hopefully it's enough to get you started.

这篇关于如何使一个应用程序的线程安全吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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