paulstretch_cpp

PaulStretch
Log | Files | Refs | LICENSE

Thread.cpp (1614B)


      1 /*
      2   Copyright (C) 2009 Nasca Octavian Paul
      3   Author: Nasca Octavian Paul
      4 
      5   This program is free software; you can redistribute it and/or modify
      6   it under the terms of version 2 of the GNU General Public License 
      7   as published by the Free Software Foundation.
      8 
      9   This program is distributed in the hope that it will be useful,
     10   but WITHOUT ANY WARRANTY; without even the implied warranty of
     11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     12   GNU General Public License (version 2) for more details.
     13 
     14   You should have received a copy of the GNU General Public License (version 2)
     15   along with this program; if not, write to the Free Software Foundation,
     16   Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
     17 */
     18 #include <unistd.h>
     19 #include "Thread.h"
     20 #include "globals.h"
     21 
     22 #ifdef WINDOWS
     23 DWORD WINAPI thread_function( LPVOID arg ) {
     24 #else
     25 void *thread_function(void *arg){
     26 #endif
     27     Thread *thr=(Thread *) arg;
     28     thr->run();
     29     thr->stopped=true;
     30     thr->running=false;
     31     return 0;
     32 };
     33 
     34 Thread::Thread(){
     35     running=false;
     36     stopnow=false;
     37     stopped=false;
     38 };
     39 
     40 Thread::~Thread(){
     41     stop();
     42 };
     43 
     44 bool Thread::start(){
     45     if (running) return false;
     46 #ifdef WINDOWS
     47     hThread=CreateThread(NULL,0,thread_function,this,0,NULL);
     48 #else
     49     if (pthread_create(&thread,NULL,thread_function,this)!=0)return false;
     50 #endif
     51     running=true;
     52     return true;
     53 };
     54 
     55 void Thread::stop(){
     56     if (!running) return;
     57     running=false;
     58     stopped=false;
     59     int maxwait=1000;
     60     stopnow=true;
     61     while(!stopped){
     62 	sleep(10);
     63     };
     64 };
     65 
     66 bool Thread::is_running(){
     67     return running;
     68 };
     69 
     70 
     71