Return-Path: william@bourbon.usc.edu Delivery-Date: Tue Oct 28 07:33:11 2008 X-Spam-Checker-Version: SpamAssassin 3.2.3 (2007-08-08) on merlot.usc.edu X-Spam-Level: X-Spam-Status: No, score=-2.3 required=5.0 tests=AWL,BAYES_00 autolearn=ham version=3.2.3 Received: from bourbon.usc.edu (bourbon.usc.edu [128.125.9.75]) by merlot.usc.edu (8.14.1/8.14.1) with ESMTP id m9SEXBYP004532 for ; Tue, 28 Oct 2008 07:33:11 -0700 Received: from bourbon.usc.edu (localhost.localdomain [127.0.0.1]) by bourbon.usc.edu (8.14.2/8.14.1) with ESMTP id m9SEiIam028767 for ; Tue, 28 Oct 2008 07:44:18 -0700 Message-Id: <200810281444.m9SEiIam028767@bourbon.usc.edu> To: cs551@merlot.usc.edu Subject: Re: CS551 final1 - pass argument to threads Date: Tue, 28 Oct 2008 07:44:18 -0700 From: Bill Cheng Someone wrote: > When beacon create threads connecting to other beacons, > I use a for-loop which calls pthread_create and pass the iterator 'i' > into the thread as argument. > I found that created thread cannot get that argument correctly unless > wait for certain time, like 10ms. > For example, when i=3 and pass into thread#3. thread#3 may get 4 due > to for-loop increment that variable. > This looks like a bad way to use thread, but how to achieve my purpose? Are you passing i by value or passing by reference? You can actually pass i by value if that's what you want. The last argument to pthread_create() is of type (void*). So, if you just typecast i to (void*), you will copy the value of i to the thread function. If you don't like typecasting to (void*), you can first typecast to (char*): (void*)(char*)i -- Bill Cheng // bill.cheng@usc.edu