On 26 Nov 2001, Oleg Goldshmidt wrote:

> > What is this last return statement?? Returning from main? That's not
> > something you're supposed to do in a multithreaded program. It
> > might, for example, free the entire malloc pool, including argwhich
> > is later freed in the second thread, causing a segmentation fault.
>
> You are right that there should not be a return statement, but it is
> not what is happening: free() is OK, according to the
> debugger.

the fact that in this specific code you didn't have a problem, does not
prove it is correct. the debugger can only be used to show when you _do_
have a race condition - it cannot be used to prove there is _no_ race
condition.

 > Besides, return from main(), as well as any exit(), should
> terminate the process (including any threads), freeing the memory by
> independent means, not execute the rest of the code, buggy as it may
> be.

not true at all. the main thread is just a thread. when it calls exit(),
the other threads may still be executing, and theoretically, there could
be a context switch in the middle of the exit() function, and this might
cause a race.

when you handle multi-threading, you are not allowed to say "it won't
happen" - because one day it will, and the problem won't be persistant and
easy to debug. it'll come and go intermitently.


as for the bug itself - you are not allowed to pass a NULL pointer as the
first parameter of pthread_create() - you must pass a pointer to a
pthread_t instance. so you should add a variable definition before
calling pthread create:

pthread_t thr;

and then:

if (pthread_create(&thr, NULL, start, arg) != 0)
..

this solves the crash. why this happens only during thread termination? i
would imagine that when a thread terminates, it atempts to updated its
state info (e.g. storing its return value somewhere) - and that state info
is quite likely to be stored inside the pthread_t variable passed to
pthread_create.

--
guy

"For world domination - press 1,
 or dial 0, and please hold, for the creator." -- nob o. dy


=================================================================
To unsubscribe, send mail to [EMAIL PROTECTED] with
the word "unsubscribe" in the message body, e.g., run the command
echo unsubscribe | mail [EMAIL PROTECTED]

Reply via email to