216 lines
5.5 KiB
C
216 lines
5.5 KiB
C
|
#ifndef headerfilethreadcontroller
|
||
|
#define headerfilethreadcontroller
|
||
|
|
||
|
/*!
|
||
|
\file threadcontroller.h
|
||
|
\brief class ThreadController manages our two threads
|
||
|
*/
|
||
|
|
||
|
#include <ttmath/ttmathobjects.h>
|
||
|
#include <windows.h>
|
||
|
#include "stopcalculating.h"
|
||
|
|
||
|
|
||
|
/*!
|
||
|
\brief the object of this class (there's only one) will be 'managing' our two threads
|
||
|
|
||
|
the first main thread is started with the application (when the system
|
||
|
runs the programme) and the second one is when the application creates it
|
||
|
at the beginning in WinMain function, the second thread is only used for calculating
|
||
|
|
||
|
as you know we have some common objects for example the string for parsing,
|
||
|
user-defined variables and functions etc. and those objects are set in the first
|
||
|
thread (the first thread is joined with gui), and when we want to make our calculations
|
||
|
we must put them to the second thread and then we need an object which helps us
|
||
|
with that job
|
||
|
|
||
|
there's only one object of this class in our application, we can get a pointer
|
||
|
to it by using GetPrgRes() function and then by GetThreadController() method
|
||
|
|
||
|
when we would like to change for example the input string first we must call
|
||
|
StopCalculating() method then we can change what we want to change and then
|
||
|
we must call StartCalculating(), for example if we wanted to change the precision
|
||
|
of displaying we'd have to do:
|
||
|
GetPrgRes()->GetThreadController()->StopCalculating();
|
||
|
GetPrgRes()->SetPrecision( ..new_precision.. );
|
||
|
GetPrgRes()->GetThreadController()->StartCalculating();
|
||
|
*/
|
||
|
class ThreadController
|
||
|
{
|
||
|
public:
|
||
|
|
||
|
/*!
|
||
|
the default constructor
|
||
|
(notice that there'll be only one object of this class)
|
||
|
*/
|
||
|
ThreadController()
|
||
|
{
|
||
|
calculations = 0;
|
||
|
ready_for_stop = 0;
|
||
|
exit_thread = false;
|
||
|
}
|
||
|
|
||
|
|
||
|
/*!
|
||
|
the destructor
|
||
|
*/
|
||
|
~ThreadController()
|
||
|
{
|
||
|
if(calculations) CloseHandle(calculations);
|
||
|
if(ready_for_stop) CloseHandle(ready_for_stop);
|
||
|
}
|
||
|
|
||
|
|
||
|
/*!
|
||
|
it initializes an object of this class
|
||
|
|
||
|
we create two system event and initialize the 'stop_calculating' object
|
||
|
*/
|
||
|
bool Init() volatile
|
||
|
{
|
||
|
char * buffer = new char[300];
|
||
|
|
||
|
// with 'GetTickCount()' we're generating an unique identifier of our event
|
||
|
// (there can be another window of ttcalc)
|
||
|
sprintf((char*)buffer,"TTCalcEventForManagingThreads9928%u",
|
||
|
(unsigned int)GetTickCount());
|
||
|
|
||
|
// 'calculations' will be for auto-reseting and initialized as non-signaled
|
||
|
if( (calculations = CreateEvent(0,false,false,(char*)buffer))==NULL)
|
||
|
{
|
||
|
delete [] buffer;
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
sprintf((char*)buffer,"TTCalcEventReadyForStop5567%u",
|
||
|
(unsigned int)GetTickCount());
|
||
|
|
||
|
// 'ready_for_stop' will be for manual-reseting and initialized as signaled
|
||
|
// 'manual-reset' means that we must call ResetEvent() function (from WinAPI)
|
||
|
// to manually reset the state to nonsignaled
|
||
|
if( (ready_for_stop = CreateEvent(0,true,true,(char*)buffer))==NULL)
|
||
|
{
|
||
|
delete [] buffer;
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
delete [] buffer;
|
||
|
|
||
|
return stop_calculating.Init();
|
||
|
}
|
||
|
|
||
|
|
||
|
/*!
|
||
|
when the second thread leaves the WaitForCalculatingAndBlockForStop() method
|
||
|
then there's special time for making copy of certain objects (e.g. the input
|
||
|
string, user-defined variables, functions etc.) and when the second thread
|
||
|
will have finished that then it call ReadyForStop() method
|
||
|
*/
|
||
|
void ReadyForStop() volatile
|
||
|
{
|
||
|
SetEvent( ready_for_stop );
|
||
|
}
|
||
|
|
||
|
|
||
|
/*!
|
||
|
if we want to close the application for example when the user pushed the
|
||
|
close button we call StopCalculatingAndExitThread() from the first main thread (gui),
|
||
|
it means that the second thread (calculations) will finish itself
|
||
|
*/
|
||
|
void StopCalculatingAndExitThread() volatile
|
||
|
{
|
||
|
WaitForSingleObject(ready_for_stop,INFINITE);
|
||
|
|
||
|
stop_calculating.Stop();
|
||
|
exit_thread = true;
|
||
|
SetEvent(calculations);
|
||
|
}
|
||
|
|
||
|
|
||
|
/*!
|
||
|
when we want to change something for caltulating for example the input string
|
||
|
first we must call StopCalculating()
|
||
|
|
||
|
StopCalculating() waits for the second thread (if it is in the special time
|
||
|
of copying variables) then sets the 'stop object' for signaled and returns to
|
||
|
the caller
|
||
|
*/
|
||
|
void StopCalculating() volatile
|
||
|
{
|
||
|
WaitForSingleObject(ready_for_stop, INFINITE);
|
||
|
|
||
|
stop_calculating.Stop();
|
||
|
}
|
||
|
|
||
|
|
||
|
/*!
|
||
|
when we have changed what we wanted we call StartCalculating()
|
||
|
in other words it means that the calculations will start
|
||
|
(maybe now, maybe at once if the second thread is still working)
|
||
|
*/
|
||
|
void StartCalculating() volatile
|
||
|
{
|
||
|
SetEvent(calculations);
|
||
|
}
|
||
|
|
||
|
|
||
|
/*!
|
||
|
this is the main method which is used by the second thread,
|
||
|
if there's nothing to do this method (and the second thread as well) waits
|
||
|
*/
|
||
|
volatile bool WaitForCalculatingAndBlockForStop() volatile
|
||
|
{
|
||
|
WaitForSingleObject(calculations,INFINITE);
|
||
|
ResetEvent(ready_for_stop);
|
||
|
|
||
|
stop_calculating.Start();
|
||
|
|
||
|
return !exit_thread;
|
||
|
}
|
||
|
|
||
|
|
||
|
/*!
|
||
|
this method returns the pointer to the 'stop object'
|
||
|
it's used by the second thread during calculating
|
||
|
*/
|
||
|
const volatile ttmath::StopCalculating * GetStopObject() volatile const
|
||
|
{
|
||
|
return &stop_calculating;
|
||
|
}
|
||
|
|
||
|
|
||
|
/*!
|
||
|
it returns 'true' if there was a stop signal during calculating
|
||
|
the stop signal can be caused by the first thread (gui thread)
|
||
|
*/
|
||
|
bool WasStopSignal() volatile const
|
||
|
{
|
||
|
return stop_calculating.WasStopSignal();
|
||
|
}
|
||
|
|
||
|
|
||
|
private:
|
||
|
|
||
|
// auto-reset, initialized as non-signaled
|
||
|
HANDLE calculations;
|
||
|
|
||
|
// manual-reset, initialized as signaled
|
||
|
HANDLE ready_for_stop;
|
||
|
|
||
|
bool exit_thread;
|
||
|
|
||
|
NewStopCalculating stop_calculating;
|
||
|
|
||
|
|
||
|
/*!
|
||
|
we make the copy-constructor private so that nobody will be able
|
||
|
to make a copy of the one object of this class
|
||
|
*/
|
||
|
ThreadController(const ThreadController & c)
|
||
|
{
|
||
|
}
|
||
|
};
|
||
|
|
||
|
|
||
|
#endif
|