[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: threads question



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