What do you think about my Thread Class?

Jul 24, 2016 at 8:18am
closed account (48bpfSEw)
In connection with my Dash Bolder Project or any game programming it is important to handle different actions independently and in a certain time beat. For example: read keyboard input every 200 miliseconds, calculate frames every 500 seconds, move enemies every 1000 seconds and so.

Coding threads can be quit challenging, so I decided to write my own thread classes.

Here it is!

Unit test successfull:
Task 1 runs every second and ends after 5 iterations
Task 2 runs every 2 seconds and ends after 10 iterations


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
#include <time.h>
#include <sstream.h>


// MY THREAD
class MyThread {
public:
  bool    boRunning;
  int     iCounter;
  int     milliseconds;
  
private:
  clock_t time_end;

public:
  MyThread () {
    boRunning     = false;
    iCounter      = 0;
    milliseconds  = 500;    // default
    TimerExecute();
    }
    
  void TimerExecute () {
    time_end = clock() + milliseconds * CLOCKS_PER_SEC/1000;
    }
    
  bool IsTimerFinished () {
    return clock() > time_end;
  }
  
  virtual void Execute    () = 0;
};

class MyThread_Task1 : public MyThread {
public:
  MyThread_Task1 () : MyThread () {
    boRunning     = true;
    milliseconds  = 1000;
    }
    
  void Execute () {
    if (!boRunning)
      return;
    if (IsTimerFinished()) {   
      iCounter ++;
      std::cout << "Task1: " << iCounter << endl;
      TimerExecute();
      
      if (iCounter > 5) { 
        boRunning = false;
        std::cout << "Task1 finished!" << endl;
        }
      }
    } 
};


class MyThread_Task2 : public MyThread {
public:
  MyThread_Task2 () : MyThread () {
    boRunning     = true;
    milliseconds  = 2000;
    }
    
  void Execute () {
    if (!boRunning)
      return;
    
    if (IsTimerFinished()) {    
      iCounter ++;
      std::cout << "Task2: " << iCounter << endl;
      TimerExecute();
      
      if (iCounter > 10) {
        boRunning = false;
        std::cout << "Task2 finished!" << endl;
        }
      }
    } 
};



int main(){
    MyThread_Task1 task1;
    MyThread_Task2 task2;
    
    while (task1.boRunning || task2.boRunning) {
      task1.Execute();
      task2.Execute();
      }
      
    return 0;
}


Jul 24, 2016 at 9:29am
closed account (48bpfSEw)
And this is a multi-multi-task application ^^ :

MyThread_Tasks is a thread, too, and responsible for the two threads!


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
class MyThread_Tasks : public MyThread {
private:
  MyThread_Task1 task1;
  MyThread_Task2 task2;
    
public:
  MyThread_Tasks () : MyThread () {
    boRunning     = true;
    milliseconds  = 500;
    }
    
  void Execute () {
    if (!boRunning)
      return;
    
    if (IsTimerFinished()) {
      if (!task1.boRunning && !task2.boRunning) {
        boRunning = false;
        std::cout << "Tasks finished!" << endl;
        return;
        }
      
      task1.Execute();
      task2.Execute();
      }
    } 
};


// the main routine shrinks to:

int main(){
    MyThread_Tasks tasks;
    
    while (tasks.boRunning) 
      tasks.Execute();
      
    return 0;
}


It seems these technique are the basics of a multi-user-system with multible tasks in each user context...

Loops with "while, do-until, for" are in this case forbidden of certainly because of the democratic system. The code has to be written in an other way without loopings.


Instead of:

1
2
for (int x=0;x<10;x++) 
  doSomething(x);


the code has to be written in:

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
class MyThread_Task : public MyThread {
public:
  int x;
  int xStart;
  int xEnd;
  
public:
  MyThread_Task (int paStart, int paEnd) : MyThread () {
    xStart        = paStart;
    xEnd          = paEnd;
    x             = xStart;
    
    boRunning     = true;
    milliseconds  = 50;
    }
    
  // This function contains the for-loop as an iteration.
  void Execute () {
    if (!boRunning)
      return;
          
    if (IsTimerFinished()) {    
      doSomething(x);
      x ++;
 
      if (x >= xEnd) {
        boRunning = false;
        return;
        }

      TimerExecute();
      }
    } 
};

    
    
int main (void) {
  
  MyThread_Task task(1,10);   
  
  while (task.boRunning)
    task.Execute();
}
Topic archived. No new replies allowed.