Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. newbie to multi threading

newbie to multi threading

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++javacom
12 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M minkowski

    Hi ya, I am a noob to multi threading on C++ (did a bit on Java) and was hoping you could explain to me the following (from http://www.ddj.com/cpp/184401518?pgno=3). #include <boost/thread/thread.hpp> #include <boost/thread/mutex.hpp> #include <iostream> boost::mutex io_mutex; struct count { count(int id) : id(id) { } void operator()() { for (int i = 0; i < 10; ++i) { boost::mutex::scoped_lock lock(io_mutex); std::cout << id << ": " << i << std::endl; } } int id; }; int main(int argc, char* argv[]) { boost::thread thrd1(count(1)); boost::thread thrd2(count(2)); thrd1.join(); thrd2.join(); return 0; } what is this for? boost::mutex io_mutex; To say you wish to define a boost::mutex lock and call it io_mutex? In the for loop a scoped lock is used. Does that mean any object used within the scope of the for loop is locked? Does the lock only lock "this"? How do I lock other objects or do I have to use a scoped lock in the member function? Thanks for any information.

    S Offline
    S Offline
    Stuart Dootson
    wrote on last edited by
    #2

    minkowski wrote:

    what is this for? boost::mutex io_mutex; To say you wish to define a boost::mutex lock and call it io_mutex?

    Yes

    minkowski wrote:

    In the for loop a scoped lock is used. Does that mean any object used within the scope of the for loop is locked?

    No - it means that the thread containing the for loop will own the mutex within the for loop

    minkowski wrote:

    Does the lock only lock "this"? How do I lock other objects or do I have to use a scoped lock in the member function?

    The lock isn't explicitly attached to the thing it locks - it's more of a conceptual relationship. What it means in this case is that only one thread at once can execute the line std::cout << id << ": " << i << std::endl;.

    Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

    M 1 Reply Last reply
    0
    • S Stuart Dootson

      minkowski wrote:

      what is this for? boost::mutex io_mutex; To say you wish to define a boost::mutex lock and call it io_mutex?

      Yes

      minkowski wrote:

      In the for loop a scoped lock is used. Does that mean any object used within the scope of the for loop is locked?

      No - it means that the thread containing the for loop will own the mutex within the for loop

      minkowski wrote:

      Does the lock only lock "this"? How do I lock other objects or do I have to use a scoped lock in the member function?

      The lock isn't explicitly attached to the thing it locks - it's more of a conceptual relationship. What it means in this case is that only one thread at once can execute the line std::cout << id << ": " << i << std::endl;.

      Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

      M Offline
      M Offline
      minkowski
      wrote on last edited by
      #3

      Hey thanks for that. So basically, anything that is in the for loop is only executable by 1 thread? If so, what if I have some member functions for a class, say a get() and a set() and I only want one thread at a time to access either of them (preventing a read occuring the same time as a write). How would I lock them?

      S 1 Reply Last reply
      0
      • M minkowski

        Hey thanks for that. So basically, anything that is in the for loop is only executable by 1 thread? If so, what if I have some member functions for a class, say a get() and a set() and I only want one thread at a time to access either of them (preventing a read occuring the same time as a write). How would I lock them?

        S Offline
        S Offline
        Stuart Dootson
        wrote on last edited by
        #4

        minkowski wrote:

        So basically, anything that is in the for loop is only executable by 1 thread

        Yup.

        minkowski wrote:

        If so, what if I have some member functions for a class, say a get() and a set() and I only want one thread at a time to access either of them

        Same sort of thing - using the same threading classes as before:

        class MyClass
        {
        void SetMyObject(SomeClass const& value)
        {
        boost::mutex::scoped_lock lock(objectMutex);
        myObject = value;
        }
        SomeClass SetMyObject() const
        {
        boost::mutex::scoped_lock lock(objectMutex);
        return myObject;
        }
        private:
        SomeClass myObject;
        boost::mutex objectMutex;
        };

        Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

        M 1 Reply Last reply
        0
        • S Stuart Dootson

          minkowski wrote:

          So basically, anything that is in the for loop is only executable by 1 thread

          Yup.

          minkowski wrote:

          If so, what if I have some member functions for a class, say a get() and a set() and I only want one thread at a time to access either of them

          Same sort of thing - using the same threading classes as before:

          class MyClass
          {
          void SetMyObject(SomeClass const& value)
          {
          boost::mutex::scoped_lock lock(objectMutex);
          myObject = value;
          }
          SomeClass SetMyObject() const
          {
          boost::mutex::scoped_lock lock(objectMutex);
          return myObject;
          }
          private:
          SomeClass myObject;
          boost::mutex objectMutex;
          };

          Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

          M Offline
          M Offline
          minkowski
          wrote on last edited by
          #5

          So from your example, say one thread accesses the SetMyObject() , given that there is a lock in the function, it will also lock the GetMyObject() blocking any other thread from calling? This kind of implies that wherever in the object there is a lock, any function owned by the object will be locked to any other thread trying to access. Is this correct? Thanks again for your help.

          S 1 Reply Last reply
          0
          • M minkowski

            So from your example, say one thread accesses the SetMyObject() , given that there is a lock in the function, it will also lock the GetMyObject() blocking any other thread from calling? This kind of implies that wherever in the object there is a lock, any function owned by the object will be locked to any other thread trying to access. Is this correct? Thanks again for your help.

            S Offline
            S Offline
            Stuart Dootson
            wrote on last edited by
            #6

            minkowski wrote:

            So from your example, say one thread accesses the SetMyObject() , given that there is a lock in the function, it will also lock the GetMyObject() blocking any other thread from calling?

            Yes

            minkowski wrote:

            This kind of implies that wherever in the object there is a lock, any function owned by the object will be locked to any other thread trying to access. Is this correct?

            Sort of - any function that attempts to acquire (lock) the mutex will be serialized, as only one thread can acquire the mutex at a time. So, that means if you have a group of functions that acquire the lock, only one of that group can be executed concurrently. There are other sorts of locks that may better suit different situations (semaphores, read-write locks) - part of learning how to design multi-threaded systems is knowing which sort of synchronisation mechanism to use.

            Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

            M 1 Reply Last reply
            0
            • S Stuart Dootson

              minkowski wrote:

              So from your example, say one thread accesses the SetMyObject() , given that there is a lock in the function, it will also lock the GetMyObject() blocking any other thread from calling?

              Yes

              minkowski wrote:

              This kind of implies that wherever in the object there is a lock, any function owned by the object will be locked to any other thread trying to access. Is this correct?

              Sort of - any function that attempts to acquire (lock) the mutex will be serialized, as only one thread can acquire the mutex at a time. So, that means if you have a group of functions that acquire the lock, only one of that group can be executed concurrently. There are other sorts of locks that may better suit different situations (semaphores, read-write locks) - part of learning how to design multi-threaded systems is knowing which sort of synchronisation mechanism to use.

              Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

              M Offline
              M Offline
              minkowski
              wrote on last edited by
              #7

              Hey thanks for that, it tidied up some of my uncertainties. I was wondering how C++ knows to lock the other member function if one is being accessed by another thread. In the thread constructor itself you pass in either a function pointer or overloaded operator() in your class. So for the case of an object, there must be some reference to the this pointer for the object for it to know to lock the other member functions if a lock is in place from another thread. Was wondering if this is correct? Thanks for any information.

              S 1 Reply Last reply
              0
              • M minkowski

                Hey thanks for that, it tidied up some of my uncertainties. I was wondering how C++ knows to lock the other member function if one is being accessed by another thread. In the thread constructor itself you pass in either a function pointer or overloaded operator() in your class. So for the case of an object, there must be some reference to the this pointer for the object for it to know to lock the other member functions if a lock is in place from another thread. Was wondering if this is correct? Thanks for any information.

                S Offline
                S Offline
                Stuart Dootson
                wrote on last edited by
                #8

                minkowski wrote:

                I was wondering how C++ knows to lock the other member function

                It doesn't. The locking is contained within the mutex implementation. When you acquire a mutex, you either get retun immediately with ownership of the mutex, or your thread waits inside the OS until you do get ownership.

                minkowski wrote:

                Was wondering if this is correct?

                As explained above, no :-) Can I suggest you read up on how mutexes[^] work[^]? That should maybe make things a bit clearer...

                Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                M 1 Reply Last reply
                0
                • S Stuart Dootson

                  minkowski wrote:

                  I was wondering how C++ knows to lock the other member function

                  It doesn't. The locking is contained within the mutex implementation. When you acquire a mutex, you either get retun immediately with ownership of the mutex, or your thread waits inside the OS until you do get ownership.

                  minkowski wrote:

                  Was wondering if this is correct?

                  As explained above, no :-) Can I suggest you read up on how mutexes[^] work[^]? That should maybe make things a bit clearer...

                  Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                  M Offline
                  M Offline
                  minkowski
                  wrote on last edited by
                  #9

                  Oh I see, so since its the same lock object ( objectMutex from your example in the get() and set() functions) of course it will be locked?

                  S 1 Reply Last reply
                  0
                  • M minkowski

                    Oh I see, so since its the same lock object ( objectMutex from your example in the get() and set() functions) of course it will be locked?

                    S Offline
                    S Offline
                    Stuart Dootson
                    wrote on last edited by
                    #10

                    That's it. So, as each instance of MyClass has a separate instance of objectMutex, you can access different instances of MyClass concurrently on separate threads, but not the same instance (because the mutex will stop you).

                    Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                    M 1 Reply Last reply
                    0
                    • S Stuart Dootson

                      That's it. So, as each instance of MyClass has a separate instance of objectMutex, you can access different instances of MyClass concurrently on separate threads, but not the same instance (because the mutex will stop you).

                      Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                      M Offline
                      M Offline
                      minkowski
                      wrote on last edited by
                      #11

                      Ah excellent. Makes sense! Thanks for your time (and patience!) :)

                      S 1 Reply Last reply
                      0
                      • M minkowski

                        Ah excellent. Makes sense! Thanks for your time (and patience!) :)

                        S Offline
                        S Offline
                        Stuart Dootson
                        wrote on last edited by
                        #12

                        My pleasure

                        Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups