Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The first problem is that your main function is spawning a thread, then immediately exiting. The thread will barely get a chance to run before the process terminates. So at a minimum you need to wait for the thread to finish, which you can do by adding <code>newThread.join()</code> to the end of <code>main</code>, thus:</p> <pre><code>int main(int argc, char* const argv[]) { MyClass a; A.push_back(a); boost::thread newThread(&amp;MyClass::DoSomething, &amp;A.back()); newThread.join(); return 0; } </code></pre> <p>The other issue is you've got the thread object in the same scope as <code>main</code>, as a local variable. While you can do this in simple situations, you need to be careful about the scope of the <code>boost::thread</code> object, which needs to give it a lifetime at least as long as you expect your thread to run.</p> <p>In the case where an object needs to run a thread on a member function, you are probably better off encapsulating the thread object inside the object, and having a simple <code>run()</code> method that starts the worker thread. That way you can encapsulate all the state and thread management in the one place, along with any mutexes and condition variables that you may need. A simple example would be something like:</p> <pre><code>class Worker { public: Worker(); void start(int N) { m_Thread = boost::thread(&amp;Worker::processQueue, this, N); } void join() { m_Thread.join(); } // Worker thread where all processing occurs void processQueue(int N); private: boost::thread m_Thread; }; </code></pre> <p>You would probably also want to add things like a flag that returns the thread state (eg. running, completed, waiting, etc).</p> <p>I noticed your example uses a vector of object instances (ie. storing by value). Just be careful that you don't inadvertently end up performing operations that cause a copy to be implicitly created, as this can cause all sorts of problems with threads. (That's why the <code>boost::thread</code> object is non-copyable.)</p> <p>I've written an article on <a href="http://antonym.org/2009/05/threading-with-boost---part-i-creating-threads.html" rel="nofollow noreferrer" title="article on threading with Boost">threading with Boost</a> that explains the various ways you can create threads, including how to run threads on member functions (see type 5).</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload