学习目标:
1.main函数里使用thread,调用普通函数
2.main函数里使用thread,调用class中的函数
3.在class中声明thread成员,调用class中线程函数
4.结合lamda表达式,使用thread
5.不同thread间,线程同步、互斥访问
6.线程优先级如何设置?
7.thread_pool实现
目标1:main函数里使用thread,调用普通函数
main函数中,启动thread调用全局函数,
- 无参:void ThreadFun();
- 有参:void ThreadFunParam(int i,int j,char* pszArr);
代码如下:
// thread example
#include <iostream> // std::cout
#include <thread> // std::threa
void ThreadFun()
{
// do stuff...
std::cout << "ThreadFun running" << std::endl;
}
void ThreadFunParam(int i,int j,char* pszArr)
{
std::cout << "ThreadFunParam running" << std::endl;
std::cout << "i:" << i << ",j:" << j << std::endl;
if (nullptr != pszArr)
{
std::cout << "pszArr:" << pszArr << std::endl;
}
else
{
std::cout << "pszArr is nullptr" << std::endl;
}
}
int main()
{
std::thread first(ThreadFun); // spawn new thread that calls ThreadFun()
char szArr[100] = "hello thread world!";
std::thread secondThread(ThreadFunParam, 10, 20,szArr);// spawn new thread that calls ThreadFunParam()
std::cout << "main, foo and bar now execute concurrently...\n";
// synchronize threads:
first.join(); // pauses until first finishes
secondThread.join();
std::cout << "Thread completed.\n";
return 0;
}
目标2:main函数里使用thread,调用class中的函数
main函数中启动thread,调用class中的函数
- 无参:void foo()
- 有参:void funParam(int i)
- 静态有参:static void funStatic(int i)
#include <thread>
#include <iostream>
class bar {
public:
void foo() {
std::cout << "hello from member function" << std::endl;
}
void funParam(int i)
{
std::cout << "funParam fun start,i:"<<i << std::endl;
}
static void funStatic(int i)
{
std::cout << "funStatic fun start,i:" << i << std::endl;
}
};
int main()
{
std::thread t(&bar::foo, bar());
t.join();
std::thread t2(&bar::funParam, bar(), 10);
t2.join();
std::thread t3(bar::funStatic, 10);
t3.join();
std::cout << "main end" << std::endl;
return 0;
}
目标3:在class中声明thread成员,调用class中线程函数
- 无参: void foo()
- 有参: void funParam(int i)
- 静态有参: static void funStatic(int i)
#include <thread>
#include <iostream>
class bar {
public:
bar()
{
pt3 = nullptr;
}
//~bar()
//{
// /* if (nullptr != pt3)
// {
// delete pt3;
// pt3 = nullptr;
// }*/
//}
void foo() {
std::cout << "foo fun running" << std::endl;
}
void funParam(int i)
{
std::cout << "funParam fun start,i:" << i << std::endl;
}
static void funStatic(int i)
{
std::cout << "funStatic fun start,i:" << i << std::endl;
}
void startThread3()
{
pt3 = new std::thread(funStatic,30);
pt3->join();
//pt3->detach();
}
std::thread* pt3;
void startThread2()
{
t2 = std::thread(&bar::foo, bar());
t2.join();
//t2.detach();
}
std::thread t2;
void startThread()
{
t1 = std::thread(funStatic, 20);
t1.join();
}
std::thread t1;
};
int main()
{
{
bar b1;
b1.startThread();
b1.startThread2();
b1.startThread3();
}
std::cout << "main end" << std::endl;
return 0;
}
目标5:不同thread间,线程同步、互斥访问
使用std::mutex
#include <iostream>
#include <thread>
#include <mutex>
int i = 0;
std::mutex m;
void threadFun()
{
std::lock_guard<std::mutex> locker(m);
//m.lock();
i++;
std::cout << "threadFun:" << i << ",this_threadid:"<<std::this_thread::get_id() << std::endl;
//m.unlock();
}
int main()
{
std::thread t[10];
for (int j = 0; j < 10; j++)
{
t[j] = std::thread(threadFun);
}
for (int j = 0; j < 10; j++)
{
t[j].join();
}
return 0;
}