bug-commoncpp
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: HowTo for queues


From: Leon Pollak
Subject: Re: HowTo for queues
Date: Sat, 5 Jun 2010 22:51:32 +0300
User-agent: KMail/1.13.3 (Linux/2.6.33.4-95.fc13.x86_64; KDE/4.4.3; x86_64; ; )

On Saturday June 5 2010, David Sugar wrote:
> First, the queue core class can use a reusable pool, but this is for
> internal management of it's own pointers.  The queue is passed an object
> to post into it that is derived from "Object", which are reference
> counted.  Hence, existing on the queue is actually a reference count
> instance of a reference counted object.  However, if you want a resuable
> pool of buffer objects, you would construct an object for your buffer
> that is itself derived from ReusableObject (which is derived from
> Object), and this is what you would then pass into the queue.
> 
> Hence maybe you would use something like:
> 
> class MyObject : public ReusableObject
> {
> public:
>       char data[100];
> };
> 
> static mempager datapool;
> 
> // we have a pool with a maximum allocation size of 100 objects...
> static paged_reuse<MyObject> bufpool(&datapool, 100);
> 
> // and a fixed size stream buffer of referenced objects...
> static queueof<MyObject> mycache(&datapool, 10);
> 
> // we have a cache for these 100 objects...
> 
> to post:
> 
>       // timeout to try
>       MyObject *x = bufpool.get(100);
> 
>       // on error getting a new pool object...
>       if(!x) ... we could allocate from datapool, throw, whatever...
> 
>       // we may also need to add x->retain(), it's been awhile....
> 
>       // make sure it survives cache...
>       String::set(x->data, 100, "something");
> 
>       // timeout possible...
>       if(!mycache.post(x, 100)) ... failed to post ...
> 
> to get an object from the queue:
> 
>       // with a timeout...
>       MyObject *x = mycache.lifo(100);
> 
>       if(!x) ... we didn't get anything before timer expired...
> 
>       printf("data %s\n", x->data);
> 
>       // release object back to reuse pool...
>       bufpool::release(x)
Hello David,

And again thanks for the example.

It seems you were right - no use of the queue/queueof was not tried yet...:-)

1. I tried to compile the example you provided above. There is a problem with 
the second template parameter of queueof - it cannot be null-pointer according 
to my search in the internet. See for example 
http://stackoverflow.com/questions/1418019/casting-pointer-as-template-
argument-comeau-msvc-compile-gcc-fails

2. You declared all three objects as 'static', which, besides the problems 
with compilation also makes them invisible from the outside, no? This forces 
me to put both threads in the same file, which is not good for me.
What was your reason to declare them static?

Thanks.
-- 
Leon



reply via email to

[Prev in Thread] Current Thread [Next in Thread]