I've tried creating a logging widget that is thread safe, but it seems it sometimes hangs up (probably because several messages arrive simultaneously, I guess). The info, warning and error functions get called by a custom C++ iostream and they can get called simultaneously by two or three threads (like the video, audio or decode thread of my viewer).
> FLTK thread uses Fl::xxx_timeout() timer to poll the above array for content, locks to read, moving data from array to widget, deletes the data read, then unlocks the array
I don't let the child thread touch anything else that the
FLTK thread might also use.
This includes whatever file descriptors/pipes/etc that the
child thread reads for its data source. All that is owned by the
child, and those descriptors are unknown to the FLTK thread.
Modern C++ has al the thread and synchronisation calls you need in a cross platform manner.
Anyway, I would use `Fl::awake(handler, user data)` Fl::awake to send strings from any thread to the main thread. The call itself has its own lock for thread safety, and the handler is called by the main thread in the order in which `awake` was called. You just have to make sure that the handler deallocates the strings.
Thank you, Matthias. Fl::awake was just what I was looking for. I use two global strings (one for the text and another for the style) with a mutex and I append them when I call Fl::awake on my local function.
That works better than Erco's solution of using a timeout (which
I also tried with success).
-- Gonzalo Garramuño ggar...@gmail.com
FWIW, I have a threaded example of a terminal emulator which uses
a combo of Fl_Text_Editor and separate read/write pipes to
interact with the shell, each pipe has its own child thread in
addition to the main FLTK thread:
http://seriss.com/people/erco/fltk/unix-bidir-dumb-terminal.cxx
It's unix specific code (mac+linux) because of the use of pthreads
and unix style pipes. There are direct windows equivalents that'd
probably be relatively easy to swap in, just WIN32 APIs for
properly mixing pipes with subprocesses (for the DOS shell) is a
bit of annoying API work.
So, far be it from me to disagree with Matt (that’s code for “I am about to disagree with Matt”, BTW) but I’m not hugely keen on dynamically creating/deleting objects to buffer the “transient” buffer objects that are passed asynch from the worker thread to main.
--
You received this message because you are subscribed to the Google Groups "fltk.general" group.
To unsubscribe from this group and stop receiving emails from it, send an email to fltkgeneral...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/fltkgeneral/F1C78245-B245-4CF6-9E04-6D0C8D9222C0%40gmail.com.
Modern ones I think use (small) per-thread heaps. The problem then is that allocating and freeing in different threads loses the multithreading advantage of this and may result in locking anyway.