jeudi 1 septembre 2016

Request a resource from a fixed pool of resources in C++ 11

I have a fixed-size pool of N global resources, where the i-th resource has the number 'i' which serves also as the id of that resource (so the resource ids go from 0 to N - 1). Each resource has the same type.

I would need a function 'int requestResourceId()' which searches for a free resource (a resource which is not locked currently), and locks it (and returns its id). If all resources are locked currently, then the fn. shall wait until a free resource becomes available.

And I would need a function 'void releaseResourceId(int id)' which unlocks the resource with id 'id'.

Both functions have to be thread-safe of course.

How to implement that in C++ 11 in a portable way, without using 3rdparty libraries like boost ?

Aucun commentaire:

Enregistrer un commentaire