473,403 Members | 2,183 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,403 software developers and data experts.

Local static variables and multithreading

I'm trying to write platform-independent code that will work correctly in
multithreaded environment. I know C++ standard says nothing about threads,
but I still think my question is not entirely off-topic here:

int f()
{
static int n = g();
return n;
}

If I call the above function from multiple threads, can I still assume that
g() will only be called once? Or is it platform dependent?

thank you,
Marcin
Jan 7 '06 #1
2 7677
"Marcin Kalicinski" <ka****@poczta.onet.pl> wrote in message
news:Do******************@newsfe3-win.ntli.net
I'm trying to write platform-independent code that will work
correctly in multithreaded environment. I know C++ standard says
nothing about threads, but I still think my question is not entirely
off-topic here:
int f()
{
static int n = g();
return n;
}

If I call the above function from multiple threads, can I still
assume that g() will only be called once?


No.

http://blogs.msdn.com/oldnewthing/ar.../08/85901.aspx

Or is it platform dependent?

I suspect the answer is no on any multithreaded platform.
--
John Carson

Jan 7 '06 #2
In order to guarantee the above, the compiler would need to be aware of
threads. It would actually need to generate code to handle that
assignment inside a mutex or a critical section. C++ doesn't do that
(yet).

The code above can exhibit a number of nasty bugs: from duplicate
invocation of g() to use of partially initialized n.

-- Sasha

Jan 10 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Tim Hill | last post by:
I'm just getting up to speed on Java, and have a question about local class behavior. Here's a mini-app to illustrate: interface Foo { void print(); } class LocalClassTest { public static...
3
by: Patrick Hoffmann | last post by:
hi folx, today I had "multithreading" trouble with the following code (MS VC++6/optimized compiling): --------------------------- class myclass { static int s_nNext; public:
1
by: Richard Hayden | last post by:
Hi, I'm trying to port my O/S kernel from C to C++. I am using g++ with the following command-line switches: -Wall -fno-builtin -fno-rtti -fno-exceptions -fno-enforce-eh-specs -nostartfiles...
2
by: james | last post by:
Hi I am considering storing my session variables within one static object with session scope. The static object will be a class with accessor functions to get and set the equivalent session...
55
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
8
by: Rahul | last post by:
Hi, I have the following code and i get a compilation error, int main() { class Locale { public: static int c;
1
by: danep2 | last post by:
Let me start by saying that this is more a question about principle than practice - with the speed of today's computers it's probably rarely an actual issue. Still I'd like to know... If I have...
1
by: Neil.Fang.CN | last post by:
Supposing I have a class like this: class SomeManager { public: static SomeManager& getInstance(); } SomeManager& SomeManager::getInstance() {
3
by: Bob Doe | last post by:
Hello, how to I replace singleton classes using function scope static variables with one that doesn't use function scope static variables?: class Foo { public: static Foo &instance();...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.