• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Passing arguments to threads?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Passing arguments to threads?


  • Subject: Re: Passing arguments to threads?
  • From: "John C. Randolph" <email@hidden>
  • Date: Mon, 20 Dec 2004 15:34:49 -0800

On Dec 20, 2004, at 3:00 PM, Mai Bui wrote:


Hi,

How can I pass C-structrure (as pass reference) to thread?
For example:

typedef struct MyCStruct{
.... member ....
}MyCStruct, *MyCStructPtr;


-(void) MymainRoutine
{
MyCStructPtr localCStructPtr;
....
[NSThread detachNewThreadSelector: @selector(MyRoutine) toTarget:self withObject:localCStructPtr];
....
}


- (void)MyRoutine: (MyCStructPtr) mcstruct
{
 ..... change member of the MyCStruct here....
}

Is it correct?
Well, if you want to pass a pointer in -detachNewThreadSelector:toTarget:withObject:, you should wrap it in an NSValue using +valueWithPointer: The more important question here though, is where is the storage for your MyCStruct allocated? If it lives in the stack frame of MymainRoutine, then it's going to vanish when MymainRoutine exits, leaving the -MyRoutine: code in the other thread trying to use memory that's no longer valid.

-jcr

John C. Randolph <email@hidden> (408) 974-8819
Sr. Cocoa Software Engineer,
Apple Worldwide Developer Relations
http://developer.apple.com/cocoa/index.html


_______________________________________________ Do not post admin requests to the list. They will be ignored. Cocoa-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden
References: 
 >Re: Passing arguments to threads? (From: Mai Bui <email@hidden>)

  • Prev by Date: Re: Current tick mark index of NSSlider?
  • Next by Date: Re: NSBeginAlertSheet (II)
  • Previous by thread: Re: Passing arguments to threads?
  • Next by thread: NSBeginAlertSheet (II)
  • Index(es):
    • Date
    • Thread