Embedding Julia while maintaining task scheduler

Hi, I’ve been trying to be able to embed Julia in a C++ application and access it from multiple (C++) threads (Why? Some context here).

After start learning how Julia “managed” threads work, I start trying to add methods to register an external thread as a Julia “unmanaged” one. However, at some point, I realized that the easiest way would be to have the main thread in Julia (a dedicated C++ thread where Julia has been initialized) accepting tasks and scheduling to other “managed” threads as needed. The tasks could be queued and waited for from any C++ thread since direct calls into the Julia runtime would be transferred to and run in the main thread.

Now the problem is that I have not being able to have the main Julia thread waiting for C++ tasks, using a C++ condition variable for instance, and at the same time having the Julia running its scheduler in that very same thread. I can either be waiting on the C++ condition or on the Julia scheduler, but not both.

I’ve been doing several experiments, and looked into using Libuv condition variables and streams, but without success.

I think I need a way to have Julia in its internal loop but while having a hook that can be triggered from the C++ side.
Does anyone have any hint or advice on I can solve this problem? Thanks.

1 Like

I don’t know enough about Julia internals but I know a bit about threading. the Julia tasks are not threads so the main Julia loop can’t schedule tasks and block on a thread sync primitive. I’d suggest to create a C++ thread that waits on a thread sync primitive and then all a Julia task has to do is fire it… which shouldn’t ever block.

Thanks. I was not clear, but I am actually aiming at Julia new multithreaded task scheduler (task.sticky = false).

What I want, in the end, is to be able to have several C++ parallel threads running and calling into Julia for running several Julia “tasks” in parallel.

I already solved the C++ code waiting for its Julia task to finish, by using condition variables that are notified from Julia with ccalls.

Well, I finally solved this. The solution is the use of a Julia’s AsyncCondition (instead of a C++ std::condition_variable), which seems to have been created to solve problems similar to mine: https://docs.julialang.org/en/v1.1/manual/calling-c-and-fortran-code/#Thread-safety-1.

1 Like