简体   繁体   中英

Is C++11's std::thread compatible with POSIX semaphores?

我想通过使用标准 C++ std::thread 库在我的 C++ 应用程序中使用线程,但是你想使用信号量并且使用 C++20 的信号量是不可能的,我想知道是否 POSIX 信号量 <semaphore.h > 与 C++ STD 的线程兼容,或者我必须更改代码才能使用 POSIX 线程

The C++ standard library will implement std::thread as a wrapper over pthreads on POSIX systems, so using <semaphore.h> would be fine. Semaphores are usually implemented regardless of the specific threading interface, though the C standard library may do some book-keeping at the same time using pthreads.

For this reason, calling sem_wait() from a thread (whether it be a pthread_t or an std::thread) will have the same effect, though it may be better to just use pthreads, as they would be the most "compatible", especially since you are only targeting POSIX systems anyway.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM