Linux 中相当于 Windows 的临界区是什么?

发布于 2024-09-15 12:45:21 字数 528 浏览 7 评论 0原文

Windows NT 操作系统具有 EnterCriticalSectionExitCriticalSection 对象来允许线程之间的同步。

使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

︶ ̄淡然 2024-09-22 12:45:21

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

怪我太投入 2024-09-22 12:45:21

检查此处: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;
}

Check here: 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;
}
木槿暧夏七纪年 2024-09-22 12:45:21

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.

凉世弥音 2024-09-22 12:45:21

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

另请参阅:QMutexQMutexLocker
您还可以使用: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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文