c ++线程-并行处理
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5716220/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
c++ threads - parallel processing
提问by Kid Green
I was wondering how to execute two processes in a dual-core processor in c++.
我想知道如何在 C++ 中的双核处理器中执行两个进程。
I know threads (or multi-threading) is not a built-in feature of c++.
我知道线程(或多线程)不是 C++ 的内置功能。
There is threading support in Qt, but I did not understand anything from their reference. :(
Qt 中有线程支持,但我从他们的参考中没有理解任何内容。:(
So, does anyone know a simple way for a beginner to do it. Cross-platform support (like Qt) would be very helpful since I am on Linux.
那么,有没有人知道初学者的简单方法。因为我在 Linux 上,所以跨平台支持(如 Qt)会非常有帮助。
回答by dubnde
Try the Multithreading in C++0x part 1: Starting Threadsas a 101. If you compiler does not have C++0xsupport, then stay with Boost.Thread
尝试C++0x 中的多线程第 1 部分:将线程作为 101启动。如果您的编译器不支持C++0x,则继续使用Boost.Thread
回答by murrekatt
Take a look at Boost.Thread. This is cross-platform and a very good library to use in your C++ applications.
看看Boost.Thread。这是跨平台的,是在 C++ 应用程序中使用的非常好的库。
What specifically would you like to know?
你特别想知道什么?
回答by holtavolt
I'd recommend using the Boost libraries Boost.Threadinstead. This will wrap platform specifics of Win32 and Posix, and give you a solid set of threading and synchronization objects. It's also in very heavy use, so finding help on any issues you encounter on SO and other sites is easy.
我建议改用 Boost 库Boost.Thread。这将包装 Win32 和 Posix 的平台细节,并为您提供一组可靠的线程和同步对象。它的使用量也非常大,因此在 SO 和其他站点上遇到的任何问题都可以轻松找到帮助。
回答by Rune Aamodt
The POSIX thread (pthreads) library is probably your best bet if you just need a simple threading library, it has implementations both on Windows and Linux.
如果您只需要一个简单的线程库,POSIX 线程 (pthreads) 库可能是您最好的选择,它在 Windows 和 Linux 上都有实现。
A guide can be found e.g. here. A Win32 implementation of pthreads can be downloaded here.
可以在例如此处找到指南。可以在此处下载 pthread 的 Win32 实现。
Edit: Didn't see you were on Linux. In that case I'm not 100% sure but I think the libraries are probably already bundled in with your GCC installation.
编辑:没看到你在 Linux 上。在这种情况下,我不是 100% 确定,但我认为这些库可能已经与您的 GCC 安装捆绑在一起。
回答by user715213
You can search for a free PDF book "C++-GUI-Programming-with-Qt-4-1st-ed.zip" and read Chapter 18 about Multi-threading in Qt.
您可以搜索免费的 PDF 书籍“C++-GUI-Programming-with-Qt-4-1st-ed.zip”并阅读有关 Qt 中的多线程的第 18 章。
Concurrent programming features supported by Qt includes (not limited to) the following:
Qt 支持的并发编程特性包括(不限于)以下内容:
- Mutex
- Read Write Lock
- Semaphore
- Wait Condition
- Thread Specific Storage
- 互斥体
- 读写锁
- 信号
- 等待条件
- 线程特定存储
However, be aware of the following trade-offs with Qt:
但是,请注意 Qt 的以下权衡:
- Performance penalties vs native threading libraries. POSIX thread (pthreads) has been native to Linux since kernel 2.4 and may not substitute for < process.h > in W32API in all situations.
- Inter-thread communication in Qt is implemented with SIGNAL and SLOT constructs. These are NOT part of the C++ language and are implemented as macros which requires proprietary code generators provided by Qt to be fully compiled.
- 性能损失与本机线程库的比较。POSIX 线程(pthreads)从内核 2.4 开始就已经是 Linux 原生的,并且在所有情况下都不能替代 W32API 中的 <process.h>。
- Qt 中的线程间通信是通过 SIGNAL 和 SLOT 结构实现的。这些不是 C++ 语言的一部分,而是作为宏实现的,需要 Qt 提供的专有代码生成器才能完全编译。
If you can live with the above limitations, just follow these recipes for using QThread:
如果您可以忍受上述限制,只需遵循以下使用 QThread 的秘诀:
- #include < QtCore >
- Derive your own class from QThread. You must implement a public function run() that returns void to contain instructions to be executed.
- Instantiate your own class and call start() to kick off a new thread.
- #include <QtCore>
- 从 QThread 派生您自己的类。您必须实现一个返回 void 的公共函数 run() 以包含要执行的指令。
- 实例化您自己的类并调用 start() 以启动一个新线程。
Sameple Code:
相同代码:
#include <QtCore>
class MyThread : public QThread {
public:
void run() {
// do something
}
};
int main(int argc, char** argv) {
MyThread t1, t2;
t1.start(); // default implementation from QThread::start() is fine
t2.start(); // another thread
t1.wait(); // wait for thread to finish
t2.wait();
return 0;
}
回答by Babra Cunningham
As an important note in c++14, the use of concurrentthreadingis available:
作为 c++14 中的一个重要说明,并发线程的使用是可用的:
#include<thread>
class Example
{
auto DoStuff() -> std::string
{
return "Doing Stuff";
}
auto DoStuff2() -> std::string
{
return "Doing Stuff 2";
}
};
int main()
{
Example EO;
std::string(Example::*func_pointer)();
func_pointer = &Example::DoStuff;
std::future<string> thread_one = std::async(std::launch::async, func_pointer, &EO); //Launching upon declaring
std::string(Example::*func_pointer_2)();
func_pointer_2 = &Example::DoStuff2;
std::future<string> thread_two = std::async(std::launch::deferred, func_pointer_2, &EO);
thread_two.get(); //Launching upon calling
}
Both std::async
(std::launch::async
, std::launch::deferred
) and std::thread
are fully compatible with Qt, and in some cases may be better at working in different OS environments.
两者std::async
(std::launch::async
,std::launch::deferred
)和std::thread
与Qt的完全兼容,并且在某些情况下,可以在不同的操作系统环境中工作会更好。
For parallelprocessing, see this.
对于并行处理,请参见这里。