summaryrefslogtreecommitdiffstats
path: root/Master/Real-Time Systems/RTS_A8/src/Task.cpp
blob: e0bce6679b2a59d8bd323ee2e0c1692ec092fc65 (plain)
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
/*
 * Task.cpp
 *
 *  Created on: 30.11.2010
 *      Author: istsveise
 */

#include "Task.h"
#include <pthread.h>
#include <iostream>
#include <sys/time.h>
#include <signal.h>

using namespace std;

void* timer_main(void* task) {
	Task* t = (Task*) task;
	while(true) {
		usleep(t->T*1000);
		sem_post(&t->threadSem);
	}
	return NULL;
}

void* execute_main(void* task) {
	cout << "Start execution..." << endl;
	Task* t = (Task*) task;
	t->execute();

	return NULL;
}

Task::Task(int t=1000)
:T(t)
{
	sem_init(&this->threadSem,0,0);

	pthread_t timerthread;
	pthread_t executethread;

	pthread_create(&timerthread,NULL,&timer_main,(void*) this);
	pthread_create(&executethread,NULL,&execute_main,(void*) this);
}

Task::~Task() {
}

void Task::waitForNextCycle()
{
	sem_wait(&this->threadSem);
}