在Linux上学习线程 [英] learning threads on linux
问题描述
Linux对我来说是一个新平台.我已经在Windows上使用c ++进行了编码,已经适应了该平台上的多线程.
Linux is a new platform to me. I've coded on Windows in c++ for a number of years and have become comfortable with multithreading on that platform.
我需要在linux平台上学习c ++的时候,C ++ 11也随之出现.
Along comes C++11 at a time when I need to learn c++ on the linux platform.
Linux似乎大部分使用pthreads-好的,boost :: threads和QT也有自己的线程.但是C ++ 11附带了std :: thread,这是一种全新的(跨平台和C ++标准)执行线程的方法.
Linux appears to use pthreads for the most part - okay there's also boost::threads and QT have their own threads too. But with C++11 comes std::thread, a whole new (cross platform and C++ standard) way to do threads.
所以我想我必须学习pthread和std :: threads.最终,std :: thread似乎更重要,但是那里有很多旧代码,所以我必须都知道.
So I guess I'll have to learn pthreads and std::threads. Ultimately, std::thread seems more important, but there's a lot of legacy code out there, so I'll have to know both.
对于Windows上的线程同步,我将使用 WaitForMultipleObjects 等待许多任务完成,然后再继续进行进一步的工作.
For thread synchronization on windows, I would use WaitForMultipleObjects to wait for a number of tasks to complete before continuing with further work.
pthread是否存在类似的同步机制? std :: threads?
Does a similar synchronization mechanism exist for pthreads? std::threads?
我看过 pthread_join ,并且似乎可以一次只等待一个线程.我可能还错过了另一个pthread调用吗?
I've had a look at pthread_join, and it seems to have the facility to only wait on one thread at a time. Am I missing another pthread call maybe?
推荐答案
std::thread
is boost::thread
accepted into C++11 with some extras. My understanding is that if boost::thread
gets replaced in code with std::thread
it should still compile and work.
boost::thread
基于pthreads
设计,为线程,互斥量和条件变量提供了精简的C ++包装器.线程取消虽然未包含在C ++ 11的范围内,但由于未达成共识,因此在C ++中应如何使用.
boost::thread
is based on pthreads
design, providing thin C++ wrappers over thread, mutex and condition variables. Thread cancellation though was left outside the scope of C++11, since there was no agreement how it should work in C++.
因此,通过学习pthreads
,您还将学习std::thread
概念. std::thread
在pthreads
C API之上主要添加了语法糖和便利功能.
So, by learning pthreads
you also learn std::thread
concepts. std::thread
adds mostly syntax sugar and convenience functions on top of pthreads
C API.
关于WaitForMultipleObjects()
,pthreads
和std::thread
均未提供与其bWaitAll=FALSE
模式相似的功能,但是,它通常在UNIX上使用管道和select()
进行模拟,或更现代的eventfd()
和epoll()
在Linux上.可以依次等待所有任务来模拟bWaitAll=TRUE
模式,因为直到所有对象都准备就绪,它才继续进行.
With regards to WaitForMultipleObjects()
, neither pthreads
nor std::thread
provide anything similar to its bWaitAll=FALSE
mode, however, it's routinely simulated using pipes and select()
on UNIX, or more modern eventfd()
and epoll()
on Linux. bWaitAll=TRUE
mode can be simulated by waiting on all tasks in turn, since it doesn't proceed until all objects are ready anyway.
这篇关于在Linux上学习线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!