Every programmer knows why parallel programming is used. The principles are the same for all programming languages, but the declaration is different.
I designed a library for parallel programming in threads for console and DLL applications.
 

Certainly most have ever encountered a problem that they needed to solve in the console application through parallel programming.
However, since the declaration of threads in a console application is quite different from Windows Form and much more complex in a console application, each of you will appreciate the module I designed to facilitate parallel programming.

The module contains a structure of classes that take care of running the function and its timing. It is possible to set two timers for each thread by the method:

   void SetTimer(Call behindcall, Call aftercall) 

Parameter "behindcall" indicates the time at which the function is to run, that is, the time that defines how long the thread should be paused before the function is executed.

Parameter "aftercall" indicates the time that defines how long the thread should be stopped after the function is executed.

 

 
The function name to be timed in the thread is inserted into the method:
 
   bool StartTimer(bool (*function)()) 

The function whose name is inserted in the StartTimer method must be static! It must not be a function called from an instance of a class!

 

The thread itself in Console application, in which the previous methods are run must be declared by the macro:

   unsigned int __stdcall



The thread itself in  DLL application,  in which the previous methods are run must be declared by the macro:

   void _cdecl 

or

   void 





Example of use Console Application:



#include "thread.h"
bool Foo1(void) { ::std::cout << "Hello Word" << endl; return 0; }
bool Foo2(void) { ::std::cout << (1+5)-3 << endl;
if((1+5)-3 == 3) { return true; } else return false; }
unsigned int __stdcall _Thread1(void * input) { ::Threading::MultyThreadsW::Timer * threadtimer = new ::Threading::MultyThreadsW::Timer(); threadtimer->SetThreadTimer(0,2000); return threadtimer->StartTimer(Foo1); }
unsigned int __stdcall _Thread2(void * input) { ::Threading::MultyThreadsW::Timer * threadtimer = new ::Threading::MultyThreadsW::Timer(); threadtimer->SetThreadTimer(1000,5000); return threadtimer->StartTimer(Foo2); }
int main(void) { // Deklaracia ID vlakien // ::Threading::ThreadID thread1 = 0; ::Threading::ThreadID thread2 = 1;
// Inicializacia a spustenie vlakien // ::Threading::ThreadHandle tHandle1 = ::Threading::MultyThreads->ThreadStart(_Thread1, NULL, &thread1); ::Threading::ThreadHandle tHandle2 = ::Threading::MultyThreads->ThreadStart(_Thread2, NULL, &thread2);
// Cakanie na dokoncenie vlakien // ::Threading::MultyThreads->WaitingForThread(tHandle1); ::Threading::MultyThreads->WaitingForThread(tHandle2);
// Ukoncenie vlakien // ::Threading::MultyThreads->CloseThread(tHandle1); ::Threading::MultyThreads->CloseThread(tHandle2);
return 0; }





Example of using function calls from class instances :


#include "thread.h" class Foo {
private:
int a; int b; int c;
public: Foo(void) { this->a = 1; this->b = 5; this->c = 3; }
bool Foo1(void) { ::std::cout << "Hello World" << endl;
return 0; }

bool Foo2(void) { ::std::cout << (a+b)-c << endl; if((a+b)-c == 3) { return true; } else return false; } }
Foo * foo = new Foo();
bool A(void) { return foo->Foo1(); }
bool B(void) { return foo->Foo2(); }
unsigned int __stdcall _Thread1(void * input) { ::Threading::MultyThreadsW::Timer * threadtimer = new ::Threading::MultyThreadsW::Timer(); threadtimer->SetThreadTimer(0,2000); return threadtimer->StartTimer(A); }
unsigned int __stdcall _Thread2(void * input) { ::Threading::MultyThreadsW::Timer * threadtimer = new ::Threading::MultyThreadsW::Timer(); threadtimer->SetThreadTimer(1000,5000); return threadtimer->StartTimer(B); }
int main(void) { . . . }





Example of use DLL Application:



#include "thread.h"
bool Foo1(void) { ::std::cout << "Hello Word" << endl; return 0; }
bool Foo2(void) { ::std::cout << (1+5)-3 << endl;
if((1+5)-3 == 3) { return true; } else return false; }
void _Thread1(void * input) { ::Threading::MultyThreadsW::Timer * threadtimer = new ::Threading::MultyThreadsW::Timer(); threadtimer->SetThreadTimer(0,2000); return threadtimer->StartTimer(Foo1); }
void _cdecl _Thread2(void * input) { ::Threading::MultyThreadsW::Timer * threadtimer = new ::Threading::MultyThreadsW::Timer(); threadtimer->SetThreadTimer(1000,5000); return threadtimer->StartTimer(Foo2); }
void OnRunProcess(void) { // Deklaracia ID vlakien // ::Threading::threadId thread1 = 1; ::Threading::threadId thread2 = 2;
// Inicializacia a spustenie vlakien // ::Threading::ThreadHandle tHandle1 = ::Threading::MultyThreads->ThreadStartDLL(_Thread1, 0, &thread1); ::Threading::ThreadHandle tHandle2 = ::Threading::MultyThreads->ThreadStartDLL(_Thread2, 0, &thread2);

//alebo // Inicializacia a spustenie vlakien // ::Threading::ThreadHandle tHandle1 = (HANDLE) _beginthread(_Thread1, 0,&thread1); ::Threading::ThreadHandle tHandle2 = (HANDLE) _beginthread(_Thread2, 0,&thread2);
// Ukoncenie vlakien // ::Threading::MultyThreads->CloseThread(tHandle1); ::Threading::MultyThreads->CloseThread(tHandle2); }

BOOL APIENTRY DllMain (HINSTANCE hInst, DWORD reason, LPVOID reserved) { switch (reason) { case DLL_PROCESS_ATTACH: //MessageBoxA(0, "Process is Attach", "Attach", MB_OK); OnRunProcess(); break;
case DLL_PROCESS_DETACH: //MessageBoxA(0, "Process is Detach", "Detach", MB_OK); //OnDeatachProcess(); break;
case DLL_THREAD_ATTACH: //MessageBoxA(0, "Thread is Attach", "Detach", MB_OK); //OnRunThread(); break;
case DLL_THREAD_DETACH: //MessageBoxA(0, "Thread is Detach", "Detach", MB_OK); //OnDeatachThread(); break;
default : //Others(); }; return TRUE; }

 






thread.h
---------------------------------------------------------------------
Language : C++
IDE: Visual Studio IDE
Last revision: 11.11 2018
Author: Diallix

Download Header:

---------------------------------------------------------------------