threadwrap.h

Go to the documentation of this file.
00001 ///
00002 /// \file       threadwrap.h
00003 ///             RAII Wrapper for a single thread.
00004 ///
00005 
00006 /*
00007     Copyright (C) 2009, Nicolas VIVIEN
00008 
00009     This program is free software; you can redistribute it and/or modify
00010     it under the terms of the GNU General Public License as published by
00011     the Free Software Foundation; either version 2 of the License, or
00012     (at your option) any later version.
00013 
00014     This program is distributed in the hope that it will be useful,
00015     but WITHOUT ANY WARRANTY; without even the implied warranty of
00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00017 
00018     See the GNU General Public License in the COPYING file at the
00019     root directory of this project for more details.
00020 */
00021 
00022 #ifndef __BARRY_THREADWRAP_H__
00023 #define __BARRY_THREADWRAP_H__
00024 
00025 #include "dll.h"
00026 #include <pthread.h>
00027 
00028 namespace Barry {
00029 
00030 class BXEXPORT Thread
00031 {
00032 public:
00033         struct CallbackData
00034         {
00035                 void *(*callback)(Barry::Thread::CallbackData *data);
00036 
00037                 // user-accessible data
00038                 volatile bool stopflag;
00039                 void *userdata;
00040 
00041                 explicit CallbackData(void *userdata);
00042         };
00043 
00044 private:
00045         pthread_t thread;
00046         int m_socket;
00047         CallbackData *m_data;
00048 
00049 public:
00050         // Note: the data pointer passed into callback is not the same
00051         // as userdata.  The data pointer is a pointer to CallbackData
00052         // which will contain the userdata pointer.
00053         Thread(int socket,
00054                 void *(*callback)(Barry::Thread::CallbackData *data),
00055                 void *userdata);
00056         ~Thread();
00057 
00058         /// Sets stopflag in the callback data to true
00059         void StopFlag();
00060 };
00061 
00062 } // namespace Barry
00063 
00064 #endif
00065