Closed paulerikf closed 2 years ago
This is definitely a bug as the docs example crashes as well, this may be related to #23. I will investigate an report back, I can reproduce the crash.
During copy/move construction (which is triggered when a task is inserted into a vector), ThreadPool::free
, which stores the function pointer for the thread, is erroneously called here. Because both the original and the newly move-constructed tasks point to the same global storage in ThreadPool
, when the old task goes out of scope, it also deallocates the storage of the new task that is now in the vector. When the task attempts to execute, it segfaults because the storage is no longer there.
Fixed by this, the change is simple but I want to take my time and update the testing suite, I apologize for the fact this wasn't caught, it really should have been since the docs example explicitly calls it like this.
Thank you for bringing this up, I will try open a PR tomorrow.
Thanks for the quick fix @Clemapfel!
Noticed one remaining issue for the Task\<void> case: ==
should be !=
.
Also, unrelated, but your docs are really really great. Thanks for all the work you put into this!
Hey Clem, sorry to bother you with another issue. Not sure if I'm missing something basic here, but struggling to get multithreading working.
Quick stats before getting into things:
unsafe::gc_disable()
Ok, so I've been working my way through the multithreading docs, and things were great until I started trying to manage Task lifetimes.
Tasks seem to terminate the moment the original task object goes out of scope, even when it's added to an std::vector which is still in scope. If I create a task with
auto task = ThreadPool::create(f)
then addtask
to a std::vector, the task terminates whentask
goes out of scope, despite the vector still being in scope. Even more immediate, when I directly add the task to the std::vector withtasks.push_back(ThreadPool::create(f))
the program segfaults the moment I attempttasks.back().schedule()
.Not sure if that explanation made any sense, here are a few examples to hopefully illustrate what I'm seeing.
Basic example
Example with scope block
Example from multithreading docs