-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCThread.cpp
More file actions
71 lines (55 loc) · 1.95 KB
/
CThread.cpp
File metadata and controls
71 lines (55 loc) · 1.95 KB
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
#include "CThread.h"
#include <iostream>
using namespace std;
extern "C" {
int startThread(void* arg) { // this friend allows me to treat any thread subclass the same
CThread* genericThread = static_cast<CThread*>(arg);
return genericThread->execute();
}
}
CThread::CThread() {
threadCount++;
updateCount = 0;
stopped = false;
}
CThread::~CThread(){
//cout << "\n~CThread() is running now.\n" << endl;
//cout << " is workingData NULL? " << (workingData==NULL) << endl;
//free(workingData); // --> causing *** glibc detected *** invalid pointer
}
int CThread::execute() { //virtual
cout << "\nCThread.execute() is running now.\n" << endl;
return 0;
}
int CThread::start(void* inputData,uint8_t size) {
//cout << "\nCThread.start() is running now.\n" << endl;
workingSize = size;
workingData = malloc(workingSize); // need this or workingData is NULL and that blows up memcpy
// memcpy the working data
/*
// common segmentation fault is the pointers being NULL
cout << " is workingData NULL? " << (workingData==NULL) << endl;
cout << " is inputData NULL? " << (inputData==NULL) << endl;
*/
memcpy(workingData,inputData,workingSize);
//cout << " memcpy successful" << endl;
thread = SDL_CreateThread(startThread,static_cast<void*>(this));
//looks like SDL_CreateThread requires a function(void* arg) as input as its first argument:
//extern DECLSPEC SDL_Thread * SDLCALL SDL_CreateThread(int (SDLCALL *fn)(void *), void *data);
return 0;
}
void CThread::stop() {
stopped = true;
}
void CThread::update(void* outputData) {
/*
updateCount++;
cout << "\nCThread.update() is running now." << endl;
cout << " update() has been called " << updateCount << " times." << endl;
cout << " outputData = " << *(static_cast<int*>(outputData)) << endl;
cout << " workingData = " << *(static_cast<int*>(workingData)) << endl;
*/
memcpy(outputData,workingData,workingSize);
//cout << " memcpy successful" << endl;
}
int CThread::threadCount = 0;