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

Thread & Worker should not be ThreadSafe objects

Open dktapps opened this issue 1 year ago • 0 comments

It doesn't really make any sense for these objects to be shared. They contain API methods which are only intended for the parent thread to use, such as start(), join(), isRunning(), isJoined(), etc.

The only reason a shared context object is desirable for these things is to allow Thread::run() to get information from the parent thread. After the initial run(), it makes more sense to put things in static properties, rather than paying a performance penalty to access them from the thread context again and again.

Proposal:

  • Make Thread and Worker no longer extend ThreadSafe
  • Make Thread and Worker become final
  • Make Thread accept a Runnable in its __construct() (which will be thread-safe), which it runs and then immediately shuts itself down (contrary to a Worker, which remains running - though we could easily ditch Thread and only have Worker with this system)
  • Possibly introduce ClosureRunnable
  • Move Thread::getCreatorId() to Runnable::getCreatorId()
  • Remove Thread::getCurrentThread() and Thread->getThreadId() (Thread::getCurrentThreadId() will remain)
  • Add Thread::getName() and Thread::setName() for identification (relates to #139)

dktapps avatar Apr 02 '24 17:04 dktapps