is PostEvent thread-safe?

BlitzMax Forums/BlitzMax Programming/is PostEvent thread-safe?

Rozek(Posted 2010) [#1]
Hello!

In a multi-threaded environment, is it safe to post events to the central EventQueue?

Thanks in advance for any hints!


ImaginaryHuman(Posted 2010) [#2]
I would think probably not and would need a mutex?


ziggy(Posted 2010) [#3]
I'm 90% sure it is not, but you have functions to determine if current thread is main, so a good approach (take this as a friendly suggestion) is to implement a queue with a thread safe 'addItem' method, and process the queue ONLY from the main thread (or from the same thread you're using to process the real single thread event queue). If you can avoid mutexes it can be even better.


marksibly(Posted 2010) [#4]
Hi,

No it's not currently - but it almost is.

The code uses 'queue_put' and 'queue_get' variables that could theoretically be modified to use atomic increment ops to make them threadsafe without the overhead of a mutex.

Such atomic ops are still slower than ordinary increments, but a lot faster than mutexes and it probably wouldn't hurt in this case.


Otus(Posted 2010) [#5]
The code uses 'queue_put' and 'queue_get' variables that could theoretically be modified to use atomic increment ops to make them threadsafe without the overhead of a mutex.

I recently tried something similar, but I couldn't figure out a way to avoid mutexes/semaphores completely, just to make them rare.

E.g. the update step needs to happen atomically and must not conflict with Poll/Peek.


beanage(Posted 2010) [#6]
So this is good to know, thanks Mark. Will the implementation be included in the update ahead?


Rozek(Posted 2010) [#7]
Hmmm,

I just ran across this thread again - has PostEvent been made threadsafe in the mean time?