static member function and thread

S

stub

Several objects of the same class, which contains a static member function,
are created in several threads.

If the said member function is called in the threads concurrently, it must
be made to be thread-safe. For a non-static member function, this should
not be a concern. Is this a correct understanding?

Similarly, for a static member variable, if more than one thread read/write
it, should the accesses of threads be synchronized?
 
M

Moonlit

Hi,




stub said:
Several objects of the same class, which contains a static member function,
are created in several threads.

If the said member function is called in the threads concurrently, it must
be made to be thread-safe. For a non-static member function, this should
not be a concern. Is this a correct understanding?

Non static/static for methods, only has to do with the fact that for non
static members the 'this' pointer is passed as an extra parameter to the
routine. For the rest there is no difference.
Similarly, for a static member variable, if more than one thread read/write
it, should the accesses of threads be synchronized?

The following for most platforms, but since I don't know every OS/platform
there might be an awkward system around that doesn't comply with the
following.

Usually you should take care that not more than one thread accesses the same
variable for writing/writing or writing/reading (reading/reading should
work. Code itself can be accessed by more than one thread without problems.

You can achieve this by using mutexes, critical sections etc.

Regards, Ron AF Greve
 

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
474,145
Messages
2,570,826
Members
47,373
Latest member
Desiree036

Latest Threads

Top