# threadwrap.cc

Go to the documentation of this file.

    00001 ///
    00002 /// \file       threadwrap.cc
    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 #include "threadwrap.h"
    00023 #include "error.h"
    00024
    00025
    00026 namespace Barry {
    00027
    00028 Thread::Thread(int socket, void *(*callback)(void *data), void *data)
    00029 {
    00030         int ret = pthread_create(&thread, NULL, callback, data);
    00031         if( ret ) {
    00032                 throw Barry::ErrnoError("Thread: pthread_create failed.", ret);
    00033         }
    00034 }
    00035
    00036
    00037 Thread::~Thread()
    00038 {
    00039         pthread_join(thread, NULL);
    00040 }
    00041
    00042
    00043 void Thread::Dispose()
    00044 {
    00045         int ret = pthread_cancel(thread);
    00046         if( ret ) {
    00047                 throw Barry::ErrnoError("Thread: pthread_cancel failed.", ret);
    00048         }
    00049 }
    00050
    00051 } // namespace Barry
    00052