Consider using "The scoped locking idiom". It is described (among many other places) at
http://www.cs.wustl.edu/~schmidt/PDF/ScopedLocking.pdf[
^]
In general, you need:
1. A class that encapsulates your shared resource.
2. All public functions in the class acquire a lock at entry, and release it at exit, possibly using the aforementioned idiom.
In Windows, the API exposes some functions to avoid the overhead for simple cases: for instance, OverlockedIncrement and OverlockedDecrement for integers.
If it's important for you, google also "lock free threading patterns".
Hope this helps,
Pablo.