wince_thread.hpp
Go to the documentation of this file.
00001 //
00002 // wince_thread.hpp
00003 // ~~~~~~~~~~~~~~~~
00004 //
00005 // Copyright (c) 2003-2008 Christopher M. Kohlhoff (chris at kohlhoff dot com)
00006 //
00007 // Distributed under the Boost Software License, Version 1.0. (See accompanying
00008 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
00009 //
00010 
00011 #ifndef ASIO_DETAIL_WINCE_THREAD_HPP
00012 #define ASIO_DETAIL_WINCE_THREAD_HPP
00013 
00014 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
00015 # pragma once
00016 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
00017 
00018 #include "asio/detail/push_options.hpp"
00019 
00020 #include "asio/detail/push_options.hpp"
00021 #include <boost/config.hpp>
00022 #include "asio/detail/pop_options.hpp"
00023 
00024 #if defined(BOOST_WINDOWS) && defined(UNDER_CE)
00025 
00026 #include "asio/error.hpp"
00027 #include "asio/system_error.hpp"
00028 #include "asio/detail/noncopyable.hpp"
00029 #include "asio/detail/socket_types.hpp"
00030 
00031 #include "asio/detail/push_options.hpp"
00032 #include <boost/throw_exception.hpp>
00033 #include <memory>
00034 #include "asio/detail/pop_options.hpp"
00035 
00036 namespace asio {
00037 namespace detail {
00038 
00039 DWORD WINAPI wince_thread_function(LPVOID arg);
00040 
00041 class wince_thread
00042   : private noncopyable
00043 {
00044 public:
00045   // The purpose of the thread.
00046   enum purpose { internal, external };
00047 
00048   // Constructor.
00049   template <typename Function>
00050   wince_thread(Function f, purpose = internal)
00051   {
00052     std::auto_ptr<func_base> arg(new func<Function>(f));
00053     DWORD thread_id = 0;
00054     thread_ = ::CreateThread(0, 0, wince_thread_function,
00055         arg.get(), 0, &thread_id);
00056     if (!thread_)
00057     {
00058       DWORD last_error = ::GetLastError();
00059       asio::system_error e(
00060           asio::error_code(last_error,
00061             asio::error::get_system_category()),
00062           "thread");
00063       boost::throw_exception(e);
00064     }
00065     arg.release();
00066   }
00067 
00068   // Destructor.
00069   ~wince_thread()
00070   {
00071     ::CloseHandle(thread_);
00072   }
00073 
00074   // Wait for the thread to exit.
00075   void join()
00076   {
00077     ::WaitForSingleObject(thread_, INFINITE);
00078   }
00079 
00080 private:
00081   friend DWORD WINAPI wince_thread_function(LPVOID arg);
00082 
00083   class func_base
00084   {
00085   public:
00086     virtual ~func_base() {}
00087     virtual void run() = 0;
00088   };
00089 
00090   template <typename Function>
00091   class func
00092     : public func_base
00093   {
00094   public:
00095     func(Function f)
00096       : f_(f)
00097     {
00098     }
00099 
00100     virtual void run()
00101     {
00102       f_();
00103     }
00104 
00105   private:
00106     Function f_;
00107   };
00108 
00109   ::HANDLE thread_;
00110 };
00111 
00112 inline DWORD WINAPI wince_thread_function(LPVOID arg)
00113 {
00114   std::auto_ptr<wince_thread::func_base> func(
00115       static_cast<wince_thread::func_base*>(arg));
00116   func->run();
00117   return 0;
00118 }
00119 
00120 } // namespace detail
00121 } // namespace asio
00122 
00123 #endif // defined(BOOST_WINDOWS) && defined(UNDER_CE)
00124 
00125 #include "asio/detail/pop_options.hpp"
00126 
00127 #endif // ASIO_DETAIL_WINCE_THREAD_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines


Castor
Author(s): Carpe Noctem
autogenerated on Fri Nov 8 2013 11:05:40