So in summary:
- Use pthread if you need maximum portability and control. 更加底层一些?
- Use std::thread if you want an easier to use API and tighter integration with C++. 是对POSIX线程(即
pthread
)的一个高级封装,提供了更加易用和跨平台的线程支持 - For most C++ programs, I would recommend starting with std::thread. Only drop down to pthreads if you need something std::thread cannot provide.
pthread
Pros:
- Portable (POSIX standard):POSIX标准是IEEE为在各种UNIX操作系统上运行的软件定义的一系列API标准的总称,定义了Unix系列操作系统对应用的接口规范
- Low level API gives you a lot of control
- Can be used in C programs as well as C++
Cons: - Low level API can be cumbersome(笨重) to use
- Lacks some convenient features of std::thread like return values, exceptions, etc. 重要不足