Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > class instances and threads

Reply
Thread Tools

class instances and threads

 
 
ouech
Guest
Posts: n/a
 
      01-29-2005
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
 
Reply With Quote
 
 
 
 
Gianni Mariani
Guest
Posts: n/a
 
      01-29-2005
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.

 
Reply With Quote
 
 
 
 
Ivan Vecerina
Guest
Posts: n/a
 
      01-29-2005
"ouech" <(E-Mail Removed)> wrote in message
newspsldexeaaozxxof@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


 
Reply With Quote
 
Scott McPhillips [MVP]
Guest
Posts: n/a
 
      01-29-2005
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]

 
Reply With Quote
 
Thomas Matthews
Guest
Posts: n/a
 
      01-30-2005
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

 
Reply With Quote
 
Shezan Baig
Guest
Posts: n/a
 
      01-31-2005

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-

 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      01-31-2005

"Shezan Baig" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
dicts,instances,containers, slotted instances, et cetera. ocschwar@gmail.com Python 8 01-29-2009 09:52 AM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
Multiple threads or app instances? Tumurbaatar S. ASP .Net 2 07-01-2005 01:38 PM
Threads over same socket/streams instances. udupi_mail@yahoo.com Java 2 02-21-2005 06:59 PM
list of class instances within a list of a class instances John Wohlbier Python 2 02-22-2004 08:41 AM



Advertisments