multithreading linux 上的 gcc 相当于微软的临界区是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3508507/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 01:10:17  来源:igfitidea点击:

What are gcc on linux's equivalent to microsoft's critical sections?

multithreadinggccsynchronizationposix

提问by Matt

The Microsoft Visual C++ compilers have the EnterCriticalSectionand ExitCriticalSectionobjects to allow for synchronization between threads.

Microsoft Visual C++ 编译器具有EnterCriticalSectionExitCriticalSection对象以允许线程之间的同步。

What is the GCC equivalent?

什么是 GCC 等价物?

I see references around to __sync_synchronizealong with __scoped_lock

我看到周围的引用__sync_synchronize沿__scoped_lock

In fact I see mention of a number of atomic __syncfunctions along with a number of __atomicones.

事实上,我看到提到了许多原子__sync函数以及一些原子函数 __atomic

I actually have been using __sync_fetch_and_addfor my atomic increment Should I be using __atomic_add_dispatchinstead?
What's the difference?

我实际上一直在使用__sync_fetch_and_add我的原子增量我应该使用它__atomic_add_dispatch吗?
有什么不同?

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.

我应该使用哪些?是否有一些 C++ 结构可以在最新版本的 GCC 和 Visual C++ 2010 中使用,因为我将要编写一些跨平台代码。

I see boost has some functions available, but for various reasons I'm not allowed to use boost under windows.

我看到 boost 有一些可用的功能,但由于各种原因,我不允许在 windows 下使用 boost。

回答by Billy ONeal

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.

在 Linux(和其他 Unixen)上,您需要使用 PThreads 或 Posix Threads。没有与 Windows 上的临界区等效的东西;改用互斥锁。

EDIT: See first comment below -- apparently Posix Mutexes are the same as Win32 Critical Sections in that they are bound to a single process.

编辑:请参阅下面的第一条评论——显然 Posix Mutex 与 Win32 临界区相同,因为它们绑定到单个进程。

回答by Tutankhamen

Check here: http://en.wikipedia.org/wiki/Critical_section

在这里查看:http: //en.wikipedia.org/wiki/Critical_section

/* Sample C/C++, Unix/Linux */
#include <pthread.h>

/* This is the critical section object (statically allocated). */
static pthread_mutex_t cs_mutex =  PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;

void f()
{
    /* Enter the critical section -- other threads are locked out */
    pthread_mutex_lock( &cs_mutex );

    /* Do some thread-safe processing! */

    /*Leave the critical section -- other threads can now pthread_mutex_lock()  */
    pthread_mutex_unlock( &cs_mutex );
}

int main()
{
    f();

    return 0;
}

回答by Amith Chinthaka

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).

如果你只为 Windows 平台开发程序,我认为最好的方法是使用 Win32 API。否则,您可以使用 Qt C++ 库(为此,Qt Core 就足够了)。

See also: QMutexand QMutexLockerYou can also use: QReadWriteLock

另见:QMutexQMutexLocker你也可以使用:QReadWriteLock

回答by Gangadhar

EnterCriticalSectionand 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 win32but ran into a bunch of issues (but that was a very old version). Now YMMV.

EnterCriticalSection其余的 API 是 Win32 API。至于跨平台同步 API,我认为没有(因为您提到不能使用 boost)。另外,您提到了跨平台,这是否也意味着不同的架构(对于 gcc 部分,即)。我见过一个大型实现,其中提供了一组通用的 API,这些 API 被有条件地编译为具有本机 API(如 AIX 上的 fetch_and_add)或使用 pthreads 和 Win32 API。我曾经尝试在 win32上使用posix 线程,但遇到了一堆问题(但那是一个非常旧的版本)。现在YMMV。