Each thread may insert an object to the container (at most once) using ‘insert’ function.
Hereafter, the thread may try to access this object using ‘get’ function.
Therefore, there is no race between ‘insert’ and ‘get’ when used by the same thread.
However, a different thread may try to insert its own object while another thread has called ‘get’
I need a container where this situation does not need any synchronization method.
The number of threads may vary dramatically between executions.
class Object;
class Container<Object>;
Container<Object> g_container;
void insert(int threadId)
{
ScopedLock<Mutex> lock(insertMutex);
Object obj;
g_container[threadId] = obj;
}
Object get(int threadId)
{
return g_container[threadId];
}
You can use a vector of container pointers. Each thread manages their own container instance, and registers it with the array of container pointers.
Now, you can iterate over the
Container<T>::containersto access all containers.