By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,092 Members | 1,650 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,092 IT Pros & Developers. It's quick & easy.

class instances and threads

P: n/a
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

--
lucas
Montes
EPITECH
Jul 23 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
ouech wrote:
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.

Jul 23 '05 #2

P: n/a
"ouech" <lo******@nospamhotmail.com> wrote in message
news:opsldexeaaozxxof@loucs...
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
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Jul 23 '05 #3

P: n/a
ouech wrote:
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

--
lucas
Montes
EPITECH


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.

--
Scott McPhillips [VC++ MVP]

Jul 23 '05 #4

P: n/a
Scott McPhillips [MVP] wrote:
ouech wrote:
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

--
lucas
Montes
EPITECH

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

Jul 23 '05 #5

P: n/a

Thomas Matthews wrote:
Scott McPhillips [MVP] wrote:
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?


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-

Jul 23 '05 #6

P: n/a

"Shezan Baig" <sh************@gmail.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...

Thomas Matthews wrote:
Scott McPhillips [MVP] wrote:
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?


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
Jul 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.