Boost logo

Boost Users :

From: Pete (petehug_at_[hidden])
Date: 2004-03-16 15:28:08


I'm having a problem with my boost multithreaded application. My app runs
fine as a Windows Service, but as a Unix daemon it stops responding after
some time. I noticed that the fewer threads I have available, the quicker
the application stops responding: HP-UX(64) after 20mins, RH8(256) after
>1hr, Linux RH9(1024) >5hrs. This led me to assume that perhaps the threads
are not cleaned up properly.

Therefore, I wrote a little test app which ought to demonstrate the problem.
To my surprise, the test application showed an entirely different problem,
but again, only under Unix (it works as expected under Windows). I'm totally
lost what the reason is and appreciate any help.

Expected: Actual:
--------------- ---------------
Start running Start running
Start swimming Finish running
Start cycling Start swimming
Finish swimming Finish swimming
Finish cycling Start cycling
Finish running Finish cycling

In windows I get the actual output but under Linux RH9, I get the Actual
output. Under RH9, the first five lines of the actual output appear
immediately, but the last one takes approx. 4secs. In other words, only the
last threads don't sleep the number of secs specified.

#include <string>
#include <list>
#include <iostream>
#include <unistd.h>
#include <boost/thread/thread.hpp>

class MyThread
{
  protected:
    std::string m_strName;
    int m_iDuration;

  public:
    MyThread(const std::string & s, int i) : m_strName(s), m_iDuration(i) {}

    void operator()()
    {
      std::cout << "Start " << m_strName << std::endl;
      sleep(m_iDuration);
      std::cout << "Finish " << m_strName << std::endl;
    }
};

int main(int argc, char *argv[])
{
  std::list<MyThread*> lst;
  std::list<MyThread*>::iterator itr;
  MyThread* pT;
  boost::thread_group tg;

   lst.push_back(new MyThread("running", 6));
   lst.push_back(new MyThread("swimming", 2));
   lst.push_back(new MyThread("cycling", 4));

   for (itr = lst.begin(); itr != lst.end(); itr++)
   {
     pT = *itr;
     tg.create_thread(boost::ref(*pT));
   }

   tg.join_all();

   while (!lst.empty())
   {
     delete lst.front();
     lst.pop_front();
   }

  return 1;
}


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