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. Why can't 2 or more boost::thread objects represent the same thread? [modified]

Why can't 2 or more boost::thread objects represent the same thread? [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
graphicsquestion
8 Posts 4 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.
  • F Offline
    F Offline
    followait
    wrote on last edited by
    #1

    After creating some thread, I want to keep track of them in a vector (vectorboost::thread ), but it fail. So I need to use vectorboost::thread\*, but in this way, I have to free them manually.

    modified on Monday, August 17, 2009 9:16 AM

    C S N 3 Replies Last reply
    0
    • F followait

      After creating some thread, I want to keep track of them in a vector (vectorboost::thread ), but it fail. So I need to use vectorboost::thread\*, but in this way, I have to free them manually.

      modified on Monday, August 17, 2009 9:16 AM

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      followait wrote:

      (vector ), but it fail.

      What do you mean by "it fails" ? I never used boost::thread before but if you are getting a compilation error, it probably means that the class do not allow to make copies of an instance (e.g. they made the assignement operator and copy constructor private).

      Cédric Moonen Software developer
      Charting control [v2.0] OpenGL game tutorial in C++

      S 1 Reply Last reply
      0
      • C Cedric Moonen

        followait wrote:

        (vector ), but it fail.

        What do you mean by "it fails" ? I never used boost::thread before but if you are getting a compilation error, it probably means that the class do not allow to make copies of an instance (e.g. they made the assignement operator and copy constructor private).

        Cédric Moonen Software developer
        Charting control [v2.0] OpenGL game tutorial in C++

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

        Cedric Moonen wrote:

        the class do not allow to make copies of an instance (e.g. they made the assignement operator and copy constructor private).

        Exactly right - just like IOstreams

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

        1 Reply Last reply
        0
        • F followait

          After creating some thread, I want to keep track of them in a vector (vectorboost::thread ), but it fail. So I need to use vectorboost::thread\*, but in this way, I have to free them manually.

          modified on Monday, August 17, 2009 9:16 AM

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

          Use a std::vector<boost::shared_ptr<boost::thread> >. That way you get a copyable object that manages its resources itself. it makes a lot of sense for a thread object to be non-copyable[^] - just like it makes sense for an iostream to be non-copyable. When you have a link to a concept visible outside your program, you only want one object to manage it otherwise you could get contradictory scenarios happening, like one object killing the thread while another is trying to join the thread or something.

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

          F 1 Reply Last reply
          0
          • F followait

            After creating some thread, I want to keep track of them in a vector (vectorboost::thread ), but it fail. So I need to use vectorboost::thread\*, but in this way, I have to free them manually.

            modified on Monday, August 17, 2009 9:16 AM

            N Offline
            N Offline
            Nemanja Trifunovic
            wrote on last edited by
            #5

            Or you can use boost::ptr_vectorboost::thread and it will delete the pointers for you when it goes out of scope. Of course, Stuart's solution with shared pointers will work as well and may be even safer.

            Programming Blog utf8-cpp

            1 Reply Last reply
            0
            • S Stuart Dootson

              Use a std::vector<boost::shared_ptr<boost::thread> >. That way you get a copyable object that manages its resources itself. it makes a lot of sense for a thread object to be non-copyable[^] - just like it makes sense for an iostream to be non-copyable. When you have a link to a concept visible outside your program, you only want one object to manage it otherwise you could get contradictory scenarios happening, like one object killing the thread while another is trying to join the thread or something.

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

              F Offline
              F Offline
              followait
              wrote on last edited by
              #6

              But when using pointer, this still will happen. Maybe things should be synchonized, but it'll be inefficient.

              S 1 Reply Last reply
              0
              • F followait

                But when using pointer, this still will happen. Maybe things should be synchonized, but it'll be inefficient.

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

                followait wrote:

                But when using pointer, this still will happen.

                No it won't - you'll be copying the pointer, not the object it refers to.

                followait wrote:

                Maybe things should be synchonized, but it'll be inefficient.

                Really? I think not.

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

                F 1 Reply Last reply
                0
                • S Stuart Dootson

                  followait wrote:

                  But when using pointer, this still will happen.

                  No it won't - you'll be copying the pointer, not the object it refers to.

                  followait wrote:

                  Maybe things should be synchonized, but it'll be inefficient.

                  Really? I think not.

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

                  F Offline
                  F Offline
                  followait
                  wrote on last edited by
                  #8

                  Stuart Dootson wrote: No it won't - you'll be copying the pointer, not the object it refers to. More than one pointers can do things than conflicts simultaneously in different threads. Stuart Dootson wrote: Really? I think not. Agree, it's good to keep it simple for the fundamental concept. It can be encapsulated as needed.

                  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