How are theads, Tid and spawn related?
    Ali Çehreli via Digitalmars-d-learn 
    digitalmars-d-learn at puremagic.com
       
    Sun Nov  2 14:34:35 PST 2014
    
    
  
On 11/02/2014 09:04 AM, Sean Kelly wrote:
 > On Sunday, 2 November 2014 at 06:23:38 UTC, Ali Çehreli wrote:
 >> Can I safely remove thread_joinAll()s if they are the last lines in
 >> main()?
 >
 > It has always been the case.  In fact, I have a comment in the body of
 > Thread.start() explaining this potential race and explaining the need
 > for certain operations in that function.
I've grepped for thread_joinAll calls in my examples and identified two 
cases where I needed it at the end of main, both of which about the need 
to "pull" thread_joinAll inside main's scope. Otherwise, the automatic 
thread_joinAll call would happen after main's scope ends and it would be 
too late in the following cases.
1) A scope(exit) should not unregister() before all threads finish.
2) A thread still uses an object tied to main's scope.
void main()
{
     auto first = spawn(&player, "second");
     register("first", first);
     scope(exit) unregister("first");  // <-- (1)
     // ...
     immutable(int) i = 42;
     spawn(&worker, &i);               // <-- (2)
     // ...
     thread_joinAll();                 // <-- justified
}
The thread_joinAll() at the end of main() above is justified to take 
care of both of those cases.
Ali
    
    
More information about the Digitalmars-d-learn
mailing list