vghiware
8/8/2004 11:31:00 AM
Hi !
I am working on a library, which has a synchronize method
to ensure all programs using this library use the correct resource
file.
However this function is called only when the process makes an attempt
to get some data ( why ? because someone designed it this way )
--snip--
myClass::GetData()
{
Synchronize()
/* get the data from the latest file
* return
*/
}
--snap--
If the process using this library doesn't call the GetData Method
frequently
enough, it would cause the process to have an handle to stale resource
for too long :( and unnecessarily waste the disk space.
Somehow if the Synchronize function was called frequently by the
programs , my problems are gone, however none of the programs
want changes in their code nor they want to recompile and
link to the new library( once i have made the changes )
I plan to create a thread in the constructor of my library class , and
thus start a thread for each process which keeps on calling the
Synchronize() ,say every 5 sec.( am i implimenting the singleton
pattern ?? )
--snip--
myClass::myClass{
/* do initialization */
pthread_create(&thread,NULL,callThread,NULL)
}
void *callThread(void)
{
if(pthread_mutex_trylock(&mymutex)){
// error
// A thread is already running.
pthread_exit(NULL);
}else{
// i have the lock now, keep calling the synchronize method.
myClass temp;
while(1)
{
sleep(5);
temp.Syncronize();
}
}// else ends
}//callThread ends
--snap--
I have kept the mutex in global scope and initialized it.
1)Is constructor ok ??
2)I do not handle any signals in the thread (or any function of the
library ),so the thread should not cause any harm to the signal
handling mechanism of the process? right?
3)But im not sure what would happen if the any of the programs using
this lib do a fork in the main() and not do execvp ( or any related
functions )
4)Also what if the programs using the library , do not link to
libpthread.sl?
will it suffice if i link my library to libpthread.sl ??
The prototype works fine for me,till now.
im working on HP-UX 11.0
Do you see any issue with this kind of implementation ?
Thanks in advance.
vaibhav