Made the thread abstraction into a publicly available class.

This commit is contained in:
chris 2004-04-15 05:54:33 +00:00
parent 9999eb0f9c
commit 044fa013fb
8 changed files with 91 additions and 10 deletions

View File

@ -69,6 +69,7 @@ nobase_include_HEADERS = \
asio/socket_error.hpp \
asio/socket_option.hpp \
asio/stream_socket.hpp \
asio/thread.hpp \
asio/timer.hpp \
asio/timer_base.hpp \
asio/wrapped_handler.hpp

View File

@ -42,6 +42,7 @@
#include "asio/socket_error.hpp"
#include "asio/socket_option.hpp"
#include "asio/stream_socket.hpp"
#include "asio/thread.hpp"
#include "asio/timer.hpp"
#include "asio/timer_base.hpp"

View File

@ -0,0 +1,80 @@
//
// thread.hpp
// ~~~~~~~~~~
//
// Copyright (c) 2003, 2004 Christopher M. Kohlhoff (chris@kohlhoff.com)
//
// Permission to use, copy, modify, distribute and sell this software and its
// documentation for any purpose is hereby granted without fee, provided that
// the above copyright notice appears in all copies and that both the copyright
// notice and this permission notice appear in supporting documentation. This
// software is provided "as is" without express or implied warranty, and with
// no claim as to its suitability for any purpose.
//
#ifndef ASIO_THREAD_HPP
#define ASIO_THREAD_HPP
#include "asio/detail/push_options.hpp"
#include "asio/detail/thread.hpp"
namespace asio {
/// The thread class provides a very simple abstraction for starting threads.
/**
* The asio::thread class implements the smallest possible subset of the
* functionality of boost::thread. It is intended to be used only for starting
* a thread and waiting for it to exit. If more extensive threading
* capabilities are required, you are strongly advised to use something else.
*
* A typical use of asio::thread would be to launch a thread to run a demuxer's
* event processing loop:
*
* @code asio::demuxer d;
* // ...
* asio::thread t(boost::bind(&asio::demuxer::run, &d));
* // ...
* t.join(); @endcode
*/
class thread
: private boost::noncopyable
{
public:
/// Start a new thread that executes the supplied function.
/**
* This constructor creates a new thread that will execute the given function
* or function object.
*
* @param f The function or function object to be run in the thread. The
* equivalent function signature must be: @code void f(); @endcode
*/
template <typename Function>
thread(Function f)
: impl_(f)
{
}
/// Destructor.
~thread()
{
}
/// Wait for the thread to exit.
/**
* This function will block until the thread has exited.
*/
void join()
{
impl_.join();
}
private:
detail::thread impl_;
};
} // namespace asio
#include "asio/detail/pop_options.hpp"
#endif // ASIO_THREAD_HPP

View File

@ -53,6 +53,7 @@ documentation</a> for more information on how to use <tt>boost::bind</tt>.
\li asio::ipv4::address
\li asio::socket_error
\li asio::thread
\li asio::timer_base
</TD>

View File

@ -252,11 +252,10 @@ int main(int argc, char* argv[])
client c(d, host, port, block_size, session_count, timeout);
std::list<detail::thread*> threads;
std::list<thread*> threads;
while (--thread_count > 0)
{
detail::thread* new_thread =
new detail::thread(boost::bind(&demuxer::run, &d));
thread* new_thread = new thread(boost::bind(&demuxer::run, &d));
threads.push_back(new_thread);
}

View File

@ -180,11 +180,10 @@ int main(int argc, char* argv[])
server s(d, port, block_size);
// Threads not currently supported in this test.
std::list<detail::thread*> threads;
std::list<thread*> threads;
while (--thread_count > 0)
{
detail::thread* new_thread =
new detail::thread(boost::bind(&demuxer::run, &d));
thread* new_thread = new thread(boost::bind(&demuxer::run, &d));
threads.push_back(new_thread);
}

View File

@ -163,8 +163,8 @@ void demuxer_test()
count = 0;
d.reset();
d.post(boost::bind(start_sleep_increments, &d, &count));
detail::thread thread1(boost::bind(&demuxer::run, &d));
detail::thread thread2(boost::bind(&demuxer::run, &d));
thread thread1(boost::bind(&demuxer::run, &d));
thread thread2(boost::bind(&demuxer::run, &d));
thread1.join();
thread2.join();

View File

@ -97,8 +97,8 @@ void locking_dispatcher_test()
count = 0;
d.reset();
d.post(boost::bind(start_sleep_increments, &d, &l, &count));
detail::thread thread1(boost::bind(&demuxer::run, &d));
detail::thread thread2(boost::bind(&demuxer::run, &d));
thread thread1(boost::bind(&demuxer::run, &d));
thread thread2(boost::bind(&demuxer::run, &d));
// Check all events run one after another even though there are two threads.
timer timer1(d, timer::from_now, 3);