Open hotwatermorning opened 2 years ago
thank you, It seems like a bug, i'll check it soon!
hi, i send a patch here that may fix this issue.
btw, the testing code above may have dead-lock and we need set larger thread pool size other than 10. the situration may be -- thread A is waiting for the idle state of thread pool. thread B is waiting for thread A call resolve.
Hi, Thank you for checking out this issue.
The patch adds a condition variable cond_
and waits for cond_
to be notified, but no one sends any notification to cond_
.
It seems that's why the deadlock occurs.
Thus the code which sends notification to cond_
should be added to promise_inl.hpp, I suppose.
hi, i send a patch here that may fix this issue.
btw, the testing code above may have dead-lock and we need set larger thread pool size other than 10. the situration may be -- thread A is waiting for the idle state of thread pool. thread B is waiting for thread A call resolve.
Hi, this deadlock can be reproduced , as follows
#include <gtest/gtest.h>
#include <thread>
#include <promise/promise.hpp>
#include <condition_variable>
struct Event {
std::condition_variable_any cv {};
std::mutex mtx {};
bool isset{false};
std::unique_lock<std::mutex> lock() {
return std::unique_lock{mtx};
}
void wait() {
auto l = lock();
while (!isset) cv.wait(l);
}
void set() {
auto l = lock();
isset = true;
cv.notify_all();
}
};
TEST(promise, parallel_lock) {
auto pms = promise::newPromise();
Event race;
pms
.then([&](int val){
auto rslt = promise::resolve(std::string{"main resolve"});
race.set();
return rslt;
})
.fail([](const std::string& str){
return std::string{str};
});
std::thread t {[&](){
race.wait();
std::this_thread::sleep_for(std::chrono::milliseconds(10));
pms.then([](const std::string& str){
return std::string{str};
});
}};
pms.resolve(1);
if (t.joinable()) t.join();
EXPECT_TRUE(true);
}
also reqired path for better reproducibility
https://github.com/xhawk18/promise-cpp/blob/556dce7183dc76a229e5f6f82bd5e7ef1203bbfd/include/promise-cpp/promise_inl.hpp#L291
std::this_thread::sleep_for(std::chrono::milliseconds(100));
test on #556dce718
There is a proposal to extend the kProccess states enum. According to which tasks will be added to the chain, but will not be executed if processing has already begun in another thread.
Hi, I'm trying to use promise-cpp in multi-threaded programs, but sometimes the following assertion error occurs in promise-cpp.
promise_inl.hpp(161)
The README document says promise-cpp is thread safe but It seems not thread safe in some situation.
Is this a bug? Or should I fix how I use promise-cpp? Thanks.
Step to reproduce:
Build and run this code snippet.