Boost logo

Boost Users :

From: Ben Hutchings (ben.hutchings_at_[hidden])
Date: 2004-02-12 10:07:42


Damien McGivern <d_mcgivern_at_[hidden]> wrote:
> (First post)
> Hi,
> I've just started working with boost and have come
> into some trouble with the thread class. I've included
> the test code that I'm having the problem with in the
> hope that someone could point out to me where I'm
> going wrong.
>
> The code tests the QueueAccessor class
> (CommandQueue.hpp) with the test code in
> CommandQueueTest.hpp. I created a base class
> (Runnable) to aid in creating a thread that runs an
> object's method rather than a function. The code
> compiles OK and seems to run OK too at first until I
> realised that the main thread has somehow become one
> of the threads that I created and is in a continuous
> loop after the first join() is called
>
> _writer_thread1->join(); // line 21

join() waits for the thread to *end*. This should be done
after the threads have been told to Stop().

> As you can probably guess from my code my background
> is in Java so I'm not entirely sure if I'm using the
> boost::thread correctly.

I thought Java's join() was exactly the same.

> I tested the QueueAccessor
> using threads that use a static worker function
> instead of an object's method and it seemed to run OK
> so I'm assuming that the problem lies with the
> Runnable base class.
>
> I'm using boost version 1.31.0 running Windows XP and
> using VS.Net 2003 to compile/run the code.
>
> Any help will be very much appreciated

Here are the bugs I found:

- Runnable::_running is read and written from multiple
  threads without use of a mutex.
- The result of QueueAccessor::Pop is undefined if the queue
  is empty. Perhaps it should throw in this case?
- The return type of QueueAccessor::Size should be
  std::queue<QueueItem>::size_type (which I think is
  std::size_t).
- QueueAccessor::Size doesn't lock the mutex for the queue.
- Reader::Do busy-waits for input. It should wait on a
  condition variable instead. (Condition variables provide
  functions equivalent to the wait, notify and notifyAll
  methods that Object has in Java.) Worse, when the
  reader is asked to stop, Reader::Do may never return to
  Worker::Run and so the thread will never stop.
- CommandQueueTest.hpp doesn't include <iostream>.
- #include <name> is reserved for use with system headers.
  You should use #include "name" for including your own
  headers.
- Identifiers that include "__" are reserved for the use
  of the implementation. You must not use them.
- Many member variables are protected when they should
  be private.

I have fixed most of the above and the program now works.

Ben.







Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net