 |
|
 |
Thank you for your great article.
Can I use this code in my commercial project?
Let me know if I have your permission.
Thank you in advance.
Hojin, Lee
Eoneo Inc.
http://www.eoneo.co.kr
leigh@eoneo.co.kr
|
|
|
|
 |
|
 |
Yes, you can freely use this code for your commercial project. There is no license or liability.
|
|
|
|
 |
|
|
 |
|
 |
how to draw graphics using borland turboc++........how to move cursor in output window
jenn
|
|
|
|
 |
|
 |
First of all, thank you for writing this very nice article.
I just noticed that the DeletePool method does not delete the objects created in the objectHolder lists(free list and reserved list). Correct me if I am wrong.
Cheers,
|
|
|
|
 |
|
 |
Valgrind memcheck confirms my doubts.
It can be simply fixed by adding this line:
delete m_pObj;
into the ObjectHolder::~ObjectHolder(){
if(m_pObj){
m_pObj->Release();
//here
delete m_pObj;
}
m_pObj=NULL;
}
Cheers
|
|
|
|
 |
|
 |
Nice article, though I have one query regarding implementation of everything inside .h files ?.
why can't we have code separation in cpp and .h files.
-- modified at 5:34 Wednesday 14th March, 2007
|
|
|
|
 |
|
 |
Thanks for your complement.
It can be done. This is just a sample code. Also with .h files, it's easier to integrate aprticular with template base code.
|
|
|
|
 |
|
 |
Excatly what I was searching for
|
|
|
|
 |
|
 |
Firstly, thanks.. This was just what i was looking for. In using this framework i have come across two small bugs.
firstly if the pool size is 1 then the function ProcessExpiredObjects() crashes when searching for expired objects.. This is because the erase method returns the iterator to the next object, and then the for loop also increments which moves the iterator on again (in this case past the end!). The new function looks like this:
void ProcessExpiredObjects()
{
ObjList::iterator it = m_oReserved.begin();
while( it != m_oReserved.end() )
{
ObjHolder &oHolder = *it;
if((long)time(NULL) - oHolder.GetTimeStamp() > m_nExpirationTime)
{
// this obj has exceeded the wait time
T *pObj = oHolder.GetObj();
if(ValidateObject(pObj) == true)
{
m_oFree.push_back(oHolder);
oHolder.SetObject(NULL);
std::cout << "Found expired Object. " << std::endl;
}
it = m_oReserved.erase(it);
}
else
++it;
}
}
secondly in the Checkout() function after ProcessExpiredObjects() is called then FindObject() gets called which returns an object you still return NULL instead of the object ref. i have just added the following lines after FindObject()
pObj = FindObject();
if (pObj != NULL)
return pObj;
PS... you get my five
ade me;
while(myKitchen.beerInFridge() == true) {
me.watchTV();
me.consumeBeer(myKitchen.getBeerCan());
}
|
|
|
|
 |
|
 |
Inside the Checkout() function, you have the follwing code:
[...]
}else if(m_nExpirationTime != -1){
while(!pObj) {
ProcessExpiredObjects();
pObj = FindObject();
sleep(m_nWaitTime);
}
}
but since the gaurd(m_mData) is locked, other threads cannot do a checkin!
To resolve I think you can unlock before the sleep and lock again after.
---
Count Dew
|
|
|
|
 |
|
 |
Hi, I'm attempting to do this mutex unlock as suggested, but I am unclear as to the proper syntax to unlock (before sleeping) and re-lock afterwards. Fairly new to template programming, so I think my confusion is there, but I have tried several syntaxes without success. Thanks!
|
|
|
|
 |
|
 |
hi
thanks for this excellent article ! it looks very useful and i'm going to use it !(if u don't mind... )
little question: when i call ResetPool() it causes N destructor calls of ObjectHolder which uses Release method of its T pointer data member.
since this pointer allocated by operator new , i think operator delete should be used (or i missed something). how the memory of m_pObj freed ?
i'm a new programmer and i'll glad to recieve some explanations.
Thanks
Sagiv.
|
|
|
|
 |
|
 |
Thanks
|
|
|
|
 |
|
 |
In the class "PoolMgr", you have the function "Init()" defined twice, and in the class "GenericObject", you have the statement, "return true;" appearing outside the function, "MakeUsable".
While your article does have merit, there are some things you "squeaked" by without the benefit of further explanation (e.g. When is the Singleton object, "PoolMgr" going to destroy itself, since this is the surest way to avoid 'resource leaks'? [See Alexandrescu "Modern C++ Design" page 133, fourth paragraph.] Instead, you made your point by showing "manual" deletion of the resources to support resetting of the pool. Even so, you did not use an automatic deletion mechanism to take care of resource deletion. You used "time out" as a sort of signal for deletion, followed by the manual method of doing it.)
There is nothing wrong with how you wrote your article, it's a first step is how I would consider it. An excellent "first step" nonetheless.
I haven't read your follow-up article as yet, but I will, and I shall be looking to see how you handle some of the other points that would describe a more comprehensive manner in dealing overall with Policy-Based Designs.
Good job overall.
William
Fortes in fide et opere!
|
|
|
|
 |
|
 |
>>>>Good observation and thanks for your comments.
In the class "PoolMgr", you have the function "Init()" defined twice, and in the class "GenericObject", you have the statement, "return true;" appearing outside the function, "MakeUsable".
>>>> If you down load the code, it don’t have the Init() function as well as “return true” outside the function. I had also noticed this but before I update the article, it wasv moved out from “unedited Reader Contributions” section. So I could not modify it.
While your article does have merit, there are some things you "squeaked" by without the benefit of further explanation (e.g. When is the Singleton object, "PoolMgr" going to destroy itself, since this is the surest way to avoid 'resource leaks'? [See Alexandrescu "Modern C++ Design" page 133, fourth paragraph.] Instead, you made your point by showing "manual" deletion of the resources to support resetting of the pool. Even so, you did not use an automatice deletion mechanism to take care of resource deletion. You used "time out" as a sort of signal for deletion, followed by the manual method of doing it.)
>>>> I agree with your point. But when I wrote the article, I concentrated more on Pool functionality in generic way rather than singleton pattern. The main aim of this article is to show the reusable pool design with the power of templates.
There is nothing wrong with how you wrote your article, it's a first step is how I would consider it. An excellent "first step" nonetheless.
I haven't read your follow-up article as yet, but I will, and I shall be looking to see how you handle some of the other points that would describe a more comprehensive manner in dealing overall with Policy-Based Designs.
>>>> Here is the link for my new article :http://www.codeproject.com/useritems/Generic_Pool_Design.asp
Here, again I have concentrated more on policy based design to achieve different requirements about the pool rather than singleton pattern. I have used Loki::SingletonHolder<> to create singleton instance of the class instead of reinventing the wheel.
Good job overall.
|
|
|
|
 |
|
 |
if you could come up with a VC 6 version for those members who are still using VC 6.
Because I am one of them who still uses that version, I only have your code from which to follow what your wonderful sample achieves, without the added benefit of seeing it work (which limits my ability to conduct any kind of experimenting with it).
Nevertheless, it's a VERY GOOD!! article and not only am I voting a '5' for it, but am looking forward to the next one you propose writing.
Excellent piece of work!!
William
Fortes in fide et opere!
|
|
|
|
 |
|
 |
If you create new project in VC6 and add all these header and CPP files, it should compile and work fine because I developed this application on Linux with GCC. Only you might need to look at the Sleep() function if it is different in VC6 verse VC7. I don't see any reason if microsoft has changed that. Look for below code in PoolMgr.h
#ifdef WIN32 // WINDOWS
#include
// Here Windows uses Sleep() verse linux uses sleep(). More over, Windows Sleep() takes parameter in Millisec while Linux sleep() takes into secs.
#define sleep(x) Sleep(x * 1000)
#else // UNIX
#include
#endif
Let me know if don't work. I will also try to verify this. Thx.
|
|
|
|
 |
|
 |
I have submitted my article "Generic Pool :policy based design"
http://www.codeproject.com/useritems/Generic_Pool_Design.asp
I am sorry, I could not provide the demo version for VC6 as I don't have IDE avaiable.
|
|
|
|
 |
|
 |
This is really nice article and handy code for pool implementation. It would have been nice if you have provided multithreading support.
|
|
|
|
 |
|
 |
I have plan to provide support for Multithreading as well as sweeper thread which will cleanup the expired resources. In my next article, Generic Pool using Policy based design, I will have this support. thanks for your comment.
|
|
|
|
 |
|
 |
There is no need to write a 'sweep up' implementation to tidy up based on some regular time or indeed by running the PoolMgr in a separate thread; just tidy up each time a request is made for a pool entry. Of course, if you are worried about freeing resources used by a pool entry then a sweeper would be useful, but would require a link between the PoolMgr and the stored objects. If this kind of integration is available then you could also provide notification to the pooled objects of when they have been locked/released/forcibly released.
|
|
|
|
 |
|
 |
I have added thread syncronization support.
|
|
|
|
 |
|
 |
Hi,
This could have been a very interesting article, but now it's just a zip file with code... It would have been nice if you would have described the design tradeoffs you made and the interface of the Pool template, along with a simple example maybe. Eg is it possible to change the pool size at runtime? If no, why did do choose this design? Etc...
|
|
|
|
 |
|
 |
I have updated this article with snippet of the code and a sample usage. It has a function call Init(..) to Initialize the Pool which can be modified to support the changing the size of the pool at runtime.
|
|
|
|
 |