You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
1.6KB

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