By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,217 Members | 1,458 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,217 IT Pros & Developers. It's quick & easy.

static method no locks

P: n/a
Hello group,

I have a static method but this function does not make use of locks.
Then what happens if
two threads access the local variables and parameters values at
different times. What value
will each thread see. For example

double d = 67.34;

static void mymethod(double d) {

// Line 1
int i = 5; // This is not static

if ( d == 78.5)
return; // Stage 2

// Initial code

First thread sets the values as
// Stage 1.
i = 89;
d = 78.5;

First thread relinquishes the control of cpu and second thread takes
over and enters the function
// Middle of code

// End of code
}

The second thread will enter the function at stage one (since there is
only one copy of code)
or at the beginning of the function.

Question:

1. I think the second thread will start executing the code from Line 1
and not from stage 1.
Is this right.

When the second thread encounters the code

if ( d == 78.5)
return; // Stage 2

What will it do. Will it return, since the value of d was changed by
thread 1 and now it
satisfies the if condition.

the book says that static function have global linkage in C++ as
against internal linkage in
C.

In short how are the variable inside a static function treated. Are
they treated like static
variables, even though their declarion may not say so.

Please provide feed back. I am having discussion with my lead and
there is a disagreement.

Thx.

Nagrik

Jun 8 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
arun wrote:
Hello group,

I have a static method but this function does not make use of locks.
Then what happens if
two threads access the local variables and parameters values at
different times. What value
will each thread see. For example

double d = 67.34;

static void mymethod(double d) {

// Line 1
int i = 5; // This is not static

if ( d == 78.5)
return; // Stage 2

// Initial code

First thread sets the values as
// Stage 1.
i = 89;
d = 78.5;

First thread relinquishes the control of cpu and second thread takes
over and enters the function
// Middle of code

// End of code
}

The second thread will enter the function at stage one (since there is
only one copy of code)
or at the beginning of the function.

Question:

1. I think the second thread will start executing the code from Line 1
and not from stage 1.
Is this right.

When the second thread encounters the code

if ( d == 78.5)
return; // Stage 2

What will it do. Will it return, since the value of d was changed by
thread 1 and now it
satisfies the if condition.

the book says that static function have global linkage in C++ as
against internal linkage in
C.

In short how are the variable inside a static function treated. Are
they treated like static
variables, even though their declarion may not say so.

Please provide feed back. I am having discussion with my lead and
there is a disagreement.

Thx.

Nagrik


Threads are off-topic here since Standard C++ knows nothing of them
(compare what is on-topic here:
http://www.parashift.com/c++-faq-lit....html#faq-5.9). You'll
want to post in comp.programming.threads or similar.

Cheers! --M

Jun 8 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.