ext-pmmpthread icon indicating copy to clipboard operation
ext-pmmpthread copied to clipboard

Threaded internal pthread_cond_t always initialized (expensive), though potentially not used

Open dktapps opened this issue 4 years ago • 2 comments

pthread_cond_t facilitates wait()/notify() functionality, which aren't needed in the majority of cases. However, the monitor always allocates them, and this accounts for the large majority of the time spent when creating new Threaded objects.

Ways this could be avoided:

  • (ng only) Split wait/notify stuff into its own class, leaving ThreadedBase even more basic
  • Lazy-init pthread_cond_t (I haven't explored whether this is safe or not).

dktapps avatar Oct 31 '21 15:10 dktapps

Lazy initialization isn't an option since it's possible for multiple threads to try to use wait() simultaneously without using synchronized(). This would lead to a data race and memory leak.

dktapps avatar Nov 01 '21 22:11 dktapps

If we decide to go with a separate class for this, ThreadedEvent would make sense, with a similar API to Python's threading.Event.

This would also simplify the majority use-case of wait() and notify() by avoiding the need for boilerplate synchronized blocks, amongst other things - the vast majority of uses of wait/notify involve some kind of boolean condition flag.

dktapps avatar Sep 02 '22 01:09 dktapps