473,378 Members | 1,539 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,378 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 3848
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; }
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.