Skip to content

Commit 392f607

Browse files
committed
Thread/ThreadPool: Unit test waitUntilFinished
1 parent f4100e2 commit 392f607

4 files changed

Lines changed: 17 additions & 24 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,12 @@ xmake [b|build] [-vD]
143143

144144
### Build & Run tests
145145
```console
146-
xmake [r|run]
146+
xmake [r|run] [-d|--debug]
147147
```
148148

149149
### Watch tests
150150
```console
151-
xmake watch -r
151+
xmake watch -r [-d|--debug]
152152
```
153153

154154
### Contribute

modules/Thread/ThreadPool.mpp

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ export namespace CppUtils::Thread
2525
.start();
2626
}
2727

28-
ThreadPool(const ThreadPool&) = delete;
29-
ThreadPool& operator=(const ThreadPool&) = delete;
30-
ThreadPool(ThreadPool&&) = delete;
31-
ThreadPool& operator=(ThreadPool&&) = delete;
28+
inline ThreadPool(const ThreadPool&) = delete;
29+
inline ThreadPool& operator=(const ThreadPool&) = delete;
30+
inline ThreadPool(ThreadPool&&) = delete;
31+
inline ThreadPool& operator=(ThreadPool&&) = delete;
3232

33-
~ThreadPool()
33+
inline ~ThreadPool()
3434
{
3535
for (auto& worker : m_workers)
3636
worker.requestStop();
@@ -57,8 +57,8 @@ export namespace CppUtils::Thread
5757

5858
inline auto waitUntilFinished() -> void
5959
{
60-
auto lockGuard = std::unique_lock{m_waitingMutex};
61-
if (m_activeWorkers != 0 and not isTasksQueueEmpty())
60+
if (auto lockGuard = std::unique_lock{m_waitingMutex};
61+
m_activeWorkers != 0 or not isTasksQueueEmpty())
6262
m_waitUntilFinishedCondition.wait(lockGuard, [this] {
6363
return m_activeWorkers == 0 and isTasksQueueEmpty();
6464
});
@@ -77,14 +77,6 @@ export namespace CppUtils::Thread
7777
return std::empty(tasksQueueAccessor.value());
7878
}
7979

80-
[[nodiscard]] inline auto isStopRequested() const noexcept -> bool
81-
{
82-
for (const auto& worker : m_workers)
83-
if (worker.isStopRequested())
84-
return true;
85-
return false;
86-
}
87-
8880
inline auto workerThread() -> void
8981
{
9082
if (isTasksQueueEmpty())
@@ -93,6 +85,7 @@ export namespace CppUtils::Thread
9385
m_startWorkingCondition.wait(tasksQueueAccessor.getLockGuard());
9486
}
9587

88+
m_activeWorkers.fetch_add(1, std::memory_order_relaxed);
9689
auto task = Task{};
9790
{
9891
auto tasksQueueAccessor = m_tasksQueue.access();
@@ -101,8 +94,6 @@ export namespace CppUtils::Thread
10194
task = std::move(tasksQueueAccessor->front());
10295
tasksQueueAccessor->pop();
10396
}
104-
105-
m_activeWorkers.fetch_add(1, std::memory_order_relaxed);
10697
try
10798
{
10899
task();

tests/Terminal/Canvas.mpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export namespace CppUtils::UnitTest::Terminal::Canvas
3636
for (auto i = 0uz; i < 5; ++i)
3737
for (auto c : chars)
3838
{
39-
std::this_thread::sleep_for(100ms);
39+
std::this_thread::sleep_for(50ms);
4040
canvas.fill(c);
4141
canvas.update();
4242
}
@@ -54,7 +54,7 @@ export namespace CppUtils::UnitTest::Terminal::Canvas
5454
for (auto i = 0uz; i <= 100; ++i)
5555
{
5656
progressBar.setPercent(i);
57-
std::this_thread::sleep_for(20ms);
57+
std::this_thread::sleep_for(5ms);
5858
}
5959
});
6060

@@ -95,7 +95,7 @@ export namespace CppUtils::UnitTest::Terminal::Canvas
9595
canvas.print(charPosition, "O");
9696

9797
canvas.update();
98-
std::this_thread::sleep_for(10ms);
98+
std::this_thread::sleep_for(5ms);
9999
}
100100
});
101101
}};

tests/Thread/ThreadPool.mpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import CppUtils;
66
export namespace CppUtils::UnitTest::Thread::ThreadPool
77
{
88
inline auto _ = CppUtils::UnitTest::TestSuite{"Thread/ThreadPool", {"UnitTest"}, [](auto& suite) {
9+
using namespace std::chrono_literals;
910
using Logger = CppUtils::Logger<"CppUtils">;
1011

1112
suite.addTest("0 task / 1 thread", [&] {
@@ -32,13 +33,14 @@ export namespace CppUtils::UnitTest::Thread::ThreadPool
3233
suite.expect(called);
3334
});
3435

35-
suite.addTest("1 task without future.wait()", [&] {
36+
suite.addTest("waitUntilFinished", [&] {
3637
auto called = std::atomic_bool{false};
3738
{
3839
auto threadPool = CppUtils::Thread::ThreadPool{};
39-
auto future = threadPool.call([&called] {
40+
threadPool.call([&called] {
4041
called = true;
4142
});
43+
threadPool.waitUntilFinished();
4244
}
4345
suite.expect(called);
4446
});

0 commit comments

Comments
 (0)