472,805 Members | 915 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Thread safety and static member functions

Hi -

I've got a class that contains static member functions alone, all of
whose arguments are passed by reference as shown below:

class MySpiffyClass{
// no constructor, destructor or variables, just static members
static void FirstFunction( args & );
static void SecondFunction( args & );
static void ThirdFunction( args & );
...
};

Will this class require synchronization -- In the event that
simultaneous calls to MySpiffyClass::FirstFunction( a_set_of_args )
for example are made? The case is clear if the class contains static
members.

Thanks,

- Olumide

Oct 22 '07 #1
6 3797
On 2007-10-22 23:50, Olumide wrote:
Hi -

I've got a class that contains static member functions alone, all of
whose arguments are passed by reference as shown below:

class MySpiffyClass{
// no constructor, destructor or variables, just static members
static void FirstFunction( args & );
static void SecondFunction( args & );
static void ThirdFunction( args & );
...
};
Convert the class to a namespace if it only has static member functions
and you do not plan to make instances of it.
Will this class require synchronization -- In the event that
simultaneous calls to MySpiffyClass::FirstFunction( a_set_of_args )
for example are made? The case is clear if the class contains static
members.
As long as none of the functions have any static variables or call other
functions that are not thread-safe then your code should be thread-safe
as well.

--
Erik Wikström
Oct 22 '07 #2
On Oct 22, 11:58 pm, Erik Wikström <Erik-wikst...@telia.comwrote:
Convert the class to a namespace if it only has static member functions
and you do not plan to make instances of it.
Thanks. I don't know a great deal about namespaces -- I'll read up on
them). Why do you suggest that I do this?

Oct 22 '07 #3
Erik Wikström wrote:
On 2007-10-22 23:50, Olumide wrote:
>Hi -

I've got a class that contains static member functions alone, all of
whose arguments are passed by reference as shown below:

class MySpiffyClass{
// no constructor, destructor or variables, just static members
static void FirstFunction( args & );
static void SecondFunction( args & );
static void ThirdFunction( args & );
...
};

Convert the class to a namespace if it only has static member functions
and you do not plan to make instances of it.
>Will this class require synchronization -- In the event that
simultaneous calls to MySpiffyClass::FirstFunction( a_set_of_args )
for example are made? The case is clear if the class contains static
members.

As long as none of the functions have any static variables or call other
functions that are not thread-safe then your code should be thread-safe
as well.
That's a bit light for a description of thread safe. Use use of static
member functions implies the use of static data members, accessing these
from multiple threads can also cause problems.

To the OP, with regards to thread safety, consider static members in the
same was a globals (functions and data). Apply the same thread safety
rules.

--
Ian Collins.
Oct 22 '07 #4
Olumide wrote:
On Oct 22, 11:58 pm, Erik Wikström <Erik-wikst...@telia.comwrote:
>Convert the class to a namespace if it only has static member functions
and you do not plan to make instances of it.

Thanks. I don't know a great deal about namespaces -- I'll read up on
them). Why do you suggest that I do this?
Style and flexibility probably. Before we had namespaces, static class
members were the only way to scope a group om data and functions. The
biggest problem is you have to define them all in one place.

--
Ian Collins.
Oct 22 '07 #5
On Oct 23, 12:19 am, Ian Collins <ian-n...@hotmail.comwrote:
That's a bit light for a description of thread safe. Use use of static
member functions implies the use of static data members, accessing these
from multiple threads can also cause problems.

To the OP, with regards to thread safety, consider static members in the
same was a globals (functions and data). Apply the same thread safety
rules.
The class contains NO static member variables.

<babble>
What I was initially unsure of was if the existence of just one copy
of the each static function implied sharing, which naturally brought
the question of thread-safety to mind.

But I recall that, all classes static or otherwise have only one copy
of their member functions (if non static, such a member function has
an additional/implicit "this" pointer to the object that called the
function) irrespective of the number of instances of the class.

My conclusion (please correct me if I'm wrong): each call to a static
member function creates new copies the function's local variables on
the stack, so that context switches between threads cannot corrupt
local variables. Recall that the class itself contains member
variables static or otherwise.
</babble>
Oct 22 '07 #6
Olumide wrote:
>
My conclusion (please correct me if I'm wrong): each call to a static
member function creates new copies the function's local variables on
the stack, so that context switches between threads cannot corrupt
local variables. Recall that the class itself contains member
variables static or otherwise.
As I said, treat them like free functions.

--
Ian Collins.
Oct 23 '07 #7

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

Similar topics

2
by: Rahul Joshi | last post by:
Hi, Is it possible to define static member functions that are 'const', i.e. they just read but do not modify the static data members of a class? Declaring functions like: class SomeClass {...
5
by: Naren | last post by:
Hello Grp, Correct me if I am wrong. static member functions can act only on static member varaibles.It can accessed by using the name of the class. Then why is there an access controller. ...
3
by: exits funnel | last post by:
Hello, One of the problems at the end of Chapter 14 in Bruce Eckel's thinking in C++ reads as follows: Create a class with two static member functions. Inherit from this class and redefine...
11
by: Roger Leigh | last post by:
The C++ book I have to hand (Liberty and Horvath, Teach yourself C++ for Linux in 21 Days--I know there are better) states that "static member functions cannot access any non-static member...
3
by: qWake | last post by:
The C++ language standard stipulates at section 9.4.1 that " A static member function shall not be declared const " The question is: what problem(s) could possibly exist in allowing static...
1
by: RainerFaulstich | last post by:
Hi, Some dummy question : Are static member functions of a class indeed a single instance for all instances of the class or have difference instances of this class have its own instances of this...
3
by: paul.furber | last post by:
Hi all, I have some code which looks a bit like this: #define Offset(m, T) ((size_t)(&((T *)1)->m) - 1) class Point: private: int *x,*y;
13
by: Henri.Chinasque | last post by:
Hi all, I am wondering about thread safety and member variables. If I have such a class: class foo { private float m_floater = 0.0; public void bar(){ m_floater = true; }
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.