1、并发、进程、线程的基本概念
并发: 两个或多个独立的活动同时进行
进程: 计算机中的程序关于某一数据集合上的一次运行活动(exe的运行状态)
线程:
1.每一个进程都有一个主线程并且只有一个主线程
2. vs编译器中ctr+f5运行程序,实际上是主线程调用main函数的代码
3. 线程可以为一个运行代码的通道,路径,我们可以自己创建多个线程
并发的实现:
1.多线程实现并发主要的问题进程间通信的问题
No.1 一个电脑上:管道,文件,消息队列,内存共享
No.2 不同电脑通过SOCKET网络通信实现
2.单个进程,多个线程实现并发,就是一个主线程多个子线程实现并发
一个进程中的所有线程共享的内存空间 例如:全局变量,指针引用
2.线程的多种创建方式
C++线程创建过程:
2.1 包含头文件:调用thread类去创建一个线程对象
2.2 创建线程:调用thread类去创建一个线程对象
2.3 join函数:加入,汇合线程,阻塞主线程,等待子线程执行结束,才回到主线中
2.4 detach()函数:分离,打破依赖关系,把子线程驻留后台。当线程detach()后就不能够再join()。
2.5 joinable()判断当前线程是否可以做join或者detach过程,可以返回true,不可以返回false。
注意点:
1.创建一个线程,不做处理,会调用abort函数终止的,如下图所示:

2.一个线程只能join一次,不能重复join
3、其他创建线程方法
3.1 普通函数方式
例子:
#include <iostream>
#include <thread>
#include <Windows.h>
using namespace std;
// 线程处理函数
void print1()
{
Sleep(5000);
cout << "子线程1运行..." << endl;
}
void print2()
{
Sleep(5000);
cout << "子线程2运行..." << endl;
}
int main()
{
// 创建线程
thread test1(print1);
test1.detach(); // 阻塞
cout << "主线程..." << endl;
if (test1.joinable())
{
test1.detach(); // 阻塞
}
else
{
cout << "子线程已被处理" << endl;
}
return 0;
}
3.2 通过类和对象
例子:
#include <iostream>
#include <thread>
using namespace std;
class MM
{
public:
// STL 仿函数
// print() MM()
void operator()()
{
cout << "子线程启动....." << endl;
}
};
int main()
{
// MM(); // MM 无名对象()
// 正常写法:对象充当这个线程处理函数
MM mm;
thread test1(mm);
test1.join();
thread test2((MM()));
test2.join();
cout << " I LOVE SH" << endl;
return 0;
}
3.3 Lambda表达式创建线程
例子:
#include <iostream>
#include <thread>
using namespace std;
int Max(int a, int b)
{
return a > b ? a : b;
}
int main()
{
int (*pMax)(int, int) = nullptr;
pMax = [](int a, int b)->int {return a > b ? a : b; };
[]() {cout << "helloword" << endl; }();
cout << pMax(1, 2) << endl;
thread test1([] { cout << "子线程..." << endl; });
test1.join();
cout << "主线程运行..." << endl;
return 0;
}
3.4 带参的方式创建线程
例子:
#include <thread>
#include <iostream>
using namespace std;
void printInfo(int& num)
{
num++;
cout << "子线程\t" << num << endl;
}
int main()
{
int num = 0;
// std::ref 用于包装引用传递值
thread test1(printInfo, ref(num));
test1.join();
cout << "子线程..." <<num << endl;
return 0;
}
3.5 带智能指针的方式创建线程
例子:
#include <thread>
#include <iostream>
using namespace std;
void print(unique_ptr<int>ptr)
{
cout << "子线程:" << ptr.get() << endl;
cout << this_thread::get_id() << endl;
}
int main()
{
int* p = new int(1000);
cout << *p << endl;
unique_ptr<int> ptr(new int(1000));
cout << "主线程:" << ptr.get() << endl;
thread test1(print, move(ptr));
test1.join();
cout << "主线程..." << endl;
return 0;
}
3.6 通过类的成员函数创建线程
例子:
#include <iostream>
#include <thread>
using namespace std;
class MM
{
public:
void print(int& num)
{
num = 1001;
cout << "子线程:" << this_thread::get_id() << endl;
}
};
int main()
{
MM mm;
int num = 1007;
// 需要告诉 是哪个对象
thread test1(&MM::print, mm, ref(num));
test1.join();
cout << "主线程:" << this_thread::get_id() << endl;
return 0;
}
3.7 线程访问共享数据
例子:
#include <iostream>
#include <thread>
#include <list>
using namespace std;
class Seaking
{
public:
void makeFriend()
{
for (int i = 0; i < 10000; i++)
{
cout << "喜得一枚女友:" << i<<endl;
mm.push_back(i);
}
}
void breakUp()
{
for (int i = 0; i < 10000; i++)
{
if(!mm.empty())
{
cout << "失去一枚女票" << endl;
mm.pop_front();
}
else
{
cout << "都分手了" << endl;
}
}
}
protected:
list<int>mm;
};
int main()
{
Seaking fx;
thread test1(&Seaking::makeFriend, fx);
thread test2(&Seaking::breakUp, fx);
test1.join();
test2.join();
return 0;
}
1880

被折叠的 条评论
为什么被折叠?



