| | |
How can I have an object delete itself after X seconds?
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
In a timer thread do this:
C++ Syntax (Toggle Plain Text)
class MyObject { public: ... void Destroy() {delete this;} ... }; MyObject* obj = new MyObject; // in a timer thread obj.Destroy();
Last edited by Ancient Dragon; Nov 12th, 2008 at 9:14 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Jan 2008
Posts: 119
Reputation:
Solved Threads: 10
•
•
•
•
Obviously the possible solution depends on the general architecture of your application (and run-time environment). In standard C++ an object life time defined with regard to the only execution thread and it's not a real time.
I tried to implement a wrapper class that takes an dynamic allocated object and destroys it after X seconds. It uses the pthred library to create the timer in another thread
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <cstdio> #include <stdlib.h> #include <pthread.h> #include <unistd.h> using namespace std; class Exception {}; template < class T> class ObjTimerWrapper { private: T* obj_; int seconds_; pthread_mutex_t mutex; pthread_t timer; void DeleteObj() { pthread_mutex_lock( &mutex ); delete obj_; obj_ = 0; pthread_mutex_unlock( &mutex ); } public: ObjTimerWrapper( T* obj, unsigned int seconds):obj_(obj), seconds_(seconds) { pthread_mutex_init( &mutex, 0 ); pthread_create(&timer, NULL, ObjTimerWrapper::startTimer, (void*)this ); } ~ObjTimerWrapper() { pthread_join( timer, 0 ); pthread_mutex_destroy(&mutex); cout<<"destructor called"<<endl; } void Lock() { pthread_mutex_lock( &mutex ); } void UnLock() { pthread_mutex_unlock( &mutex ); } T* operator->() { if ( ! obj_ ) throw Exception(); return obj_; } T* get() { if ( ! obj_ ) throw Exception(); return obj_; } static void* startTimer(void * arg); }; template < class T> void *ObjTimerWrapper<T>::startTimer(void* arg) { ObjTimerWrapper* p = (ObjTimerWrapper*)arg; sleep( p->seconds_); p->DeleteObj(); cout<<"timer expired, object destroyed-----------"<<endl; return 0; } class X { public: double x; void iterate(); X( float xx ):x(xx){} }; void X::iterate() { for( int i = 0; i < 7; ++i ) { sleep(1); } cout<<"rdy"<<endl; } int main ( int argc, char* argv[] ) { { ObjTimerWrapper<X> myobj( new X(0), 1 ); myobj.Lock(); myobj->iterate(); myobj.UnLock(); } cout<<"end program"<<endl; }
Well, what happenes is that the timer thread that runs
void *ObjTimerWrapper<T>::startTimer(void* arg)
issues sleeps for X secs and then destroys the contained object
The destructor of ObjTimerWrapper joines with the timer thread, so the object can not be destroyed before the seconds pass ( say if ObjTimerWrapper gets out of scope it will wait in it's destructor till the timer joins )
The real problem is sycronization.
What happens if you are currently working on the contained object and the timer thread tries to destroy it? the thing is i provided a mutex locking mechanism, so that the timer thread deletes the contained object only if the mutex is unlocked. But this leaves responsablility to the client code to lock and unlock the mutex each time you call a method on the contained object ( ie , in the example,
myobj.Lock();
myobj->iterate();
myobj.UnLock(); )
•
•
Join Date: Mar 2008
Posts: 173
Reputation:
Solved Threads: 1
You could use the Sleep() function that will wait ex: seconds
before continue the code.
before continue the code.
C++ Syntax (Toggle Plain Text)
Sleep(5000); //Wait 5 seconds /5000 milliseconds //Destroy/Delete object
That's just the point that an object per se in C++ is a passive entity. It comes into being, lives and dies with the aid of a thread (or process - an active entity) only. Of course. it's possible to set a death timer then... what? It's not an object catches a timer interrupt: it's a thread!
As soon as we understand a passive nature of an object, we can fit a key to the designated problem. Possible solutions:
1. Let the guardian thread maintains a pool of mapping objects (creates, provides access to and kills them). May be, it's the only (main) thread of the application. Remember asynchronous i/o standard logic.
2. Let every such object lives itself with its own thread (process). Remember http requests and (most of) web-server standard logics.
As kux mentioned in both (and others) cases you need (more or less) well-designed access synchronization logics. That's why I said about application architecture issues.
Apropos, Sleep function does not bear a relation to the problem: it suspends the active thread, DNS server (or what else) does nothing. As usually, classic army logics does not work in this case (if you don't know what to do now, go to bed
)...
As soon as we understand a passive nature of an object, we can fit a key to the designated problem. Possible solutions:
1. Let the guardian thread maintains a pool of mapping objects (creates, provides access to and kills them). May be, it's the only (main) thread of the application. Remember asynchronous i/o standard logic.
2. Let every such object lives itself with its own thread (process). Remember http requests and (most of) web-server standard logics.
As kux mentioned in both (and others) cases you need (more or less) well-designed access synchronization logics. That's why I said about application architecture issues.
Apropos, Sleep function does not bear a relation to the problem: it suspends the active thread, DNS server (or what else) does nothing. As usually, classic army logics does not work in this case (if you don't know what to do now, go to bed
)... Last edited by ArkM; Nov 14th, 2008 at 10:45 am.
•
•
Join Date: Jan 2008
Posts: 119
Reputation:
Solved Threads: 10
•
•
•
•
That's just the point that an object per se in C++ is a passive entity. It comes into being, lives and dies with the aid of a thread (or process - an active entity) only. Of course. it's possible to set a death timer then... what? It's not an object catches a timer interrupt: it's a thread!
As soon as we understand a passive nature of an object, we can fit a key to the designated problem. Possible solutions:
1. Let the guardian thread maintains a pool of mapping objects (creates, provides access to and kills them). May be, it's the only (main) thread of the application. Remember asynchronous i/o standard logic.
2. Let every such object lives itself with its own thread (process). Remember http requests and (most of) web-server standard logics.
As kux mentioned in both (and others) cases you need (more or less) well-designed access synchronization logics. That's why I said about application architecture issues.
Apropos, Sleep function does not bear a relation to the problem: it suspends the active thread, DNS server (or what else) does nothing. As usually, classic army logics does not work in this case (if you don't know what to do now, go to bed)...
I posted before a method of providing a wrapper class that deletes an object after X seconds. It worked ok, but it forced the client to write code that explicitly locked and unlocked the mutex before and after each method call so that the object won't get destroyed while a method is called.
Now i found an article from Stroustrup that explains how to write a wraper class that wraps methos calls . This is ok, as with this you can do the lock and unlock inside the wrapper.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <cstdio> #include <stdlib.h> #include <pthread.h> #include <unistd.h> using namespace std; //forward template < class T > class ObjTimerWrapper; class Exception {}; template < class T > class Call_proxy { T* obj_; ObjTimerWrapper<T> *twrap_; public: Call_proxy( T* obj, ObjTimerWrapper<T> *twrap ): obj_(obj), twrap_(twrap) {} ~Call_proxy() { twrap_->UnLock(); } T* operator->(){ return obj_; } }; template < class T> class ObjTimerWrapper { private: T* obj_; int seconds_; pthread_mutex_t mutex; pthread_t timer; void DeleteObj() { pthread_mutex_lock( &mutex ); delete obj_; obj_ = 0; pthread_mutex_unlock( &mutex ); } void Lock() { pthread_mutex_lock( &mutex ); } void UnLock() { pthread_mutex_unlock( &mutex ); } friend class Call_proxy<T>; public: ObjTimerWrapper( T* obj, unsigned int seconds):obj_(obj), seconds_(seconds) { pthread_mutex_init( &mutex, 0 ); pthread_create(&timer, NULL, ObjTimerWrapper::startTimer, (void*)this ); } ~ObjTimerWrapper() { pthread_join( timer, 0 ); pthread_mutex_destroy(&mutex); cout<<"destructor called"<<endl; } Call_proxy<T> operator->() { if ( ! obj_ ) throw Exception(); Lock(); return Call_proxy<T>( obj_, this ); } T* get() { if ( ! obj_ ) throw Exception(); return obj_; } static void* startTimer(void * arg); }; template < class T> void *ObjTimerWrapper<T>::startTimer(void* arg) { ObjTimerWrapper* p = (ObjTimerWrapper*)arg; sleep( p->seconds_); p->DeleteObj(); cout<<"timer expired, object destroyed-----------"<<endl; return 0; } class X { public: double x; void iterate(); X( float xx ):x(xx){} }; void X::iterate() { for( int i = 0; i < 1; ++i ) { sleep(5); } cout<<"rdy"<<endl; } int main ( int argc, char* argv[] ) { { ObjTimerWrapper<X> myobj( new X(0), 3 ); //myobj.Lock(); //this no longer required myobj->iterate(); //myobj.UnLock(); //this no longer required ObjTimerWrapper<X> mysecobj( new X(0), 9 ); mysecobj->iterate(); } }
![]() |
Similar Threads
- Explorer shuts down by itself, reappears a few seconds later (Viruses, Spyware and other Nasties)
- modify script (Perl)
- Trojan.Vundo.B (Viruses, Spyware and other Nasties)
- I'm new...HIJACKED...hijackthis log within (Viruses, Spyware and other Nasties)
- I've got Trojan.Holax... is this bad? (Viruses, Spyware and other Nasties)
- not-a-virusadware (Viruses, Spyware and other Nasties)
- Help! - Hijack log but i need to know what to delete (Viruses, Spyware and other Nasties)
- All Kinds of Adware and Spyware, Please Help (Viruses, Spyware and other Nasties)
- Yet another IE home page hijacking (Viruses, Spyware and other Nasties)
Other Threads in the C++ Forum
- Previous Thread: Card game war; completely lost
- Next Thread: Double Linked List I/O Undeclared???
Views: 1338 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph homeworkhelp iamthwee ifstream input int java lib library lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting spoonfeeding string strings struct temperature template templates text tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






