Upcoming Events
Unite 2010
11/10 - 11/12 @ Montréal, Canada

GDC China
12/5 - 12/7 @ Shanghai, China

Asia Game Show 2010
12/24 - 12/27  

GDC 2011
2/28 - 3/4 @ San Francisco, CA

More events...
Quick Stats
88 people currently visiting GDNet.
2406 articles in the reference section.

Help us fight cancer!
Join SETI Team GDNet!
Link to us Events 4 Gamers
Intel sponsors gamedev.net search:

  Contents

 Reference Counting
 Smart Pointers
 Reference counting
 and STL

 Conclusion

 Get the Source
 Printable version

 


Multithreaded Operation

In multithreaded programs, it is necessary to synchronize shared objects, to avoid inconsistencies due to scheduling in the middle of an (in/de)crement. This can be done by adding a mutex to each object, and locking it before doing the actual change of counter.

Implementation of this is OS specific.

Overhead

There are 2 kinds of overhead when using this scheme:

  1. Space, requiring 1 int per object.
  2. Time, the (in/de)crement of the counter whenever a pointer changes.

Usually, when correctness is required, this is a small price to pay.

Pros-Cons

Drawbacks:

This scheme cannot be used in data structures where pointer circles are formed (e.g. doubly linked lists). For tree like structures (trees, vectors, singly linked lists) it can be safely used. Most game data is stored in tree-like structures so it is useful for games.

Advantages:

  • A simple system, can be easily integrated into existing software
  • Time distributed garbage collection, with very low latency, as it is done when you would normally delete objects.
  • Does not require a special allocator.
  • Frees you from the worry of calling delete or managing the counter manually.