BDE 4.14.0 Production release
|
Functions | |
bdlcc::ObjectCatalogIter< TYPE >::operator const void * () const | |
Return non-zero if the iterator is valid, and 0 otherwise. | |
Provide an efficient indexed, thread-safe object container.
bdlcc::ObjectCatalog
This component provides a thread-safe and efficient templatized catalog of objects. A bdlcc::ObjectCatalog
supports efficient insertion of objects through the add
method, which returns a handle that can be used for further reference to the newly added element. An element can be accessed by providing its handle to the find
function. Thread-safe design implies that the element is returned by value into an object buffer rather than by reference (see this package documentation for a discussion of thread-safe container design). Likewise, an element can be modified by providing its handle and a new value to the replace
method. Finally, an element can be removed by passing its handle to the remove
method; the handle is then no longer valid and subsequent calls to find
or remove
with this handle will return 0.
bdlcc::ObjectCatalogIter
provides thread safe iteration through all the objects of an object catalog of parameterized TYPE
. The order of the iteration is implementation defined. Thread safe iteration is provided by (read)locking the object catalog during the iterator's construction and unlocking it at the iterator's destruction. This guarantees that during the life time of an iterator, the object catalog can't be modified (however multiple threads can still concurrently read the object catalog).
Note that an object catalog has a maximum capacity of 2^23 items.
This section illustrates intended use of this component.
Consider a client sending queries to a server asynchronously. When the response to a query arrives, the client needs to invoke the callback associated with that query. For good performance, the callback should be invoked as quickly as possible. One way to achieve this is as follows. The client creates a catalog for the functors associated with queries. It sends to the server the handle (obtained by passing the callback functor associated with the query to the add
method of catalog), along with the query. The server does not interpret this handle in any way and sends it back to the client along with the computed query result. The client, upon receiving the response, gets the functor (associated with the query) back by passing the handle (contained in the response message) to the find
method of catalog.
Assume the following declarations (we leave the implementations as undefined, as the definitions are largely irrelevant to this example):
Furthermore, let also the following variables be declared:
Now we define functions that will be used in the thread entry functions:
In some thread, the client executes the following code.
In some other thread, the client executes the following code.
The following code fragment shows how to use bdlcc::ObjectCatalogIter to iterate through all the objects of catalog
(a catalog of objects of type MyType
).
Now iterate through the catalog
:
Note that the associated catalog is (read)locked when the iterator is constructed and is unlocked only when the iterator is destroyed. This means that until the iterator is destroyed, all the threads trying to modify the catalog will remain blocked (even though multiple threads can concurrently read the object catalog). So clients must make sure to destroy their iterators after they are done using them. One easy way is to use the for (bdlcc::ObjectCatalogIter<MyType> it(catalog); ...
as above.
|
inline |