c++11多线程(转)
转:https://blog.csdn.net/ouyangfushu/article/details/80199140
1. 普通函数多线程调用
1.1 无参数函数
#include<iostream>
#include<thread>
using namespace std;
void testThread()
{
cout << "test thread!" << endl;
}
int main()
{
std::thread t1(testThread);
t1.join();
cout << "*****end*****" << endl;
return 0;
}
/*输出结果:
Test thread!
*****end*****
*/
1.2 有参数函数
#include<iostream>
#include<thread>
using namespace std;
void add(int a, int b)
{
int c = a + b;
cout << "c: " << c << endl;
}
int main()
{
std::thread t1(add, 5, 6);
t1.join();
cout << "*****end*****" << endl;
return 0;
}
/*输出结果:
c: 11
*****end*****
*/
2. 在类内部创建线程
1.类内部函数为静态函数
#include<iostream>
#include<thread>
using namespace std;
class TestThread
{
public:
static void threadFun() {
cout << "test thread function" << endl;
}
static void start() {
std::thread t(threadFun);
t.join();
}
};
int main()
{
TestThread::start();
return 0;
}
/*
输出结果:
test thread function
*/
2.类内部使用单例模式创建线程
#include <iostream>
#include <functional>
#include <thread>
using namespace std;
class TestThread
{
public:
~TestThread() {}
void threadFun() {
cout << "test thread function" << endl;
}
static void start() {
std::thread t(std::bind(&TestThread::threadFun, &TestThread::getInstance()));
t.join();
}
static TestThread& getInstance() {
if (NULL != m_instance) {
m_instance = new TestThread();
}
return *m_instance;
}
private:
TestThread() {}
static TestThread* m_instance;
};
TestThread* TestThread::m_instance = NULL;
int main()
{
TestThread::start();
return 0;
}
/*
输出结果:
test thread function
*/
3. 用在类内部的函数在类外部创建线程
#include <iostream>
#include <functional>
#include <thread>
using namespace std;
class TestThread
{
public:
void threadFun(int a, int b) {
cout << "test thread function: " << a + b << endl;
}
};
int main()
{
TestThread testObj;
std::thread t(std::bind(&TestThread::threadFun, &testObj, 3, 4));
t.join();
return 0;
}
/*
输出结果:
test thread function: 7
*/
4. join()和detach()的区别
join(): 主线程等待子线程结束后执行下一步(串行)
detach():独立于主线程并发执行,主线程后续代码无需等待
join()示例
#include<iostream>
#include<thread>
using namespace std;
void testThread()
{
cout << "test thread!" << endl;
cout << endl;
}
void add(int a, int b)
{
cout << "add, a+b = " << a+b << endl;
}
int main()
{
std::thread t1(testThread);
t1.join();
std::thread t2(add, 3, 4);
t2.join();
cout << "*****main end*****" << endl;
return 0;
}
/*
test thread!
add, a+b = 7
*****main end*****
*/
detach()示例:
#include<iostream>
#include<thread>
using namespace std;
void testThread()
{
cout << "test thread!" << endl;
cout << endl;
}
void add(int a, int b)
{
cout << "add, a+b = " << a+b << endl;
}
int main()
{
std::thread t1(testThread);
std::thread t2(add, 3, 4);
t1.detach();
t2.detach();
cout << "*****main end*****" << endl;
return 0;
}
/*输出结果:随机的,下面是一种可能
add, a+b = 7
*****main end*****
test thread!
*/
5. 数据同步(线程同时操作一个数据的安全性)
#include<iostream>
#include<thread>
#include<mutex>
using namespace std;
std::mutex mt;
int data = 1;
void add(int a)
{
mt.lock();
data += a;
cout << "add, data = " << data << endl;
mt.unlock();
}
void multi(int a)
{
mt.lock();
data *= a;
cout << "multi, data = " << data << endl;
mt.unlock();
}
int main()
{
std::thread t1(add, 2);
std::thread t2(multi, 10);
t1.detach();
t2.detach();
cout << "*****main end*****" << endl;
return 0;
}
/*输出结果有不同的可能
结果1:
multi, data = 10
*****main end*****
add, data = 12
结果2:
add, data = 3
*****main end*****
multi, data = 30
*/