Thread safety and static member functions

O

Olumide

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
 
G

Guest

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.
 
O

Olumide

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?
 
I

Ian Collins

Erik said:
Convert the class to a namespace if it only has static member functions
and you do not plan to make instances of it.


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.
 
I

Ian Collins

Olumide said:
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.
 
O

Olumide

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>
 
I

Ian Collins

Olumide said:
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.
 

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

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top