[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: threads question
- To: linux-il(at-nospam)linux.org.il
- Subject: Re: threads question
- From: "Nadav Har'El" <nyh(at-nospam)math.technion.ac.il>
- Date: Mon, 26 Nov 2001 19:12:58 +0200
- Delivered-To: linux.org.il-linux-il@linux.org.il
- Hebrew-Date: 12 Kislev 5762
- In-Reply-To: <m3snb1nl00.fsf@localhost.localdomain>; from ogoldshmidt@computer.org on Mon, Nov 26, 2001 at 06:57:35PM -0500
- References: <m3snb1nl00.fsf@localhost.localdomain>
- Sender: linux-il-bounce(at-nospam)cs.huji.ac.il
- User-Agent: Mutt/1.2i
On Mon, Nov 26, 2001, Oleg Goldshmidt wrote about "threads question":
> static void* start(void* arg)
> {
> int c = *((int*)arg);
> free(arg);
> .....
> }
>
> int main(void)
> {
> int* arg = NULL;
>
> if ((arg = (int*)malloc(sizeof(*arg))) == NULL)
>...
> if (pthread_create(NULL, NULL, start, arg) != 0)
> {
> free(arg);
> fprintf(stderr,"Error: %s\n",strerror(errno));
> return EXIT_FAILURE;
> }
>
> return EXIT_SUCCESS;
> }
>
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 arg which is later freed in the
second thread, causing a segmentation fault.
To reiterate - any thread might simply 'return', but the main thread may
not.
Try replacing "return EXIT_SUCCESS" by "pause()" (for example) and see
if it now works.
--
Nadav Har'El | Monday, Nov 26 2001, 12 Kislev 5762
nyh@math.technion.ac.il |-----------------------------------------
Phone: +972-53-245868, ICQ 13349191 |I before E except after C. We live in a
http://nadav.harel.org.il |weird society!
=================================================================
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