Closed paulerikf closed 1 year ago
Module::safe_eval
is not thread-safe in regards to the module instance, what's happening is that the internal proxy memory management systems gets out of sync because safe_eval
modifies it concurrently to create the temporary proxies that are the result of safe_eval
.
Note how in the docs, only functions marked with [thread safe] are, c.f. https://clemens-cords.com/jluna/list.html#module. Some module functions are, some are not.
This is intended behavior but I'll at least update the docs to make it clearer so I will leave this issue open.
By adding a lock for Main, it runs without issue:
initialize(4);
auto mutex = jluna::Mutex();
auto lambda = [&]() {
while(true) {
mutex.lock();
Main.safe_eval("@info \"lambda:\" Threads.threadid()");
Main.safe_eval("sleep(1)");
mutex.unlock();
}
};
Task<void> t1 = ThreadPool::create<void()>(lambda);
t1.schedule();
while(true) {
mutex.lock();
Main.safe_eval("@info \"main:\" Threads.threadid()");
Main.safe_eval("sleep(1)");
mutex.unlock();
}
return 0;
Alternatively you could use unsafe::eval
which isn't exactly thread-safe but since you are not modifying any objects including no C++-objects, it runs through
auto lambda = [&]() {
while(true) {
"@info \"lambda:\" Threads.threadid()"_eval;
"sleep(1)"_eval;
}
};
Task<void> t1 = ThreadPool::create<void()>(lambda);
t1.schedule();
while(true) {
"@info \"lambda:\" Threads.threadid()"_eval;
"sleep(1)"_eval;
}
Ahh, ok. That makes sense.
Thanks for the explanation.
Docs updated by https://github.com/Clemapfel/jluna/commit/ae4767e8cbe1f7bcbf18f844427e9d78f556897c, I should've closed this once that commit was merged
Hey there clem, I'm running into a few different crashes whenever I try multithreading. Here's a minimal example that tends to crash in 5 seconds or less. Am I misunderstanding the docs and doing something unsafe here?
ctest --verbose output is all fine (except for the resize_array test #25) Ubuntu 20.04, Julia 1.7.1, clang-14
Minimal Example:
Most common crash output Worth noting the crash sometimes happens in
free_reference(key::UInt64)
rather thanget_reference(key::UInt64)
.Less common crash output