Request a resource ( __constant__ memory buffer) from a fixed pool of resources

[http://stackoverflow.com/questions/39284805/request-a-resource-from-a-fixed-pool-of-resources-in-c-11 => Cross-posted from stackoverflow as I didn’t get a proper answer there. The resources I mean are a pool of constant memory buffers of the same size - e.g. a fixed pool of 4 buffers, each holding 25 elements).

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 ?

use atomic operations on the array of integers

@Bulat: I suppose I will need atomic operations in some way. The difficult thing (for me) is that on request, I don’t want to lock a specific resource (with a specified id), instead I want the request fn. to search for any free resource and lock it and return its id. And I have no idea how to do that.

for (i=0..N)
  if element #i is free - mark it as used and return i

OK - seems that at times I think to complicated …
So this is sort of a spin-wait which will tax one CPU core fully (if no resource is free). Maybe at the end of each iteration I should insert a ‘std::this_thread::yield’.

i thought you ask for a CUDA code

on CPU, you have power of STL at least. So, you have so much more ways to do it.

you can use mutexes+locks instead of atomics. Also you can protect with lock/atomic entire datastructure instead of each element. You can hold IDs of free resources in array, deque, list, tree

Instead of yield it’s more efficient to use condtion vars since they wakes thread only when condition mets: http://en.cppreference.com/w/cpp/thread/condition_variable - look at example, it shows how to correctly implement consumer (Allocation) and producer (deallocation)

You may get more professional help on SO by asking how to manage pool of N resources, it has nothing CUDA-specific