Computer Chess Club Archives




Subject: Re: gcc / cygwin threads revisited

Author: Robert Hyatt

Date: 10:45:36 07/25/99

Go up one level in this thread

On July 24, 1999 at 17:19:31, James Swafford wrote:

>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?

Note that after fork() you have _two_ processes running.  If one reads the
quit command and does an exit(0), the other continues running.  You have two
or three choices...

1.  tell the other process to quit also (I assume you communicate with it thru
a pipe or shared memory).

2.  when you do the fork, make it pid=fork(). Then when the parent exits, it
can do a kill(pid,9); to terminate the other process instantly.

3.  If you communicate to the child thru a pipe, make sure that the child
knows how to handle a broken pipe (EOF condition) and that it exits when it
sees it.

4.  catch the SIGPIPE signal that happens when one end of the pipe is closed,
as when one thread exits (again, if you are using pipes).

5.  create a global/shared variable initialized to 0.  Whenever a thread notices
it is 1, they exit.  Then set this when needed...

Just because one process exits, this has nothing to do with the other one and
it can live for years...

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.