Thread创建线程
时间: 2023-08-22 22:10:35 浏览: 45
引用[2]中提到了通过Executors创建线程池的方式,其中包括了Executors.newFixedThreadPool()、Executors.newCachedThreadPool()、Executors.newSingleThreadExecutor()、Executors.newScheduledThreadPool()、Executors.newSingleThreadScheduledExecutor()和Executors.newWorkStealingPool()等方法。这些方法都是通过调用ThreadPoolExecutor的构造函数来创建线程池的。而引用[3]中展示了最原始的创建线程池的方式,即通过直接实例化ThreadPoolExecutor类来创建线程池,并设置相应的参数。在这个例子中,通过ThreadPoolExecutor的构造函数传入了核心线程数、最大线程数、线程空闲时间、时间单位和任务队列等参数来创建线程池。然后通过调用execute方法来提交任务。所以,Thread类并不是直接用来创建线程池的,而是通过ThreadPoolExecutor类来创建线程池。
相关问题
std::thread创建线程
使用std::thread可以创建线程。可以通过以下几种方式来创建线程:
1. 使用普通函数创建线程:通过将函数名作为参数传递给std::thread的构造函数来创建线程。例如,可以定义一个普通函数functionToThread,并将其作为参数传递给std::thread的构造函数。
2. 使用lambda表达式创建线程:可以使用lambda表达式来定义线程的执行代码,并将其作为参数传递给std::thread的构造函数。
注意事项:
- std::thread对象只具有移动属性,不具备拷贝属性。
- 每个线程都具有唯一的id,可以使用std::this_thread::get_id()函数来获取当前线程的id,也可以通过std::thread对象的成员函数get_id()来获取。
示例代码如下:
#include <iostream>
#include <thread>
using namespace std;
void functionToThread() {
cout << "线程启动......" << endl;
cout << "线程结束......" << endl;
}
int main() {
// 使用普通函数创建线程
thread myThread(functionToThread);
// 使用lambda表达式创建线程
auto lambdaThread = [](){
cout << "线程启动......" << endl;
cout << "线程结束......" << endl;
};
thread myThread2(lambdaThread);
// 等待线程执行完毕
myThread.join();
myThread2.join();
return 0;
}
<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* *3* [使用 std::thread 创建线程](https://blog.csdn.net/lizhichao410/article/details/123547768)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 100%"]
[ .reference_list ]
std::thread 创建线程
std::thread是C++标准库中用于创建线程的类。使用std::thread,可以在一个新的线程中执行指定的函数或成员函数。要创建一个线程,可以使用std::thread的构造函数。构造函数的参数可以是一个函数指针、函数对象或Lambda表达式,以及该函数或函数对象的参数。例如,可以使用以下方式创建一个线程:std::thread t(myFunction);其中myFunction是一个函数名。另外,也可以在创建线程时传递参数,例如:std::thread t(myFunction, arg1, arg2);其中arg1和arg2是myFunction的参数。创建线程后,可以通过调用它的join()函数等待线程执行完毕,或者调用它的detach()函数将线程分离,使其在后台运行。需要注意的是,创建线程时需要包含头文件<thread>。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* *3* [数据库课程设计——实验报告管理系统(超详细)](https://blog.csdn.net/ViolinW/article/details/122320084)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"]
[ .reference_list ]
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)