# Calling Julia functions from a multi-threading scheduler written in C?

**URL:** <https://discourse.julialang.org/t/calling-julia-functions-from-a-multi-threading-scheduler-written-in-c/54114>\
**Category:** General Usage\
**Tags:** question, multithreading, c\
**Created:** [January 28, 2021, 12:11pm UTC](https://discourse.julialang.org/t/calling-julia-functions-from-a-multi-threading-scheduler-written-in-c/54114 "2021-01-28T12:11:15Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![stustd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stustd/32/21430_2.png) [@stustd](https://discourse.julialang.org/u/stustd)\
**Post date:** [January 28, 2021, 12:11pm UTC](https://discourse.julialang.org/t/calling-julia-functions-from-a-multi-threading-scheduler-written-in-c/54114/1 "2021-01-28T12:11:15Z")

</div>

I have a scheduler written in C that repetitively executes batches if C functions in multi-threaded fashion:

- Mutually exclusive groups of C functions update one reactor state (i.e. the total set of C functions is partitioned around a smaller set of reactor states);
- The C functions are scheduled in and out;
- Each C function can call into a Julia callback passing it the reactor state.

As a setup my thinking is as follows:

1. Define a Julia struct holding the reactor state.
2. The Julia reactor state should have one field holding a reference to a Julia interpreter engine;
3. In C initialize the Julia reactor state at reactor creation; this includes starting the Julia interpreter engine;
4. Call reaction callbacks from reactor’s Julia engine.

Is this feasible?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 28, 2021, 1:16pm UTC](https://discourse.julialang.org/t/calling-julia-functions-from-a-multi-threading-scheduler-written-in-c/54114/2 "2021-01-28T13:16:18Z")

</div>

> [@stustd](#):
>
> The Julia reactor state should have one field holding a reference to a Julia interpreter engine;

As far as I know, embedding multiple instances of Julia (which uses a compiler, not just an interpreter) within a single process is not supported, and perhaps will never be supported. See [this thread from 2014](http://julia-programming-language.2336112.n4.nabble.com/Distinguish-between-modules-when-embedding-Julia-in-C-td507.html).

The best approach is probably to let Julia “drive” — Julia already has a scheduler for multi-threaded code, so you could spawn Julia threads that call your C functions and pass them callback functions.
