Is a G'MIC instance threadsafe?

Here is a rather specific question, most likely for @David_Tschumperle himself:
Can a single instance of a G’MIC instance be called from multiple threads concurrently?
I am not talking about the command line binary or lib, since these always create their own instance on each call, but rather the C++ class. Just asking before I add any mutex locks around the calls in my code.

Currently I am working on/testing an improved version of libcgmic/libcgmicstatic that can handle several G’MIC instances in parallel (each of them protected by a mutex), so the library can be called concurrently, e.g. from several render threads in host applications. This should improve performance in projects in Natron or After Effects that use multiple G’MIC filters at the same time.

A G’MIC instance should be actually thread-safe by itself.
For instance, command parallel already runs multiple G’MIC instances in parallel.

The thing to take care about is more the fact that sharing the same list of images or image names among the different threads is dangerous : if one instance modifies the state of the list (remove or add images, or change the buffer address of one image), then it could mess up the other running instances.
Having a different input list for each instance is then safer.

Ok, thanks for the info. Of course input and output buffers sharing the same memory is a risk, but this is already handled by the library and the host in my case, they are guaranteed to be separate buffers.
I will do some more tests and see if I run into any problems using one instance from multiple threads. If it seems to work fine, I’ll leave it as it is without a mutex check.

Hmm, ran into an issue here. When I access a single G’MIC interpreter instance concurrently from two or more threads, I get this error:
*** Error in ./ *** An instance of G’MIC interpreter 00007FFEE4BF9108 is already running.

Any idea?

Yes, actually, what I thought at first was you were asking if different instances of gmic classes could be run in parallel, and for this, the answer is “yes”.
But trying to run method run() in parallel for a single gmic instance is not possible indeed.

What I do in G’MIC is actually create as much gmic instances as I need, and run them in parallel.

Ah, ok, sorry for being a bit too unclear.
Ok, then I will include my dynamic instance allocator in the new libcgmicstatic code. It manages a pool of instances and know which ones are busy and automatically assigns incoming requests to the next free instance.

1 Like