Often I use pthread's mutex only to protect a section, for instance an asynchronous timed draw function which pick the objects to draw from a list where objects can be deleted.
Is there any alternative? (libc's atomic types?)
edit
A more simple example, no lists.
I've a 3D model viewer using OpenGL, the draw function is called by a timer. The user can select another file to view with keyboard or mouse, but destroying the data of the current model from memory while drawing will obviously cause a segmentation.
Currently I'm using pthread_mutex_trylock
in the draw function and pthread_mutex_lock
where the object is destroyed and the new is loaded.
The only lock-free alternative I see would be to load the new object in memory before destroying the old, but IMHO that would add some overhead, I just need some lock
unlock
primitive (eventually a trylock
).