class instances and threads

Discussion in 'C++' started by ouech, Jan 29, 2005.

  1. ouech

    ouech Guest

    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
     
    ouech, Jan 29, 2005
    #1
    1. Advertising

  2. 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.
     
    Gianni Mariani, Jan 29, 2005
    #2
    1. Advertising

  3. "ouech" <> wrote in message
    news:eek:psldexeaaozxxof@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
     
    Ivan Vecerina, Jan 29, 2005
    #3
  4. 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]
     
    Scott McPhillips [MVP], Jan 29, 2005
    #4
  5. 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
     
    Thomas Matthews, Jan 30, 2005
    #5
  6. ouech

    Shezan Baig Guest

    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-
     
    Shezan Baig, Jan 31, 2005
    #6
  7. ouech

    Mike Wahler Guest

    "Shezan Baig" <> wrote in message
    news:...
    >
    > 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
     
    Mike Wahler, Jan 31, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tumurbaatar S.

    Multiple threads or app instances?

    Tumurbaatar S., Jul 1, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    675
    Alvin Bruney [MVP - ASP.NET]
    Jul 1, 2005
  2. Replies:
    2
    Views:
    584
    iksrazal
    Feb 21, 2005
  3. John Wohlbier
    Replies:
    2
    Views:
    369
    Josiah Carlson
    Feb 22, 2004
  4. Brad Tilley

    instances v. threads

    Brad Tilley, Nov 19, 2004, in forum: Python
    Replies:
    4
    Views:
    328
    Brad Tilley
    Nov 21, 2004
  5. Replies:
    8
    Views:
    470
    James Stroud
    Jan 29, 2009
Loading...

Share This Page