Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2001-09-07 06:24:21


http://groups.yahoo.com/group/boost/files/thread_ref/

contains a thread::ref prototype design and win32-based implementation.
Works with bcc32 5.5.1:

bcc32 -tWM thread_test.cpp thread_win32.cpp

and MSVC 7:

cl -GX -MT thread_test.cpp thread_win32.cpp

VC6 users should replace std::for_each/mem_fn with for loops.

Acknowledgements: This work could not have been possible without all the
effort spent by Bill Kempf on Boost.Threads. Although the current prototype
doesn't use Boost.Threads, earlier iterations did.

Here's the relevant portion of thread_test.cpp:

mutex m;

void printing_thread(int n)
{
  for(;;)
  {
    thread::test_cancel();
    mutex::scoped_lock lock(m);
    std::cout << n;
  }
}

void canceling_thread(std::vector<thread::ref> v)
{
  std::for_each(v.begin(), v.end(), boost::mem_fn(&thread::ref::cancel));
}

int main()
{
  std::vector<thread::ref> v;

  for(int i = 0; i < 8; ++i)
  {
    v.push_back(thread::create(boost::bind(printing_thread, i)));
  }

  thread::create(boost::bind(canceling_thread, v)).join();

  std::for_each(v.begin(), v.end(), boost::mem_fn(&thread::ref::join));

  std::cout << '\n';

  return 0;
}

--
Peter Dimov
Multi Media Ltd.

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk