[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: threads question
- To: Oleg Goldshmidt <ogoldshmidt(at-nospam)computer.org>
- Subject: Re: threads question
- From: guy keren <choo(at-nospam)actcom.co.il>
- Date: Mon, 26 Nov 2001 23:53:58 +0200 (EET)
- cc: <linux-il(at-nospam)linux.org.il>
- Delivered-To: linux.org.il-linux-il@linux.org.il
- In-Reply-To: <m3elmkoog3.fsf@localhost.localdomain>
- Sender: linux-il-bounce(at-nospam)cs.huji.ac.il
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 linux-il-request@linux.org.il with
the word "unsubscribe" in the message body, e.g., run the command
echo unsubscribe | mail linux-il-request@linux.org.il