469,950 Members | 2,362 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,950 developers. It's quick & easy.

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 3613
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Rahul Joshi | last post: by
5 posts views Thread by Naren | last post: by
3 posts views Thread by exits funnel | last post: by
3 posts views Thread by qWake | last post: by
1 post views Thread by RainerFaulstich | last post: by
3 posts views Thread by paul.furber | last post: by
13 posts views Thread by Henri.Chinasque | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.