Skip to content

Commit

Permalink
Activate threadpool
Browse files Browse the repository at this point in the history
  • Loading branch information
ben committed Apr 5, 2024
1 parent 1d15e76 commit f6da0cc
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
14 changes: 7 additions & 7 deletions analyze/algorithm/ObjectDetector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,20 +99,20 @@ namespace am::analyze::algorithm
mThreadsCount);

// threadpool could be replaced with std::async calls
//am::common::ThreadPool pool;
am::common::ThreadPool pool;
for (size_t rowId = 0; rowId < mThreadsCount - 1; ++rowId)
{
ImageRowSegment row{rowId * rowHeight, rowId * rowHeight + rowHeight};
futures.emplace_back(std::async(std::launch::async, startObjectsSearchInPair,
pair, row, *mConfiguration));
// futures.emplace_back(std::async(std::launch::async, startObjectsSearchInPair,
// pair, row, *mConfiguration));

//futures.emplace_back(pool.run(std::bind(&startObjectsSearchInPair, pair, row, *mConfiguration)));
futures.emplace_back(pool.run(std::bind(&startObjectsSearchInPair, pair, row, *mConfiguration)));
}

ImageRowSegment final_row{(mThreadsCount - 1) * rowHeight, pair.getHeight()};
futures.emplace_back(std::async(std::launch::async, startObjectsSearchInPair,
pair, final_row, *mConfiguration));
//futures.emplace_back(pool.run(std::bind(&startObjectsSearchInPair, pair, final_row, *mConfiguration)));
// futures.emplace_back(std::async(std::launch::async, startObjectsSearchInPair,
// pair, final_row, *mConfiguration));
futures.emplace_back(pool.run(std::bind(&startObjectsSearchInPair, pair, final_row, *mConfiguration)));
for (auto &e : futures)
{
res.emplace_back(e.get());
Expand Down
10 changes: 5 additions & 5 deletions common/ThreadPool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ namespace am::common
class ThreadPool
{
public:
// according to test on CPU with Hyper-threading feature better to use mutiplier of 2
// without hyper-threading - probably this mutiplier shall be 1, didn`t test yet
// according to test on CPU with Hyper-threading feature better to use multiplier of 2
// without hyper-threading - probably this multiplier shall be 1, didn`t test yet
explicit ThreadPool(unsigned num_threads = std::thread::hardware_concurrency() * 2)
{
while (num_threads--)
Expand Down Expand Up @@ -39,14 +39,14 @@ namespace am::common
template <typename F, typename R = std::result_of_t<F && ()>>
std::future<R> run(F &&f) const
{
auto task = std::packaged_task<R()>(std::forward<F>(f));
auto task = std::packaged_task<std::vector<am::analyze::algorithm::ObjectRectangle>()>(std::forward<F>(f));
auto future = task.get_future();
{
std::lock_guard<std::mutex> lock(mutex);
// conversion to packaged_task<void()> erases the return type
// so it can be stored in the queue. the future will still
// contain the correct type
queue.push(std::packaged_task<void()>(std::move(task)));
queue.push(std::packaged_task<std::vector<am::analyze::algorithm::ObjectRectangle>()>(std::move(task)));
}
condvar.notify_one();
return future;
Expand All @@ -69,7 +69,7 @@ namespace am::common

private:
std::vector<std::thread> threads;
mutable std::queue<std::packaged_task<void()>> queue;
mutable std::queue<std::packaged_task<std::vector<am::analyze::algorithm::ObjectRectangle>()>> queue;
mutable std::mutex mutex;
mutable std::condition_variable condvar;
};
Expand Down

0 comments on commit f6da0cc

Please sign in to comment.