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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
|
#include <iostream>
#include <vector>
#include <windows.h>
#define VERSION 1
typedef unsigned long ulong;
DWORD WINAPI runningThread(void *);
typedef void (*fp)(void *);
struct Thread{
HANDLE handle;
#if VERSION==1
HANDLE startCallEvent,
callEndedEvent;
#endif
ulong index;
volatile fp function;
void *parameter;
volatile bool destroy;
Thread():initialized(0){}
~Thread();
void init(ulong index);
void call(fp f,void *p);
#if VERSION==0
void wait();
#elif VERSION==1
void wait(ulong timeout=INFINITE);
#endif
private:
bool initialized;
};
struct ThreadManager{
std::vector<Thread> threads;
ulong freeThreads;
ThreadManager(ulong cpu_count);
ulong call(fp f,void *p);
void wait(ulong index);
void waitAll();
};
void Thread::init(ulong index){
this->initialized=1;
this->index=index;
#if VERSION==0
this->handle=CreateThread(0,0,(LPTHREAD_START_ROUTINE)runningThread,this,CREATE_SUSPENDED,0);
#elif VERSION==1
this->startCallEvent=CreateEvent(0,0,0,0);
this->callEndedEvent=CreateEvent(0,1,1,0);
this->handle=CreateThread(0,0,(LPTHREAD_START_ROUTINE)runningThread,this,0,0);
#endif
this->function=0;
this->parameter=0;
this->destroy=0;
}
Thread::~Thread(){
if (!this->initialized)
return;
this->destroy=1;
this->function=(fp)1;
#if VERSION==0
ResumeThread(this->handle);
WaitForSingleObject(this->handle,INFINITE);
#elif VERSION==1
this->wait();
SetEvent(this->startCallEvent);
WaitForSingleObject(this->handle,INFINITE);
CloseHandle(this->startCallEvent);
CloseHandle(this->callEndedEvent);
#endif
CloseHandle(this->handle);
}
void Thread::call(fp f,void *p){
this->function=f;
this->parameter=p;
#if VERSION==0
ResumeThread(this->handle);
#elif VERSION==1
SetEvent(this->startCallEvent);
#endif
}
#if VERSION==0
void Thread::wait(){
#elif VERSION==1
void Thread::wait(ulong timeout){
#endif
#if VERSION==0
while (this->function);
SuspendThread(this->handle);
#elif VERSION==1
WaitForSingleObject(this->callEndedEvent,timeout);
#endif
}
ThreadManager::ThreadManager(ulong cpu_count){
this->threads.resize(cpu_count-1);
for (ulong a=0;a<this->threads.size();a++)
this->threads[a].init(a);
this->freeThreads=this->threads.size();
}
ulong ThreadManager::call(fp f,void *p){
long ret=-1;
if (!this->freeThreads){
while (ret<0){
for (ulong a=0;a<this->threads.size() && ret<0;a++)
if (!this->threads[a].function)
ret=a;
}
}else{
for (ulong a=0;a<this->threads.size() && ret<0;a++)
if (!this->threads[a].function)
ret=a;
this->freeThreads--;
}
this->threads[ret].call(f,p);
return ret;
}
void ThreadManager::wait(ulong index){
this->threads[index].wait();
this->freeThreads++;
}
void ThreadManager::waitAll(){
for (ulong a=0;a<this->threads.size();a++)
this->wait(a);
}
DWORD WINAPI runningThread(void *p){
Thread *t=(Thread *)p;
while (1){
#if VERSION==0
while (!t->function);
#elif VERSION==1
WaitForSingleObject(t->startCallEvent,INFINITE);
ResetEvent(t->callEndedEvent);
#endif
if (t->destroy)
break;
t->function(t->parameter);
t->function=0;
#if VERSION==1
SetEvent(t->callEndedEvent);
#endif
}
return 0;
}
void thread(void *p){
ulong b=0;
for (ulong a=0;a<1000000;a++)
b+=a;
*(ulong *)p=b;
}
#define N 10000
int main(){
ulong cpu_count=1;
{
SYSTEM_INFO si;
GetSystemInfo(&si);
cpu_count=si.dwNumberOfProcessors;
}
ThreadManager tm(cpu_count);
ulong t0,t1;
t0=GetTickCount();
ulong d;
for (ulong a=0;a<N;a++){
tm.call(thread,&d);
thread(&d);
}
tm.waitAll();
t1=GetTickCount();
double t2,t3;
t2=double(t1-t0)/double(N);
t0=GetTickCount();
for (ulong a=0;a<N;a++)
thread(&d);
t1=GetTickCount();
t3=double(t1-t0)/double(N);
std::cout <<t2<<" ms/call"<<std::endl;
std::cout <<t3<<" ms/call"<<std::endl;
std::cout <<"Threading overhead: "<<t2-t3<<" ms."<<std::endl;
return 0;
}
|