#include "J118/Thread.h"//TODO: Fix crash when deleting right after creation.namespace J118
Thread::Thread()
thread_handle = NULL;
strcpy(thread_name,
"Uknown");
running =
false;
Thread::Thread(
constchar* n)\
thread_handle = NULL;
strcpy(thread_name,n);
running =
false;
data_lock =
new std::mutex();
Thread::~Thread()
printf(
"Deleting thread\n");
//data_lock->lock();if(thread_handle != NULL)
//data_lock->unlock();
printf(
"ID001\n");
join();
printf(
"ID002\n");
//thread_handle->detach();delete thread_handle;
thread_handle = NULL;
data_lock->unlock();
SAFE_DELETE(data_lock);
void Thread::runProxy(
void* me)
printf(
"In thread...\n");
Thread* callee = (Thread*)me;
printf(
"Casted\n");
callee->running =
true;
//callee->data_lock->unlock(); // Unlocking after thread is started [ID001]
callee->run();
//printf("POINT %x\n",((void*)callee->thread_handle));//delete callee->thread_handle;//callee->thread_handle = NULL;
callee->running =
false;
void Thread::start()
printf(
"SSSSS\n");
if(thread_handle != NULL)
//throw ThreadRunningException();if(running)
printf(
"Oh no...\n");
throw ThreadRunningException();
return;
join();
//thread_handle->detach();delete thread_handle;
thread_handle = NULL;
printf(
"Starting\n");
//data_lock->lock(); //Locking to start the thread [ID001]
thread_handle =
new std::thread(runProxy,
this);
printf(
"started\n");
void Thread::join()
int result = data_lock->try_lock();
printf(
"Try lock returned %d\n", result);
data_lock->lock();
//----------------------------------------CRASHES HERE------------------------------------------------
printf(
"LOCKED\n");
if(thread_handle != NULL && thread_handle->joinable())
printf(
"ID003\n");
thread_handle->join();
printf(
"ID004\n");
delete thread_handle;
thread_handle = NULL;
data_lock->unlock();
data_lock->unlock();
constchar* ThreadRunningException::what()
constthrow()
return"Instance of thread already running";
}
/* namespace J118 */1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
|
include/J118/Thread.h