std::make_unique<std::thread>
时间: 2023-08-26 20:11:58 浏览: 69
引用中提到,std::make_unique函数只是把参数完美转发给要创建对象的构造函数,再从new出来的原生指针构造std::unique_ptr智能指针。因此,std::make_unique<std::thread>的作用是创建一个std::thread对象,并使用std::unique_ptr进行管理。注意,std::make_unique<std::thread>函数不支持数组和自定义删除器。
相关问题
std::shared_ptr<ThreadX::ITask>
对于std::shared_ptr<ThreadX::ITask>,建议使用std::make_shared来构造智能指针,以提高效率。例如:
std::shared_ptr<ThreadX::ITask> taskPtr = std::make_shared<ThreadX::ITask>(); [1]
另外,std::weak_ptr是一种非拥有性引用,可以用于对被std::shared_ptr管理的对象进行访问。在使用之前,需要将其转换为std::shared_ptr。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *3* [《c++11学习笔记》 --- 智能指针(std::shared_ptr、std::uniq_ptr、std::weak_ptr)](https://blog.csdn.net/qq_41653875/article/details/121780253)[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: 50%"]
- *2* [c++11:智能指针之std::unique_ptr、std::shared_ptr、std::weak_ptr](https://blog.csdn.net/weixin_40179091/article/details/109757523)[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: 50%"]
[ .reference_list ]
#include <vector> #include <queue> #include <thread> #include <mutex> #include <condition_variable> #include <functional> #include <future> class ThreadPool { public: ThreadPool(size_t threads) : stop(false) { for (size_t i = 0; i < threads; ++i) { workers.emplace_back([this] { for (;;) { std::function<void()> task; { std::unique_lockstd::mutex lock(this->queue_mutex); this->condition.wait(lock, [this] { return this->stop || !this->tasks.empty(); }); if (this->stop && this->tasks.empty()) return; task = std::move(this->tasks.front()); this->tasks.pop(); } task(); } }); } } ~ThreadPool() { { std::unique_lockstd::mutex lock(queue_mutex); stop = true; } condition.notify_all(); for (std::thread &worker : workers) worker.join(); } template<class F, class... Args> auto enqueue(F&& f, Args&&... args) -> std::future<typename std::result_of<F(Args...)>::type> { using return_type = typename std::result_of<F(Args...)>::type; auto task = std::make_shared<std::packaged_task<return_type()>>(std::bind(std::forward<F>(f), std::forward<Args>(args)...)); std::future<return_type> res = task->get_future(); { std_lockstd::mutex lock(queue_mutex); if (stop) throw std::runtime_error("enqueue on stopped ThreadPool"); tasks.emplace([task] { (*task)(); }); } condition.notify_one(); return res; } private: std::vectorstd::thread workers; std::queue<std::function<void()>> tasks; std::mutex queue_mutex; std::condition_variable condition; bool stop; }; 怎么获取结果
可以使用`std::future`来获取线程池中任务的返回值。在`enqueue`函数中,任务被包装在`std::packaged_task`中,这个包装器可以将一个函数包装成一个可调用对象,并且可以使用`std::future`来获取函数的返回值。
在`enqueue`函数中,我们使用`std::make_shared`创建了一个`std::packaged_task`,并将要执行的任务`f`和其参数`args`绑定在一起。然后,我们将这个`std::packaged_task`封装在一个`std::shared_ptr`中,以便可以在其他线程中访问它。
接下来,我们使用`std::future`获取`std::packaged_task`的返回值。`std::future`是一个异步结果的占位符,可以用来检查任务是否已经完成,并且可以获取任务的返回值。
具体地,我们可以在调用`enqueue`函数后,使用返回的`std::future`对象的`get()`函数来获取任务的返回值。`get()`函数会阻塞当前线程,直到任务执行完毕并返回结果。
例如,假设我们要执行一个函数`int add(int x, int y)`,我们可以使用以下方式来获取其结果:
```c++
ThreadPool pool(4); // 创建线程池,有4个线程
// 将任务加入线程池,并获取返回值的future对象
auto result = pool.enqueue(add, 3, 4);
// 等待任务执行完成,并获取返回值
int res = result.get();
std::cout << "3 + 4 = " << res << std::endl;
```
这里的`result.get()`会阻塞当前线程,直到任务执行完毕并返回结果。最后,我们将`res`输出到控制台。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)