473,324 Members | 2,313 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

static method no locks

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
1 1632
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Simon Harvey | last post by:
Hi all, In my project I have made a number of helper methods static. As I understand it, this will create the problem that multiple threads could access the static method at the same time and...
0
by: Mike Meyer | last post by:
The recent thread on threads caused me to reread the formal definition of SCOOP, and I noticed something I hadn't really impressed me the first time around: it's using staticly checkable rules to...
10
by: Marek | last post by:
Hi, I am analyzing Duwamish7 source code boundled with Visual Studio .NET 2003. Could anoybody explain why the Monitor.Enter and Monitor.Exit block is used inside a static constructor? The code...
9
by: Clint | last post by:
Hey all - Excuse the cross-post ... I'm not sure what the appropriate newsgroup would be for this question. I have a question that I'm not quite sure how to ask. For all I know, I have the...
18
by: Frank Rizzo | last post by:
Hello, I have a class with all static methods that is called by multiple threads. I was wondering what effect that has on the competing threads. Does Thread2 have to wait until Thread1 is done...
5
by: blah, blah, blah | last post by:
I'm developing a .Net web application and created many helper classes often using static (shared in VB.Net) methods. Do I need to use the lock (SyncLock) statement in these methods to prevent...
6
by: depalau | last post by:
I'm running into some issues on maintaining a static variable across the lifetime of a web service and I was wondering if anyone could help. Background: We have developed a C#/1.1 web service...
25
by: =?iso-8859-1?B?QW5kcuk=?= | last post by:
Hello, I'm having a discussion with my colleagues (and boss) over the use of static methods in a class. My colleagues say that static methods should be avoided whenever is possible, because...
4
by: Dave | last post by:
I have a global.asax file with Application_Start defined and create some static data there and in another module used in the asp.net application and I realize that static data is shared amongst...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.