Re: Working with C-functions in separate NSThreads: via Stack or Heap?
Re: Working with C-functions in separate NSThreads: via Stack or Heap?
- Subject: Re: Working with C-functions in separate NSThreads: via Stack or Heap?
- From: "Frederick C. Lee" <email@hidden>
- Date: Mon, 8 Nov 2010 17:54:11 -0800
My original reply was too long for Cocoa Dev; then I lost it.
So this is another (small draft)...
It appears I'll be working with blocks, keeping the data/functioning local
to the stack & copying data out to the GUI:
typedef sat_t (^MyBlock)();
// C - function:
int addFoot(int k) {
return (++k);
}
// Block declaration:
MyBlock MakeSat() {
__block sat_t sat;
__block tle_t tle;
__block int k = 0;
return Block_copy( ^ {
sat.satName = "George";
sat.tsince += 1.0;
k = addFoot(k);
sat.footprint = k;
predict_calc1(2); // ok.
predict_calc2(&sat, 2); //...passing struct{} ptr.
return sat;
});
} // end MyBlock().
// --------------------------------
int main(void) {
MyBlock myBlock = MakeSat();
printf("\nMyBlock value: %s, since= %g, addFoot= %g TLE name= %s\n",
myBlock().satName, myBlock().tsince, myBlock().footprint, myBlock().tle.name);
Block_release(myBlock);
return 0;
}
---------------
Output:
MyBlock value: Henry, since= 3, addFoot= 2 TLE name= Frederick C. Lee
... So I'll work with your (B) paradigm: keep the data 'regional' w/in a block.
That way, I can pass the data amongst functions w/in a block and copy
the result to the 'outside' calling ObjC object.
Ric.
On Nov 6, 2010, at 11:24 AM, lwj wrote:
>
> There it too much missing here to really understand whats going on. There is no context for 'sat_t workingSat'. This can mean very different things depending on where it is. When initSatWorkArea(&workingSat) is called you are passing a pointer to workingSat and not the structure itself.
>
> Based on what you have above I think there are 2 possibilities for where workingSat 'lives'.
>
> A.)
>
> sat_t workingSat;
>
> void someFunc(void) {
>
> initSatWorkArea(&workingSat);
>
> ...
> }
>
> In this case workingSat exists in the data section (neither heap nor stack) of your program. There is *only* 1 of them. You can share it between threads but every thread will see the same thing. Unless you want every thread working on the same thing this is *not* what you want.
>
>
> B.)
>
> void someFunc(void) {
>
> sat_t workingSat;
>
> initSatWorkArea(&workingSat);
>
> ...
> }
>
> In this case workingSat exists on the stack. A new one is created every time someFunc is called. *However*, when someFunc() returns workingSat *NO LONGER EXISTS*. From your description it sounds like initSatWorkArea might pass a pointer to workingSat to a thread but workingSat goes away once someFunc returns.
>
> There is a good chance that neither one of these is what you want.
>
> Wayne
>
>
>
>
>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden