OK, I have some very complicated datatypes, that have member functions executing as THREADS constantly. There will be at any given time 1 to 3 member functions running constantly as a thread in the object. BTW the object is called ALARM. Lets you set an absoloute time, function * and void * and when the time is reached it will simply execute the function via pointer, passing it the void * as data. So i have alarm1 = somestuff. same with alarms 2 and 3. If i say alarm1 = alarm2, what happens? These running threads dont get stopped to my knowledge, So would what alarm1 was still be running in memory? Would the destructor be called? Is it simply freed memory?
I have posted the as of yet UNFINISHED but WORKING code for both parts below, and i NEED input to know how i should go about protecting these threads from accidentally getting cut, or knowing what i need to do to make it work properly.
Compiles Clean on MS VC++6.0 SP5:
Code for main.cpp:
Code:
#include "alarm.h"
#include <iostream>
void myfunc(void * spaz)
{
int * x = static_cast<int*>(spaz);
std::cout << "asdf: " << *x << std::endl;
return;
}
int main()
{
int x;
x = 4;
void *bob = &x;
myfunc(bob);
/*year, month, day, hour, minute, second, function, data to pass*/
Alarm thealarm = Alarm(2003,10,10,17,11,30,*myfunc,bob);
thealarm = Alarm(2003,10,10,17,11,20,*myfunc,bob);
while(1){Sleep(1000);}
return 0;
}
Code for alarm.h
Code:
#ifndef __ALARM_H__
#define __ALARM_H__
//DISABLE STUPID STL WARNINGS
#pragma warning(disable:4786)
#include <string>
#include <vector>
#include <sstream>
#include <time.h>
#include <windows.h>
class Alarm
{
public:
Alarm();
Alarm(int inYear, int inMonth, int inDay, int inHour, int inMinute, int inSecond, void (*inPtr)(void *),void * inPtrData);
~Alarm();
private:
//Called In The Constructor To Start Checking If We Should Execute
static void CreateAlarmCheckThread(void *param)
{
Alarm *thisAlarm = static_cast<Alarm*>(param);
thisAlarm->AlarmCheckThread();
}
//Loop forever to see if we should alarm.
void AlarmCheckThread();
//Update the nowTime variables with the current time
void UpdateTime();
//Do we want to terminate the thread?
bool die;
//Time to execute
int exeYear;
int exeMonth;
int exeDay;
int exeHour;
int exeMinute;
int exeSecond;
void (*exePtr)(void *);
void * exePtrData;
//Time now
int nowYear;
int nowMonth;
int nowDay;
int nowHour;
int nowMinute;
int nowSecond;
//variables
};
Alarm::Alarm()
{
//We are ready to terminate because we have no vars yet.
die = true;
//No variables to initiate yet.
return;
}
Alarm::Alarm(int inYear, int inMonth, int inDay, int inHour, int inMinute, int inSecond, void (* inPtr)(void *), void * inPtrData)
{
//We are not ready to terminate
die = false;
//Set Other Variables
exeYear = inYear;
exeMonth = inMonth;
exeDay = inDay;
exeHour = inHour;
exeMinute = inMinute;
exeSecond = inSecond;
exePtr = inPtr;
exePtrData = inPtrData;
//Create The AlarmCheckThread To See If We Should Alarm
CreateThread(0,0,(LPTHREAD_START_ROUTINE)CreateAlarmCheckThread,this,0,0);
//Done
return;
}
Alarm::~Alarm()
{
//Tell the AlarmCheckThread to terminate.
die = true;
//Wait for the AlarmCheckThread to terminate.
Sleep(1500);
}
void Alarm::AlarmCheckThread()
{
//Do until the destructor is called
while(!die)
{
UpdateTime();
if(exeYear==nowYear&&exeMonth==nowMonth&&exeDay==nowDay&&exeHour==nowHour&&exeMinute==nowMinute&&exeSecond==nowSecond)
{ exePtr(exePtrData); Sleep(1000); }
//Check to see if i should Alarm
Sleep(500);
}
}
void Alarm::UpdateTime()
{
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
std::string timestring = asctime(timeinfo);
//Contains: Thu Oct 09 20:38:45 2003
//5 [0-4] strings, day. month, ##date ##hour:##minutes:##seconds ####year
for(int i=0;i<timestring.size();i++)
{
if (timestring[i]==':') { timestring[i] = ' '; }
}
//Contains: Thu Oct 09 20 38 45 2003
//7 [0-6] strings, day. month, ##date ##hour ##minutes ##seconds ####year
std::vector<std::string> words;
std::istringstream is(timestring);
std::string word;
while(is>>word)
{ words.push_back(word); }
nowYear = atoi(words[6].c_str());
//Thx Local$$$$$.
nowMonth = ( strstr( "Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec", words[1].c_str() ) - "Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec" ) / 4 + 1;
nowDay = atoi(words[2].c_str());
nowHour = atoi(words[3].c_str());
nowMinute = atoi(words[4].c_str());
nowSecond = atoi(words[5].c_str());
return;
}
/*LOTS AND LOTS OF UNFINISHED CODE*/
class AlarmSystem
{
public:
AlarmSystem();
~AlarmSystem();
AddAlarm();
DelAlarm();
private:
std::vector<Alarm> alarms;
};
#endif