问题描述
我想开始学习C ++中的多线程.我也在Java中学习它.在Java中,如果我编写一个使用多线程的程序,它将可以在任何地方使用.但是,在C ++中,多线程是否不依赖于特定于平台的API?如果是这样,那似乎会妨碍可移植性.
如何在C ++中进行多线程处理而又不会引起可移植性问题? boost的 thread 库是否是一个好的解决方案? /p>
作为旁注-怎么可能将多线程实现为一个库?那不是编译器必须要做的事情吗?
如果您没有支持 C ++ 0x (例如,Visual Studio c ++ 2010中提供),请使用增强线程. (除非您使用已经支持线程的框架,否则不是这种情况-否则您将不会问这个问题-).这些增强线程实际上已成为全新C ++中的标准.在此之前,C ++本身并不了解线程.
如果您想学习并行编程的其他方面,您可能也会对TBB线程构建块感兴趣.
关于Qt
:如果仅需要线程支持,那就太过头了.从编译到生成结果,往返时间极慢.这确实是精心设计的思想.但是还没有像boost这样的C ++ 0x线程这样的官方标准.因此,我不会将其作为首选.
I'd like to start learning multithreading in C++. I'm learning it in Java as well. In Java, if I write a program which uses multithreading, it will work anywhere. However in C++, doesn't multithreading rely on platform-specific API's? If so, that would seem to get in the way of portability.
How can I do multithreading in C++ without causing portability issues? Is boost's thread library a good solution?
As a sidenote - how is it even possible to implement multithreading as a library? Isn't that something that has to be done by the compiler?
If you do not have a compiler that supports C++0x yet (available with visual studio c++ 2010 for example), use boost threads. (Unless you use a framework that already supports threading, which is not the case - you wouldn't ask the question otherwise -). These boost threads became actually the standard in the brand new C++. Before that time C++ itself was thread unaware.
TBB Threading Building Blocks might also be interesting to you if you want to learn other aspects in parallel programming.
Regarding Qt
: if you only want threading support it is complete overkill. It has horribly slow round trip times from compiling to result. It is really well designed thought. But not an official standard like the C++0x threads from boost. Therefore I would not take it as a first choice.
这篇关于C ++中的多线程...从哪里开始?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!