0

Is boost::object_pool synchronized?

1
  • 1
    Please explain what you mean by 'synchronised' as this term doesn't have a specific meaning in C++ like it does have in Java, for example. Nov 2, 2009 at 19:59

2 Answers 2

4

C++ doesn't specify anything about thread-safety, so if it isn't mentioned it likely doesn't deal with threading. Sometimes, Boost provides things that can be thread-safe out of the box, this is not one of them.

Wrap access to the pool in a mutex.

0
0

boost::object_pool is not synchronized for concurrent access and freeing of objects to and from pool. but if you want pool with synchronization, singleton_pool from boost is the one. There are few restrictions on how to start using singleton_pool, but they are pretty fair and applicable for all applications. Please see below notes from boot documentation from here and here.

Object Usage vs. Singleton Usage

Object Usage is the method where each Pool is an object that may be created and destroyed. Destroying a Pool implicitly frees all chunks that have been allocated from it.

Singleton Usage is the method where each Pool is an object with static duration; that is, it will not be destroyed until program exit. Pool objects with Singleton Usage may be shared; thus, Singleton Usage implies thread-safety as well. System memory allocated by Pool objects with Singleton Usage may be freed through release_memory or purge_memory.

singleton_pool usages restrictions

The underlying pool p referenced by the static functions in singleton_pool is actually declared in a way that it is:

  • Thread-safe if there is only one thread running before main() begins and after main() ends -- all of the static functions of singleton_pool synchronize their access to p.

  • Guaranteed to be constructed before it is used -- thus, the simple static object in the synopsis above would actually be an incorrect implementation. The actual implementation to guarantee this is considerably more complicated.

Note that a different underlying pool p exists for each different set of template parameters, including implementation-specific ones.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.