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
|
#define delay(x) _sleep(x)
class Multithread
{
private:
HANDLE hThread=NULL;
DWORD dwThreadId=0;
bool blnSingleThreadAtTime=false;
bool blnCreationOrder=true;
function<void()> multithreadfunction=NULL;
function<void(const LPVOID param)> multithreadparameterfunction=NULL;
HANDLE myEvent = CreateEvent(0, 0, 0, 0);
struct mytread
{
Multithread *classpointer=NULL;
LPVOID multithreadparameters=NULL;
};
vector<mytread> mtparameterthread;
public:
void SingleThreadAtTime(bool SingleThread)
{
blnSingleThreadAtTime=SingleThread;
}
bool SingleThreadAtTime()
{
return blnSingleThreadAtTime;
}
void CreationOrder(bool OrderCreation)
{
blnCreationOrder=OrderCreation;
}
bool CreationOrder()
{
return blnCreationOrder;
}
void WaitCondition()
{
WaitForSingleObject(myEvent, INFINITE);
}
void SetCondition()
{
SetEvent(myEvent);
}
static DWORD WINAPI MyThreadFunction( const LPVOID lpParam )
{
static bool blnfirsttime=false;
Multithread *pThis = static_cast<Multithread*>(lpParam);
if(pThis->blnSingleThreadAtTime==true && blnfirsttime==true)
pThis->WaitCondition();
blnfirsttime=true;
pThis->multithreadfunction();
if(pThis->blnSingleThreadAtTime==true && blnfirsttime==true)
pThis->SetCondition();
return 0;
}
static DWORD WINAPI MyParameterThreadFunction( const LPVOID lpParam )
{
static bool blnfirsttime=false;
mytread *pThis = static_cast<mytread*>(lpParam);
if(pThis->classpointer->blnSingleThreadAtTime==true && blnfirsttime==true)
pThis->classpointer->WaitCondition();
blnfirsttime=true;
pThis->classpointer->multithreadparameterfunction(pThis->multithreadparameters);
if(pThis->classpointer->blnSingleThreadAtTime==true && blnfirsttime==true)
pThis->classpointer->SetCondition();
return 0;
}
Multithread(std::function<void()> SetFunction)
{
mtparameterthread.resize(100);
multithreadfunction=SetFunction;
}
Multithread( function<void(LPVOID)> SetFunction )
{
mtparameterthread.resize(100);
multithreadparameterfunction =SetFunction;
}
//corrigir
template<typename tpVariant>
Multithread( function<void(tpVariant)> SetFunction )
{
multithreadparameterfunction = [=]( LPVOID pv ) { SetFunction( static_cast<tpVariant>( pv ) ); };
}
int i=-1;
template<typename tpVariant>
void operator ()(tpVariant &vrParam)
{
LPVOID lpParam=static_cast<LPVOID>(&vrParam);
i=i+1;
mtparameterthread[i].classpointer=this;
mtparameterthread[i].multithreadparameters=lpParam;
DebugText(to_string(vrParam));
hThread= CreateThread(
NULL, // default security attributes
0, // use default stack size
Multithread::MyParameterThreadFunction, // thread function name
static_cast<LPVOID>(&mtparameterthread[i]), // argument to thread function
0, // use default creation flags
&dwThreadId);
if(i==100)
i=-1;
if(blnCreationOrder==true)
delay(100);
}
void operator ()(LPVOID vrParam)
{
i=i+1;
mtparameterthread[i].classpointer=this;
mtparameterthread[i].multithreadparameters=vrParam;
LPVOID lptest=static_cast<LPVOID>(&mtparameterthread[i]);
hThread= CreateThread(
NULL, // default security attributes
0, // use default stack size
&Multithread::MyParameterThreadFunction, // thread function name
lptest, // argument to thread function
0, // use default creation flags
&dwThreadId);
if(i==100)
i=-1;
//delay for avoiding execution of all threads in same time and for not losing data because of that
if(blnCreationOrder==true)
delay(100);
}
void operator ()()
{
hThread = CreateThread(
NULL, // default security attributes
0, // use default stack size
&Multithread::MyThreadFunction, // thread function name
this, // argument to thread function
0, // use default creation flags
&dwThreadId);
if(blnCreationOrder==true)
delay(100);
}
~Multithread()
{
CloseHandle(hThread);
CloseHandle(myEvent);
}
};
|