Re: Passing arguments to threads?
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