Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
General
GeneralPortable MacsHardwareNetworking
Applications
Mac ApplicationsEudoraFirefox / MozillaInternet ExplorerOutlook ExpressMS OfficeEntourageExcelPowerPointWordVirtual PCMedia PlayerOther MS Products
Programming
Mac ProgrammingCodeWarriorPerl
Country Specific
Australian Mac GroupUK Mac Group

Mac Forum / Programming / Mac Programming / April 2006



Tip: Looking for answers? Try searching our database.

Multithreading (NSThread) and Local Variables

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Darel - 25 Apr 2006 16:23 GMT
I just got done reading Apple's documentation of Cocoa multithreading,
but I didn't find an answer to this basic question:

Are local variables re-created automatically for each thread?  In other
words, if I have an object method, or for that matter a plain, old, C
function, that gets called by one of my threads, are its local
variables created uniquely for that thread?  For example:

int q=0;

double calc(double a, double b) {

 double c;

 a*=a; b*=b; c=a+b;
 c=5./a/c/q;

 if (c<10.) q++;
 return c; }

In the above function, a, b, and c are all locals.  If multiple threads
are running this function simultaneously, does each thread have its
own, private variables for a, b, and c, not shared with the other
threads?

(I am assuming that the variable q would be shared by all threads since
it is defined outside the function.)

And while we're on the subject of multithreading -- is there a way to
ask the OS if any processor cores are free to execute a new thread?  I
didn't see anything about that in Apple's documentation either, and in
the project on which I am working it would be pretty pointless to
create a new thread if there is no unused processor core to execute it.

Any help is appreciated!

Thanks,
Darel

<DarelRex@gmail.com>
Bruno CAUSSE - 25 Apr 2006 16:51 GMT
dans l'article 1145978617.200359.55820@v46g2000cwv.googlegroups.com, Darel à
DarelRex@gmail.com a écrit le 25/04/06 17:23 :

> I just got done reading Apple's documentation of Cocoa multithreading,
> but I didn't find an answer to this basic question:
[quoted text clipped - 20 lines]
> own, private variables for a, b, and c, not shared with the other
> threads?

Of course

> (I am assuming that the variable q would be shared by all threads since
> it is defined outside the function.)
[quoted text clipped - 4 lines]
> the project on which I am working it would be pretty pointless to
> create a new thread if there is no unused processor core to execute it.

Good question.

> Any help is appreciated!
>
> Thanks,
> Darel
>
> <DarelRex@gmail.com>
Michael Ash - 25 Apr 2006 16:52 GMT
> I just got done reading Apple's documentation of Cocoa multithreading,
> but I didn't find an answer to this basic question:
[quoted text clipped - 3 lines]
> function, that gets called by one of my threads, are its local
> variables created uniquely for that thread?

This really has nothing to do with threads. Local variables are
"re-created" for every function invocation, threads or no. This is
essential for recursion. For example:

int useless(int x)
{
  int y = x - 1;
  if(y <= 0)
     return y;
  else
     return useless(y) + 1;
}

Now, when I call useless(10), I get eleven function invocations. Each one
has its own copy of y. The same thing happens for threads.

> (I am assuming that the variable q would be shared by all threads since
> it is defined outside the function.)

This is correct, globals are still globals even when threading.

> And while we're on the subject of multithreading -- is there a way to
> ask the OS if any processor cores are free to execute a new thread?  I
> didn't see anything about that in Apple's documentation either, and in
> the project on which I am working it would be pretty pointless to
> create a new thread if there is no unused processor core to execute it.

The question really doesn't make much sense; whether there are any free
CPUs is something that can change on a microsecond-to-microsecond basis.
You might query the OS and discover that there are three free CPUs, and
then a millisecond later the OS goes back to running software on all three
of them. Generally your best bet for this kind of situation is to query
the OS for the total number of CPUs (which can be done using sysctl or, I
think, Gestalt) and then run that many threads. This gives you the
smallest number of threads that can always use all available CPU power.

Signature

Michael Ash
Rogue Amoeba Software

vze35xda@verizon.net - 25 Apr 2006 17:16 GMT
Yes, local variables are created on the stack and are unique for each
invocation of the function.  (This is why recursion works....).

--jim
Patrick Machielse - 25 Apr 2006 17:31 GMT
> In the above function, a, b, and c are all locals.  If multiple threads
> are running this function simultaneously, does each thread have its
> own, private variables for a, b, and c, not shared with the other
> threads?

yes, local variables are private to that thread, _except_ when the local
variable is declared static.

patrick
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.