static method no locks

A

arun

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
 
M

mlimber

arun said:
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-lite/how-to-post.html#faq-5.9). You'll
want to post in comp.programming.threads or similar.

Cheers! --M
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,278
Latest member
BuzzDefenderpro

Latest Threads

Top