Zak*_*Zak 391 c++ multithreading
有人可以发布一个在C++中启动两个(面向对象)线程的简单示例.
我正在寻找实际的C++线程对象,我可以扩展运行方法(或类似的东西),而不是调用C风格的线程库.
更新 - 我遗漏了任何特定于操作系统的请求,希望无论谁回复都会回复使用跨平台库.我现在只是明白了.
Mas*_*tic 535
创建一个您希望线程执行的函数,例如:
void task1(std::string msg)
{
std::cout << "task1 says: " << msg;
}
Run Code Online (Sandbox Code Playgroud)
现在创建thread
最终将调用上述函数的对象,如下所示:
std::thread t1(task1, "Hello");
Run Code Online (Sandbox Code Playgroud)
(你需要#include <thread>
访问该std::thread
课程)
构造函数的参数是线程将执行的函数,后跟函数的参数.线程在构造时自动启动.
如果稍后您要等待线程执行该函数,请调用:
t1.join();
Run Code Online (Sandbox Code Playgroud)
(加入意味着调用新线程的线程将等待新线程完成执行,然后才会继续自己的执行).
#include <string>
#include <iostream>
#include <thread>
using namespace std;
// The function we want to execute on the new thread.
void task1(string msg)
{
cout << "task1 says: " << msg;
}
int main()
{
// Constructs the new thread and runs it. Does not block execution.
thread t1(task1, "Hello");
// Do other things...
// Makes the main thread wait for the new thread to finish execution, therefore blocks its own execution.
t1.join();
}
Run Code Online (Sandbox Code Playgroud)
-std=c++0x -pthread
.Edw*_*ETT 78
好吧,从技术上讲,任何这样的对象最终都会在C风格的线程库上构建,因为C++只是std::thread
在c ++ 0x中指定了一个库存模型,它刚刚被确定并且尚未实现.问题在某种程度上是系统性的,从技术上讲,现有的c ++内存模型不够严格,无法为所有"之前发生的"案例提供明确定义的语义.Hans Boehm不久前写了一篇关于这个主题的论文,并且有助于敲定关于该主题的c ++ 0x标准.
http://www.hpl.hp.com/techreports/2004/HPL-2004-209.html
这就是说有几个跨平台的线程C++库在实践中运行得很好.英特尔线程构建块包含一个非常接近c ++ 0x标准的tbb :: thread对象,而Boost有一个boost :: thread库,它也是如此.
http://www.threadingbuildingblocks.org/
http://www.boost.org/doc/libs/1_37_0/doc/html/thread.html
使用boost :: thread你会得到类似的东西:
#include <boost/thread.hpp>
void task1() {
// do stuff
}
void task2() {
// do stuff
}
int main (int argc, char ** argv) {
using namespace boost;
thread thread_1 = thread(task1);
thread thread_2 = thread(task2);
// do other stuff
thread_2.join();
thread_1.join();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Hoh*_*erg 20
POSIX操作系统还有一个POSIX库. 检查兼容性
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <iostream>
void *task(void *argument){
char* msg;
msg = (char*)argument;
std::cout<<msg<<std::endl;
}
int main(){
pthread_t thread1, thread2;
int i1,i2;
i1 = pthread_create( &thread1, NULL, task, (void*) "thread 1");
i2 = pthread_create( &thread2, NULL, task, (void*) "thread 2");
pthread_join(thread1,NULL);
pthread_join(thread2,NULL);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
用-lpthread编译
http://en.wikipedia.org/wiki/POSIX_Threads
Can*_*ner 14
这是一种创建任意数量线程的更强大的方法(在C++ 11或更新版本中):
#include <thread>
#include <iostream>
#include <vector>
using namespace std;
void doSomething(int id) {
cout << id << "\n";
}
/**
* Spawns n threads
*/
void spawnThreads(int n)
{
std::vector<thread> threads(n);
// spawn n threads:
for (int i = 0; i < n; i++) {
threads[i] = thread(doSomething, i + 1);
}
for (auto& th : threads) {
th.join();
}
}
int main()
{
spawnThreads(10);
}
Run Code Online (Sandbox Code Playgroud)
liv*_*ech 10
当搜索在新线程中调用其自己的实例方法之一的C++类的示例时,会出现此问题,但我们无法以这种方式使用任何这些答案.这是一个例子:
Class.h
class DataManager
{
public:
bool hasData;
void getData();
bool dataAvailable();
};
Run Code Online (Sandbox Code Playgroud)
Class.cpp
#include "DataManager.h"
void DataManager::getData()
{
// perform background data munging
hasData = true;
// be sure to notify on the main thread
}
bool DataManager::dataAvailable()
{
if (hasData)
{
return true;
}
else
{
std::thread t(&DataManager::getData, this);
t.detach(); // as opposed to .join, which runs on the current thread
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,此示例不会进入互斥锁或锁定.
除非想要在全局namespac中使用单独的函数,否则我们可以使用lambda函数来创建线程.
使用lambda创建线程的一个主要优点是我们不需要将本地参数作为参数列表传递.我们可以使用相同的捕获列表,lambda的closure属性将处理生命周期.
这是一个示例代码
int main() {
int localVariable = 100;
thread th { [=](){
cout<<"The Value of local variable => "<<localVariable<<endl;
}}
th.join();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
到目前为止,我发现C++ lambdas是创建线程的最佳方式,尤其是对于更简单的线程函数
它在很大程度上取决于您决定使用的库.例如,如果您使用wxWidgets库,则线程的创建将如下所示:
class RThread : public wxThread {
public:
RThread()
: wxThread(wxTHREAD_JOINABLE){
}
private:
RThread(const RThread ©);
public:
void *Entry(void){
//Do...
return 0;
}
};
wxThread *CreateThread() {
//Create thread
wxThread *_hThread = new RThread();
//Start thread
_hThread->Create();
_hThread->Run();
return _hThread;
}
Run Code Online (Sandbox Code Playgroud)
如果您的主线程调用CreateThread方法,您将创建一个新线程,该线程将开始执行"Entry"方法中的代码.在大多数情况下,您必须保持对线程的引用才能加入或停止它.更多信息:wxThread文档