Skip to content

Commit

Permalink
Proposal for fixing the issue skypjack#292
Browse files Browse the repository at this point in the history
Signed-off-by: Fiorentino, Stefano <[email protected]>
  • Loading branch information
fiorentino-at-adesso committed Jul 7, 2023
1 parent d88bfca commit e691cda
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/uvw/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ namespace uvw {
UVW_INLINE thread::thread(loop::token token, std::shared_ptr<loop> ref, task t, std::shared_ptr<void> d) noexcept
: uv_type{token, std::move(ref)},
data{std::move(d)},
func{std::move(t)} {}
func{std::move(t)},
joined{false} {}

UVW_INLINE void thread::create_callback(void *arg) {
thread &curr = *(static_cast<thread *>(arg));
Expand Down Expand Up @@ -42,7 +43,10 @@ UVW_INLINE bool thread::run(create_flags opts, std::size_t stack) noexcept {
}

UVW_INLINE bool thread::join() noexcept {
return (0 == uv_thread_join(raw()));
if(!joined && 0 == uv_thread_join(raw())) {
joined = true;
}
return joined;
}

UVW_INLINE thread_local_storage::thread_local_storage(loop::token token, std::shared_ptr<loop> ref) noexcept
Expand Down
1 change: 1 addition & 0 deletions src/uvw/thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ class thread final: public uv_type<uv_thread_t> {
private:
std::shared_ptr<void> data;
task func;
bool joined;
};

/**
Expand Down
28 changes: 28 additions & 0 deletions test/uvw/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,31 @@ TEST(Mutex, RecursiveLockUnlock) {

loop->run();
}

TEST(Thread, joined)
{
auto loop = uvw::loop::get_default();
auto threadWorker = [](std::shared_ptr<void> d) {
std::cout << "all done" << std::endl;
};

std::vector<std::shared_ptr<uvw::thread>> threads;
for (int i = 0; i < 8; i++)
{
auto threadId = std::make_shared<int>(i);
auto threadHandle = loop->resource<uvw::thread>(threadWorker, threadId);
threads.push_back(threadHandle);
}

for (auto& threadHandle : threads)
{
threadHandle->run();
}

loop->run();

for (auto& threadHandle : threads)
{
threadHandle->join();
}
}

0 comments on commit e691cda

Please sign in to comment.