![]() |
libzmq master
The Intelligent Transport Layer
|
00001 /* 00002 Copyright (c) 2010-2011 250bpm s.r.o. 00003 Copyright (c) 2007-2011 iMatix Corporation 00004 Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file 00005 00006 This file is part of 0MQ. 00007 00008 0MQ is free software; you can redistribute it and/or modify it under 00009 the terms of the GNU Lesser General Public License as published by 00010 the Free Software Foundation; either version 3 of the License, or 00011 (at your option) any later version. 00012 00013 0MQ is distributed in the hope that it will be useful, 00014 but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 GNU Lesser General Public License for more details. 00017 00018 You should have received a copy of the GNU Lesser General Public License 00019 along with this program. If not, see <http://www.gnu.org/licenses/>. 00020 */ 00021 00022 #ifndef __ZMQ_THREAD_HPP_INCLUDED__ 00023 #define __ZMQ_THREAD_HPP_INCLUDED__ 00024 00025 #include "platform.hpp" 00026 00027 #ifdef ZMQ_HAVE_WINDOWS 00028 #include "windows.hpp" 00029 #else 00030 #include <pthread.h> 00031 #endif 00032 00033 namespace zmq 00034 { 00035 00036 typedef void (thread_fn) (void*); 00037 00038 // Class encapsulating OS thread. Thread initiation/termination is done 00039 // using special functions rather than in constructor/destructor so that 00040 // thread isn't created during object construction by accident, causing 00041 // newly created thread to access half-initialised object. Same applies 00042 // to the destruction process: Thread should be terminated before object 00043 // destruction begins, otherwise it can access half-destructed object. 00044 00045 class thread_t 00046 { 00047 public: 00048 00049 inline thread_t () 00050 { 00051 } 00052 00053 // Creates OS thread. 'tfn' is main thread function. It'll be passed 00054 // 'arg' as an argument. 00055 void start (thread_fn *tfn_, void *arg_); 00056 00057 // Waits for thread termination. 00058 void stop (); 00059 00060 // These are internal members. They should be private, however then 00061 // they would not be accessible from the main C routine of the thread. 00062 thread_fn *tfn; 00063 void *arg; 00064 00065 private: 00066 00067 #ifdef ZMQ_HAVE_WINDOWS 00068 HANDLE descriptor; 00069 #else 00070 pthread_t descriptor; 00071 #endif 00072 00073 thread_t (const thread_t&); 00074 const thread_t &operator = (const thread_t&); 00075 }; 00076 00077 } 00078 00079 #endif