1.C创建线程可以参考下(别人写的还是挺详细的)
pthread_create函数详解(向线程函数传递参数)-CSDN博客
2.C++创建线程
A》头文件
B》编译
C.1》创建普通线程
结果:
如果是带参数的
结果:
C.2》通过lambda创建线程 (刚看到添加代码窗口,后续代码窗口替换图片)
int main()
{
auto Func = [](int max) {
for(int i = 0; i < max; i++) {
cout<<i<<endl;
}
};
thread th(Func, 2);
th.join();//主线程等待子线程完成退出
return 0;
}
C.3》创建线程通过重构“()”运算符
写法一:(构建对象)
class CAbs
{
public:
int m_iA;
public:
CAbs(int iA);
// CAbs(const CAbs &abs);
void operator()();
};
最好别构建拷贝构造函数,如果有拷贝构造函数,拷贝构造函数将会运行两次
root@ubuntu:/home/sulier/work/C++11/ThreadTest# ./ThreadTest
class create
===========
class create copy
class create copy
operator
CAbs::CAbs(int iA):m_iA(iA)
{
cout<<"class create"<<endl;
}
/*
CAbs::CAbs(const CAbs &abs):m_iA(abs.m_iA)
{
cout<<"class create copy"<<endl;
}*/
void CAbs::operator()()
{
cout<<"operator"<<endl;
return;
}
int main()
{
CAbs abs(10);
cout<<"==========="<<endl;
thread th(abs);
th.join();//主线程等待子线程完成退出
return 0;
}
写法二:(不构建对象)
void CAbs::operator()()
{
cout<<"operator"<<endl;
return;
}
int main()
{
cout<<"==========="<<endl;
thread th((CAbs(10)));
th.join();//主线程等待子线程完成退出
return 0;
}
C.4》创建线程通过类的静态函数
class CAbs
{
public:
int m_iA;
public:
CAbs(int iA);
void operator()();
static void Run();
};
void CAbs::Run()
{
cout<<"Run let's go"<<endl;
}
int main()
{
cout<<"==========="<<endl;
thread th(&CAbs::Run);
th.join();//主线程等待子线程完成退出
return 0;
}
C.4》创建线程通过类对象的非静态函数
class CAbs
{
public:
int m_iA;
public:
CAbs(int iA);
void operator()();
static void Run();
void Product();
void Consumer();
};
void CAbs::Product()
{
cout<<"Product:"<<m_iA<<endl;
return;
}
void CAbs::Consumer()
{
cout<<"Consumer:"<<m_iA<<endl;
return;
}
int main()
{
cout<<"==========="<<endl;
CAbs abs(2);
thread th0(&CAbs::Product, &abs);
thread th1(&CAbs::Consumer, &abs);
th0.join();//主线程等待子线程完成退出
th1.join();
return 0;
}
结果:
root@ubuntu:/home/sulier/work/C++11/ThreadTest# g++ -o ThreadTest ThreadTest.cpp -lpthread -std=c++11
root@ubuntu:/home/sulier/work/C++11/ThreadTest# ./ThreadTest
===========
class create
Product:Consumer:22
2.C++创建简单线程互斥锁(生产消费者模式)
前提概述:500毫秒生产,通过数据共享利用互斥锁达到一生产一消费模式
需要额外增加互斥、条件变量以及chrono,也用到lamda表达式
#include<thread>
#include<chrono>
#include<mutex>
#include<condition_variable>
class CAbs
{
public:
deque<int> dataBase;
mutex cvMtx;
bool m_bWork;
int m_iIndex;
int m_max;
condition_variable cv;
public:
CAbs(bool bWork,int index, int max);
void Product();
void Consumer();
};
因为lock_guard只支持构造函数内对互斥量加锁,所以此时不可用
CAbs::CAbs(bool bWork, int index, int max):m_bWork(bWork),m_iIndex(index),m_max(max)
{
cout<<"class create"<<endl;
}
void CAbs::Product()
{
while (m_bWork)
{
this_thread::sleep_for(chrono::milliseconds(500));
//lock_guard<mutex> lk(cvMtx);
unique_lock<mutex> lk(cvMtx);
cv.wait(lk,[this](){return this->dataBase.size() <= this->m_max;});
dataBase.push_back(m_iIndex);
cout<<"Product:"<<m_iIndex<<endl;
m_iIndex++;
cv.notify_all();
}
return;
}
void CAbs::Consumer()
{
while (m_bWork)
{
//lock_guard<mutex> lk(cvMtx);
unique_lock<mutex> lk(cvMtx);
cv.wait(lk,[this](){return !this->dataBase.empty();});
int iTemp = dataBase.front();
cout<<"Consumer:"<<iTemp<<endl;
dataBase.pop_front();
cv.notify_all();
}
return;
}
int main()
{
CAbs abs(true,1,3);
thread th0(&CAbs::Product, &abs);
thread th1(&CAbs::Consumer, &abs);
th0.detach();//主线程和子线程分离
th1.detach();
cout<<"==========="<<endl;
sleep(3);
abs.m_bWork = false;
return 0;
}