Gire: https://blog.csdn.net/ouyangfushu/article/details/80199140
Directorio
1. Función común llamada multiproceso
1.1 Función sin parámetros
#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 Función con parámetros
#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. Crear un hilo dentro de la clase
1. Las funciones internas de la clase son funciones estáticas.
#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. Use el modo singleton para crear hilos dentro de la clase
#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. Use funciones dentro de la clase para crear hilos fuera de la clase
#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. La diferencia entre join () y detach ()
join(): 主线程等待子线程结束后执行下一步(串行)
detach():独立于主线程并发执行,主线程后续代码无需等待
Ejemplo de 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*****
*/
Ejemplo de 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. Sincronización de datos (seguridad de hilos que operan en un dato al mismo tiempo)
#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
*/