Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > BOOK: Professional C++
|
BOOK: Professional C++
This is the forum to discuss the Wrox book Professional C++ by Nicholas A. Solter, Scott J. Kleper; ISBN: 9780764574849
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional C++ section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 5th, 2008, 10:31 AM
Authorized User
 
Join Date: Mar 2008
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default problems with object pool in ch17

I print the words following the book,it compiled OK,linked OK,but it doesn't work as i expected.in class UserRequest,there is a static var named data,i use ++data in ctor to mark different objects.then in while loop using cout to show the different objects ,but they all print the same object.i was confused where were my objects...
code followed:
//ObjectPool.h
#include <queue>
#include <vector>
#include <stdexcept>
#include <memory>

using std::queue;
using std::vector;

template <typename T>
class ObjectPool
{
public:
    ObjectPool(int chunkSize)
        throw(std::invalid_argument,std::bad_alloc);
    ~ObjectPool();
    T& acquireObject();
    void releaseObject(T& obj);

    queue<T*> mFreeList;
    vector<T*> mAllObjects;
    int mChunkSize;
protected:
    static const int kDefaultChunkSize = 10;
    void allocateChunk();
    static void arrayDeleteObject(T* obj);

private:
    ObjectPool(const ObjectPool<T>& obj);
    ObjectPool<T> operator=(const ObjectPool<T>& ths);
};

#include "ObjectPools.h"

//ObjectPools.h function definition
#include "stdafx.h"
#include <iostream>
using namespace std;
template <typename T>
ObjectPool<T>::ObjectPool(int chunkSize) throw(std::invalid_argument,std::bad_alloc) : mChunkSize(chunkSize)
{
    if (mChunkSize <= 0) {
        throw std::invalid_argument("chunk size must be opsitive");
    }
    allocateChunk();
}

template <typename T>
void ObjectPool<T>::allocateChunk()
{
    T* newObjects = new T[mChunkSize];
    mAllObjects.push_back(newObjects);
    for (int i = 0; i < mChunkSize; i++) {
        mFreeList.push(&newObjects[i]);
    }
}

template <typename T>
void ObjectPool<T>::arrayDeleteObject(T *obj)
{
    delete [] obj;
}

template <typename T>
ObjectPool<T>::~ObjectPool()
{
    for_each(mAllObjects.begin(),mAllObjects.end(),arr ayDeleteObject);
}

template <typename T>
T& ObjectPool<T>::acquireObject()
{
    if (mFreeList.empty()) {
        allocateChunk();
    }
    T* obj = mFreeList.front();

    mFreeList.pop();
// cout << "return:" << obj << endl;
    return *obj;
}

template <typename T>
void ObjectPool<T>::releaseObject(T& obj)
{
    mFreeList.push(&obj);
}

//UserRequest.h
class UserRequest
{
public:
[u] UserRequest() { ++data; }</u>
    ~UserRequest() {}
[u] int get() { return data; }</u>

protected:
[u] static int data;</u>
};
int UserRequest::data = 0;
//main cpp
// ObjectPool.cpp : ¶¨Òå¿ØÖÆÌ¨Ó¦ÓóÌÐòµÄÈë¿Úµ ã¡£
//

#include "stdafx.h"
#include "ObjectPool.h"
#include "UserRequest.h"
#include <vector>
#include <iostream>
#include <windows.h>
using namespace std;

UserRequest& obtainUserRequest(ObjectPool<UserRequest>& pool)
{
    UserRequest& request = pool.acquireObject();
[u] cout << "required data:" << request.get() << endl;</u>
    return request;
}

void processUserRequest(ObjectPool<UserRequest>& pool, UserRequest& req)
{
// pool.releaseObject(req);
[u] cout << "data " << req.get() << " released" << endl;</u>
}

int _tmain(int argc, _TCHAR* argv[])
{
    ObjectPool<UserRequest> requestPool(100);
    /*
    while (true) {
[u] Sleep(100);</u> UserRequest& req = obtainUserRequest(requestPool);
[u] cout << "UserRequest:" << req.get() << endl;
// cout << "ojbpool:" << requestPool.mFreeList.back()->get() << endl;</u> cout << "mChunkSize:" << requestPool.mChunkSize << endl;
        processUserRequest(requestPool, req);
    }

[u] vector<UserRequest*>::iterator it= requestPool.mAllObjects.begin();
    for (size_t i = 0; i < requestPool.mAllObjects.size(); i++) {
        cout << (*it)->get() << endl;
        if (it != requestPool.mAllObjects.end()) {
            ++it;
        } else {
            cout << "it is the end of vector!" << endl;
        }
    }*/
    cout << (*requestPool.mAllObjects.begin())->get() << endl;
    cout << (*(requestPool.mAllObjects.end()-1))->get() << endl;
    cout << "queue size :" << requestPool.mFreeList.front()->get() << endl;
    cout << "queue size :" << (requestPool.mFreeList.front()+1000)->get() << endl;</u>
//forward was when debugging
    return 0;
}

waiting for your proffessional explaination and thanks.


fight now,date in future.
__________________
fight now,date in future.





Similar Threads
Thread Thread Starter Forum Replies Last Post
where is my topic about object pool Aiming BOOK: Professional C++ 2 September 7th, 2008 06:39 AM
Problems with copyRecord method of Record object Syster Tara Classic ASP Basics 7 September 19th, 2006 10:55 PM
ch17 php4 hymns BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 0 August 21st, 2006 04:25 AM
Ch17-entrytolog security problem gcook BOOK: Beginning ASP.NET 1.0 4 August 2nd, 2003 09:42 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.