is PostEvent thread-safe?
BlitzMax Forums/BlitzMax Programming/is PostEvent thread-safe?
| ||
Hello! In a multi-threaded environment, is it safe to post events to the central EventQueue? Thanks in advance for any hints! |
| ||
I would think probably not and would need a mutex? |
| ||
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. |
| ||
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. |
| ||
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. |
| ||
So this is good to know, thanks Mark. Will the implementation be included in the update ahead? |
| ||
Hmmm, I just ran across this thread again - has PostEvent been made threadsafe in the mean time? |