Linux 中相当于 Windows 的临界区是什么?
Windows NT 操作系统具有 EnterCriticalSection
和 ExitCriticalSection
对象来允许线程之间的同步。
使用 GCC 编译器时,Linux 的等效项是什么?
我看到对 __sync_synchronize
和 __scoped_lock
的引用
事实上,我看到提到了许多原子 __sync
函数以及许多 __atomic
的。
我实际上一直在使用 __sync_fetch_and_add 来实现原子增量 我应该使用 __atomic_add_dispatch
来代替吗?
有什么区别?
我应该使用哪些? C++ 中是否有一些构造可以在最新版本的 GCC 和 Visual C++ 2010 中使用,因为我将编写一些跨平台代码。
我看到boost有一些可用的功能,但是由于各种原因我不允许在windows下使用boost。
The Windows NT operating system has the EnterCriticalSection
and ExitCriticalSection
objects to allow for synchronization between threads.
What is the Linux equivalent, while using the GCC compiler?
I see references around to __sync_synchronize
along with __scoped_lock
In fact I see mention of a number of atomic __sync
functions along with a number of__atomic
ones.
I actually have been using __sync_fetch_and_add
for my atomic increment
Should I be using __atomic_add_dispatch
instead?
What's the difference?
Which ones should I be using? Are there some constructs in C++ that I can use in both the latest version of GCC and Visual C++ 2010 that are available as I'm going to be writing some cross platform code.
I see boost has some functions available, but for various reasons I'm not allowed to use boost under windows.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 Linux(和其他 Unixen)上,您需要使用 PThreads 或 Posix Threads。 Windows 上没有与关键部分等效的东西;请改用互斥锁。
编辑:请参阅下面的第一条评论 - 显然 Posix 互斥体与 Win32 关键部分相同,因为它们绑定到单个进程。
On Linux (and other Unixen) you need to use PThreads, or Posix Threads. There is no equivalent to Critical Sections on Windows; use a Mutex instead.
EDIT: See first comment below -- apparently Posix Mutexes are the same as Win32 Critical Sections in that they are bound to a single process.
检查此处:http://en.wikipedia.org/wiki/Critical_section
Check here: http://en.wikipedia.org/wiki/Critical_section
EnterCriticalSection
和其余 API 都是 Win32 API。至于跨平台同步API,我认为没有(因为你提到你不能使用boost)。另外,您提到跨平台,这是否也意味着不同的架构(对于 gcc 部分,即)。我见过一个大型实现,其中提供了一组通用的 API,这些 API 有条件地编译为具有本机 API(如 AIX 上的 fetch_and_add)或使用 Win32 API 的 pthreads。
我曾经尝试使用 win32 上的 posix 线程 但遇到了一堆问题(但那是非常旧的版本)。现在YMMV。
EnterCriticalSection
and the rest of the APIs are Win32 APIs. As far as cross-platform synchronization APIs, I don't think there are any (since you mention you can't use boost). Also, you mentioned cross-platform, does this mean different architectures too (for the gcc part i.e.).I've seen one large implementation where there was a common set of APIs provided which were conditionally compiled to have the native APIs (like fetch_and_add on AIX) or used pthreads the Win32 APIs.
I once tried to use posix threads on win32 but ran into a bunch of issues (but that was a very old version). Now YMMV.
如果你只为Windows平台开发程序,我认为最好的方法是使用Win32 API。否则,您可以使用 Qt C++ 库(为此目的 Qt Core 就足够了)。
另请参阅:QMutex 和 QMutexLocker
您还可以使用:QReadWriteLock
If you are developing programs only for Windows platform, I think the best way is using Win32 API. Otherwise you can use Qt C++ libraries (for that purpose Qt Core is enough).
See also: QMutex and QMutexLocker
You can also use: QReadWriteLock