threadpool small fix
This commit is contained in:
parent
7f0801b2be
commit
cb38073236
@ -27,10 +27,12 @@ public:
|
||||
template <typename R> std::future<R> add_task(std::function<R()> func)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
std::packaged_task<R()> pt(func);
|
||||
std::future<R> fut = pt.get_future();
|
||||
_queue.push([&pt]{ pt(); });
|
||||
_cv.notify_all();
|
||||
std::shared_ptr<std::packaged_task<R()>> pt_ptr = std::make_shared<std::packaged_task<R()>>(func);
|
||||
std::future<R> fut = pt_ptr->get_future();
|
||||
|
||||
std::function<void()> tf = [pt_ptr] { (*pt_ptr)(); };
|
||||
_queue.push(tf);
|
||||
_cv.notify_one();
|
||||
return fut;
|
||||
}
|
||||
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
#include "ustring.h"
|
||||
#include "utf.h"
|
||||
#include "logger.h"
|
||||
#include "threadpool.h"
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
@ -578,14 +579,9 @@ int main(int argc, char **argv)
|
||||
//::testing::InitGoogleTest(&argc, argv);
|
||||
//return RUN_ALL_TESTS();
|
||||
|
||||
/*
|
||||
const std::size_t threadsCount = 2;
|
||||
|
||||
//int one = 1;
|
||||
//double two = 2.5;
|
||||
//ulog.log("one: %, two: %", one, two);
|
||||
//ulog.log("Hello world!");
|
||||
//ulog.log("some string: %", "qwe!");
|
||||
|
||||
std::thread threads[threadsCount];
|
||||
for(std::size_t i = 0; i < threadsCount; ++i)
|
||||
{
|
||||
@ -596,6 +592,24 @@ int main(int argc, char **argv)
|
||||
|
||||
for(std::size_t i = 0; i < threadsCount; ++i)
|
||||
threads[i].join();
|
||||
*/
|
||||
|
||||
threadpool pool(4);
|
||||
|
||||
for (std::size_t i = 0; i < 100; ++i)
|
||||
{
|
||||
std::function<int()> func = [i]() -> int {
|
||||
auto id = std::this_thread::get_id();
|
||||
std::this_thread::sleep_for(std::chrono::seconds(5));
|
||||
ulog.log_info("task % with id %", i, id);
|
||||
return 0;
|
||||
};
|
||||
|
||||
pool.add_task(func);
|
||||
}
|
||||
|
||||
int n;
|
||||
std::cin >> n;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user