高端平面网站,可视化设计最重要的是确定网站的,无锡城乡建设部网站首页,重庆网站建设网站建设1.基础概念
并发#xff08;Concurrency#xff09;
并发是指在同一时间段内#xff0c;多个任务看起来像是同时执行的。并发并不一定意味着真正的同时执行#xff0c;它可以是通过时间片轮转等方式在多个任务之间快速切换#xff0c;让用户感觉多个任务在同时进行。并发…1.基础概念
并发Concurrency
并发是指在同一时间段内多个任务看起来像是同时执行的。并发并不一定意味着真正的同时执行它可以是通过时间片轮转等方式在多个任务之间快速切换让用户感觉多个任务在同时进行。并发可以通过多线程、多进程等方式实现。
线程Thread
线程是进程中的一个执行单元是 CPU 调度和分派的基本单位。一个进程可以包含多个线程这些线程共享进程的内存空间和系统资源但每个线程有自己独立的栈空间和执行上下文。线程之间的通信和数据共享比进程更加方便和高效。比如在一个浏览器进程中可能会有负责渲染页面的线程、处理网络请求的线程等。
多线程Multithreading
多线程是实现并发的一种方式它允许一个进程中同时存在多个线程这些线程可以并行在多核 CPU 上或并发在单核 CPU 上执行不同的任务从而提高程序的执行效率和响应速度。
进程Process
进程是程序在操作系统中的一次执行过程是系统进行资源分配和调度的基本单位。每个进程都有自己独立的内存空间、系统资源如文件描述符等和执行上下文。不同进程之间相互独立一个进程的崩溃通常不会影响其他进程。例如当你打开一个浏览器、一个文本编辑器时它们分别对应着不同的进程。(一个进程有一个主线程)
2 .操作多线程
2.1 创造线程
在 C 中thread 库提供了用于管理线程的类和函数它是 C11 标准引入的用于支持多线程编程
默认构建
std::thread t;
带执行函数的构造函数创建一个新线程并执行指定的函数。可以传递参数给该函数
#include iostream
#include threadvoid func(int a) {std::cout 线程执行参数: a std::endl;
}int main() {std::thread t(func, 42);t.join(); // 等待线程执行完毕return 0;
}
记得 线程只能出传入右值
右值引用与线程
#include iostream
#include thread
#include string// 线程函数接受一个字符串参数
void threadFunction(std::string str) {std::cout 线程接收到的字符串: str std::endl;
}int main() {// 使用右值引用传递临时对象给线程std::thread t(threadFunction, std::string(Hello, Thread!));// 等待线程执行完毕t.join();return 0;
}
左值引用与线程
左值引用用于引用一个已经存在的对象。在多线程编程中如果需要在线程函数中修改外部对象或者避免对象的拷贝可以使用左值引用
#include iostream
#include thread
#include string// 线程函数接受一个字符串的左值引用
void threadFunction(std::string str) {str - Modified by thread;std::cout 线程修改后的字符串: str std::endl;
}int main() {std::string message Initial message;// 使用左值引用传递对象给线程std::thread t(threadFunction, std::ref(message));//ref提取地址// 等待线程执行完毕t.join();std::cout 主线程看到的修改后的字符串: message std::endl;return 0;
}
因为线程只能右值传递,所有以引用形式传递对象值传递和引用之间的区别 你可以理解为复制和对地址操作关于右值的话c新特性之 左右值 lambda 以及“for”-CSDN博客
成员函数
普通
#include iostream
#include threadclass MyClass {
public:// 普通成员函数void memberFunction(int value) {std::cout 线程正在执行成员函数传入的值是: value std::endl;}
};int main() {MyClass obj;int param 42;// 创建线程并引用类的普通成员函数std::thread t(MyClass::memberFunction, obj, param);// 等待线程执行完毕t.join();return 0;
}
静态
#include iostream
#include threadclass MyClass {
public:// 普通成员函数void memberFunction(int value) {std::cout 线程正在执行成员函数传入的值是: value std::endl;}
};int main() {MyClass obj;int param 42;// 创建线程并引用类的普通成员函数std::thread t(MyClass::memberFunction, obj, param);// 等待线程执行完毕t.join();return 0;
}
2.2成员函数
join函数
join() 函数的作用是阻塞当前线程直到被调用 join() 的 std::thread 对象所代表的线程执行完毕。也就是说当在一个线程通常是主线程中调用另一个线程对象的 join() 方法时当前线程会暂停执行等待目标线程执行结束后才会继续执行后续代码。
#include iostream
#include thread// 线程函数
void threadFunction() {for (int i 0; i 5; i) {std::cout 子线程输出: i std::endl;}
}int main() {std::thread t(threadFunction);std::cout 主线程等待子线程执行完毕... std::endl;t.join(); // 主线程阻塞等待子线程执行完毕std::cout 子线程执行完毕主线程继续执行。 std::endl;return 0;
}
detch 函数
detach() 函数用于将 std::thread 对象所代表的线程与该对象分离让线程在后台独立执行。分离后的线程在执行完毕后会自动释放资源而不需要主线程调用 join() 来等待它结束。一旦线程被分离就无法再通过 join() 来等待它也不能再使用该 std::thread 对象来管理这个线程。
#include iostream
#include thread
#include chrono// 线程函数
void detachedThreadFunction() {std::this_thread::sleep_for(std::chrono::seconds(2));std::cout 分离的线程执行完毕。 std::endl;
}int main() {std::thread t(detachedThreadFunction);std::cout 主线程不等待分离的线程继续执行。 std::endl;t.detach(); // 分离线程std::cout 主线程继续执行其他任务... std::endl;// 主线程可以继续执行其他任务不需要等待分离的线程return 0;
}
2.3detach陷阱
你考虑一下 上面是不是陷入陷阱
应该是的 因为
#include iostream
#include thread
#include chrono// 线程函数
void detachedThreadFunction() {std::this_thread::sleep_for(std::chrono::seconds(2));//等待2s才进行std::cout 分离的线程执行完毕。 std::endl;
}int main() {std::thread t(detachedThreadFunction);std::cout 主线程不等待分离的线程继续执行。 std::endl;t.detach(); // 分离线程std::cout 主线程继续执行其他任务... std::endl;// 让主线程等待足够长的时间确保分离的线程执行完毕std::this_thread::sleep_for(std::chrono::seconds(3)); return 0;
}
你会发现主线程已经结束了
2.4 异步执行的特性
#include iostream
#include thread
#include chrono
#include vectorvoid threadFunction(int id) {// 模拟线程的工作负载int iid;if (id%20)idid*3;std::this_thread::sleep_for(std::chrono::seconds(id));std::cout Thread i finished its work. std::endl;
}int main() {const int numThreads 5;std::vectorstd::thread threads;// 创建多个线程for (int i 0; i numThreads; i) {threads.emplace_back(threadFunction, i 1);}// 等待所有线程完成for (auto thread : threads) {thread.join();}std::cout All threads have finished. std::endl;return 0;
}
//就会清晰的发现 结果
Thread 1 finished its work.
Thread 3 finished its work.
Thread 5 finished its work.
Thread 2 finished its work.
Thread 4 finished its work.windows按工作时间来的不是按顺序