Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: gcc / cygwin threads revisited

Author: Robert Hyatt

Date: 10:48:27 07/25/99

Go up one level in this thread


On July 24, 1999 at 20:27:09, James Swafford wrote:

>On July 24, 1999 at 17:19:31, James Swafford wrote:
>
>I've done some experimenting today, and discovered that
>gcc (or at least the cygwin port) will not let a multithreaded
>program terminate until each thread in the program has
>called exit( ), or returned from main.
>
>One frustrating question still remains.  Please examine
>the code segment below:
>
>**********
>   ui_thread_is_dead=0;
>
>   // start ui thread
>//   beginthread(UIThread,0,0);
>   if (!fork()) {
>      UIThread(NULL);
>      ui_thread_is_dead=1;
>      cout << "terminating child thread...\n" << flush;
>      return 0;
>   }
>
>   cout << "executing parent thread\n";
>   while (!ui_thread_is_dead) {
>   }
>
>   cout << "terminating parent thread...\n" << flush;
>   return 0;
>**********



that won't work.  The problem is that when you fork(), _everything_ in the
parent is duplicated in the child.  Open files... variable values..  etc..

after the fork, the child can change "ui_thread_is_dead" all it wants, but
the parent's copy will not be modified.  Ditto if the parent changes it after
the fork, the child will only see the value it copied.

You need to put this in shared memory or use the other ideas I gave in a
previous post...

Bob





>
>Once the program starts, the friendly message
>"executing parent thread" is displayed.
>
>Once I type "quit," which is handled by UIThread( ),
>the message "terminating child thread..." is displayed.
>At this point the variable ui_thread_is_dead *should* be=1,
>but for some reason it's not.
>
>The message "terminating parent thread" is never displayed,
>and the program never terminates.  Ugh.
>
>Any insight would be greatly appreciated. :-/
>
>--
>James
>
>
>
>
>
>>
>>I posted a question several days ago about the gcc
>>equivalent to MSVC's "beginthread( )" .
>>
>>Bob replied with "fork( )", which does the job, but
>>there's one rather ugly side effect:  the program
>>won't terminate.
>>
>>To be more specific, when I type "quit", the user
>>thread parses the input and processes it, but for
>>some reason when the call to exit( ) is performed,
>>the whole process freezes.  The same thing happens
>>when executing "return" from main( ).
>>
>>This doesn't happen when the program is running test
>>suites, simply because the UIThread( ) is never started.
>>When the suite is complete, the program exits cleanly.
>>
>>Perhaps threads are handled differently than with MSVC.
>>Is it okay to call exit( ) from within UIThread( )?
>>Would it be better to have UIThread( ) return some variable
>>indicating the program should terminate, then somehow kill
>>the thread, then call exit( ) from the "main thread"?  If so,
>>how do I kill a thread with gcc / cygwin?
>>
>>--
>>James



This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.