Thread Executor
unknown
c_cpp
5 years ago
1.7 kB
8
Indexable
//
typedef void (thread_func)(void *);
void sleep(int milliseconds);
class Thread {
public:
void run(thread_func exec_fun);
void kill();
bool isRunning();
};
class Task {
public:
void execute();
};
// START HERE >>>>>>>>>>>>>
#include <vector>
#define STATUS_IDLING 0
#define STATUS_WORKING 1
#define STATUS_EXIT 2
class TaskExecutor {
public:
void start() {
thread = new Thread();
thread->run(execute);
killed = false;
}
void stop() {
tasks.clear();
killed = true;
thread->kill();
delete thread;
}
void taskAdd( Task * _task ) {
tasks.push_back( _task );
}
int getStatus() {
if( thread->isRunning() && current_task == 0 ) {
return STATUS_IDLING;
}
if( thread->isRunning() && current_task != 0 ) {
return STATUS_WORKING;
}
if( !thread->isRunning() ) {
return STATUS_EXIT;
}
}
static void execute(void * instance) {
TaskExecutor * te = (TaskExecutor*)instance;
while(!te->killed)
{
if (!te->tasks.empty()) {
te->current_task = te->tasks[0];
te->current_task->execute();
delete te->current_task;
te->current_task = 0;
}
sleep(100);
}
}
bool killed = false;
std::vector<Task *> tasks;
Task * current_task;
Thread * thread;
};Editor is loading...