mutex 与 C++11 中的 pthread 兼容吗?
Is mutex compatible with pthread in C++11?
尽管 pthread_mutex
,互斥锁 (#include <mutex>
) 会使用 C++11 使 pthread 安全吗?
请查下例子:
// Creating Server thread
pthread_create(&server_thread, NULL,Server,NULL);
// Creating Client thread
pthread_create(&client_thread, NULL, Client,NULL);
// Wait until client_thread exits
pthread_join( client_thread, NULL);
服务器和客户端都调用 foo()
。
mutex mut;
void foo (){
mut.lock();
CRITICAL_WRITE();
mut.unlock();
}
它是特定于实现的。我所知道的 C++11 标准库(例如 GCC 4.9, and probably also libc++
from Clang/LLVM) are practically built above the existing pthreads(7) 库中的 libstdc++
Linux。
原则上有人可以在 Linux 上直接使用 system calls (e.g. futex(2) 构建一些 C++11 标准库 - 与手写汇编代码混合 - 用于互斥锁),但我知道 none 这样的 C++11 库。
线程是在低级 clone(2) 系统调用之上的 pthreads 中的 Linux 上构建的。
所以你没有正式的保证,但实际上你今天相当安全(Linux)
尽管 pthread_mutex
,互斥锁 (#include <mutex>
) 会使用 C++11 使 pthread 安全吗?
请查下例子:
// Creating Server thread
pthread_create(&server_thread, NULL,Server,NULL);
// Creating Client thread
pthread_create(&client_thread, NULL, Client,NULL);
// Wait until client_thread exits
pthread_join( client_thread, NULL);
服务器和客户端都调用 foo()
。
mutex mut;
void foo (){
mut.lock();
CRITICAL_WRITE();
mut.unlock();
}
它是特定于实现的。我所知道的 C++11 标准库(例如 GCC 4.9, and probably also libc++
from Clang/LLVM) are practically built above the existing pthreads(7) 库中的 libstdc++
Linux。
原则上有人可以在 Linux 上直接使用 system calls (e.g. futex(2) 构建一些 C++11 标准库 - 与手写汇编代码混合 - 用于互斥锁),但我知道 none 这样的 C++11 库。
线程是在低级 clone(2) 系统调用之上的 pthreads 中的 Linux 上构建的。
所以你没有正式的保证,但实际上你今天相当安全(Linux)