c# - Does ThreadPool.RegisterWaitForSingleObject block the current thread or a thread-pool thread? -


from reading the documentation of threadpool.registerwaitforsingleobject method, not clear whether:

  1. it blocks current thread while waiting on eventwaithandle , commissions waitortimercallback on thread pool thread, or

  2. it commissions thread pool thread wait on wait handle , on same thread execute waitortimercallback once wait handle has been signaled.

  3. it blocks current thread , when wait handle signaled, calls waitortimercallback on current thread. equivalent functionality of waithandle.waitone(). also, not involve thread pool @ all.

which of 3 do?

it none of above, 2) closest. exact details pretty convoluted, of code buried in clr , has changed across .net versions. can have look-see @ current version in coreclr source, i'll give 10,000 feet view.

key doesn't block, work done dedicated unmanaged thread. called "wait thread" in source code, uses waitformultipleobjects() winapi function wait on registered waits. if there none (left) sleeps. thread woken either queueuserapc() if wait list changes can resume waiting updated list.

once 1 of wait objects signaled, uses threadpool.queueuserworkitem() invoke callback delegate target on threadpool thread. if executeonlyonce argument true wait handle removed list. , resumes waiting wfmo. thread never ends.

the executeonlyonce argument important btw, hilarity ensues if pass false , use manualresetevent. thread explosion triggered mre's set() method interesting artifact observe :) can see wait thread in debugger's debug > windows > threads when enable unmanaged debugging. doesn't have interesting name however.


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -