Hi,
I'm new to Nim, sorry if that's a stupid question.
I need to create a thread in which I will invoke some GUI library(to display an image). When I envelop this code(to open window) inside a procedure I find out that it's gc-unsafe, which is the case for majority of libs out there:
and so on.
Are they considered thread unsafe? Is there a way to use these libraries inside a thread without changing their source code?
I'm asking because I would implement this task in any other language without such hurdle...
Thank You!
Usually the GUI is the main thread and the heavy duty processing is the background thread.
I'm asking because I would implement this task in any other language without such hurdle...
You're going against software usual practices and I'm curious to know in which other languages there is no such hurdle?
To interact with global memory in a multithreaded context, annotate your threaded access with {.gcsafe.} and do some special handling such as locks (mutexes) or queues (message passing) to avoid data corruption.
For NiGui, check this example out and the doc for queueMain:
proc queueMain*(app: App, fn: proc()) ## \
## Queues `fn` to be executed on the GUI thread and returns immediately.
##
## This is the only function that can be safely called from other threads, and it must be called from a `{.gcsafe.}:` block.
## Before a thread that has called this function returns, it should wait for all queued funcitons to be executed:
##
## .. code-block:: nim
## while app.queued() > 0:
## discard