class instances and threads

O

ouech

hi,

I'd like to know if i need mutexs to lock the use of
member methods of a class instance shared between
several threads via a pointer? if the method modify
member variables, i know that mutexs are needed but
the case that interrest me is when the method never
modify a member variable. I really have no idea cause
i don't know if the process duplicate the method in
memory for each threads or if the local variables of
this method are shared by all the threads.

thanks a lot for your help, i just begin C++ and i
don't figure out all the subtleties of classes yet
 
G

Gianni Mariani

ouech said:
hi,

I'd like to know if i need mutexs to lock the use of
member methods of a class instance shared between
several threads via a pointer? if the method modify
member variables, i know that mutexs are needed but
the case that interrest me is when the method never
modify a member variable. I really have no idea cause
i don't know if the process duplicate the method in
memory for each threads or if the local variables of
this method are shared by all the threads.

thanks a lot for your help, i just begin C++ and i
don't figure out all the subtleties of classes yet

C++ classes does not change threading issues related to concurrent access.

In other words, if you need to lock your critical regions without
classes, you'll need to do the same with classes and C++ specifically
(in the current revision of the standard) does not specify thread semantics.

Having said that, many C++ classes STL types are thread safe to some
level (not for concurrent access though).

If *all* your threads are simply not modifying class state, then most
implementations of C++ will not require any locking of critical regions.
 
I

Ivan Vecerina

ouech said:
I'd like to know if i need mutexs to lock the use of
member methods of a class instance shared between
several threads via a pointer? if the method modify
member variables, i know that mutexs are needed but
the case that interrest me is when the method never
modify a member variable. I really have no idea cause
i don't know if the process duplicate the method in
memory for each threads or if the local variables of
this method are shared by all the threads.

This really has nothing to do with C++ (the current C++
standard specifies nothing about multi-threaded platforms).

But on common platforms, as long as all accesses to an
object/memory location are read-only, no synchronization
is required. As soon as one of the threads may modify
the object, a synchronization is required for all threads
(including the read-only ones).

On most C++ platforms, the previous can also be applied
to standard library containers: read-only access (i.e.
usage of 'const' member functions only) demands no
explicit synchronization.


I hope this helps,
Ivan
 
S

Scott McPhillips [MVP]

ouech said:
hi,

I'd like to know if i need mutexs to lock the use of
member methods of a class instance shared between
several threads via a pointer? if the method modify
member variables, i know that mutexs are needed but
the case that interrest me is when the method never
modify a member variable. I really have no idea cause
i don't know if the process duplicate the method in
memory for each threads or if the local variables of
this method are shared by all the threads.

thanks a lot for your help, i just begin C++ and i
don't figure out all the subtleties of classes yet

A mutex is only needed if the multiple threads will be making changes to
shared data. Local variables of a method are not shared data, they are
unique to each thread.
 
T

Thomas Matthews

Scott said:
A mutex is only needed if the multiple threads will be making changes to
shared data. Local variables of a method are not shared data, they are
unique to each thread.

Is this stated in the standard?

Is this true for all platforms?


--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 
S

Shezan Baig

Thomas said:
Is this stated in the standard?

Is this true for all platforms?

Each thread has its own execution stack. Local non-static variables in
one thread are independent of the "same" local non-static variables in
another thread. This is true for all multi-threaded platforms I know
of.

Hope this helps,
-shez-
 
M

Mike Wahler

Shezan Baig said:
Each thread has its own execution stack. Local non-static variables in
one thread are independent of the "same" local non-static variables in
another thread. This is true for all multi-threaded platforms I know
of.

You missed Thomas' point. Standard C++ (the topic
of this newsgroup) has no direct support for threads at all.
Such support must be provided by extensions or third party tools.

-Mike
 

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,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top