Am Thursday 03 September 2009 06:02:05 schrieb Gottlob Frege:
<code>
//initializion Thread (runs before any other thread): shared_ptr< MyClass > global_ptr(createNewObject()) ;
//WriterThread :
while(true) { mutex.lock() global_ptr.reset( createNewObject()) mutex.unlock(); sleep (5) ; }
By the way, to minimize contention, call createNewObject() outside the lock: local = createNewObject(); lock(); global = local; unlock();
//ReaderThreads:
mutex.lock() weak_ptr<MyClass> local_weak_ptr (globally_ptr) ; mutex.lock()
why would you even need a lock here? the shared_ptr doc says that you can expect the same thread safety from shared_ptr as you can from built-in types. you can use multiple-readers-single-writer without any locks on built-in types.