From owner-freebsd-hackers Sat Nov 27 15: 5: 0 1999 Delivered-To: freebsd-hackers@freebsd.org Received: from bluerose.windmoon.nu (c255152-a.plstn1.sfba.home.com [24.7.89.179]) by hub.freebsd.org (Postfix) with ESMTP id 4B8F514C03 for ; Sat, 27 Nov 1999 15:04:58 -0800 (PST) (envelope-from fengyue@bluerose.windmoon.nu) Received: from localhost (fengyue@localhost) by bluerose.windmoon.nu (Windmoon-Patched/8.9.3/8.9.3) with ESMTP id PAA42297 for ; Sat, 27 Nov 1999 15:11:36 -0800 (PST) Date: Sat, 27 Nov 1999 15:11:36 -0800 (PST) From: FengYue To: hackers@FreeBSD.ORG Subject: Re: PThreads and Sockets In-Reply-To: <19991126173228.A2608@spirit.jaded.net> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG On Fri, 26 Nov 1999, Dan Moschuk wrote: > > | int sd2; > | if((sd2=accept(sd, (struct sockaddr*)&cad, &alen)) > 0) { > | pthread_create(&thread1, pthread_attr_default, > | serverstart, &sd2); > | } > | > | Then the serverstart function: > | > | void *serverstart(void *ptr) > | { > | int *sd2; > | sd2 = (int*)ptr; > | > | dowhatever(sd2); > | } > | > | Any ideas as to what I'm doing wrong? Also, thanks for your help. > | > | Rob > > Try this. > > void *serverstart(void *ptr) > { > int sd2; > > sd2 = *((int *) ptr); > ... > } There is a race condition. You're passing sd2's address to serverstart() and inside serverstart() you def' the pointer. What if "sd2=accept(sd, (struct sockaddr*)&cad, &alen)" gets executed before your previous serverstart() finishs "sd2 = *((int*)ptr)"? btw, IMHO, creating threads per connection is a very bad design. To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message