Here is a code below. The code is not complete, I omitted releasing resources part and implementation of QueryRes logic.
#define N 5
/*simply resources manager which has N shared resources*/
class ResourceManager
{
public:
ResourceManager()
{
for (int i = 0; i < N; ++i)
resources[i] = CreateMutex(NULL, FALSE, NULL);
}
/*CreateMutex for on resources array in ctor*/
/*CloseHandle() in dtor and ReleaseMutex in another function which is called after QueryRes*/
void QueryRes(int i)
{
WaitForSingleObject(resources[i], INFINITE); //(*) Here is the problem
}
private:
HANDLE resources[N];
};
/*User who asks for resource time-to-time*/
class User
{
public:
User(ResourceManager& res_holder_, int res_num) : resource_holder(resource_holder), resource_to_query(res_num) {}
void WorkWithResource()
{
while(1)
{
resource_holder.QueryRes(resource_to_query);
}
}
static void Run (void* params)
{
static_cast<User*>(params)->WorkWithResource();
}
private:
ResourceManager& resource_holder;
int resource_to_query;
};
int main()
{
ResourceManager resource_manager;
User* users[5];
HANDLE threads[5];
for (size_t i = 0 ; i < 5; ++i)
{
users[i] = new User(resource_manager, i % 5);
threads[i] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)&User::Run, users[i], 0, NULL);
}
WaitForMultipleObjects(5, threads, true, INFINITE);
return 0;
}
At (*) place I get an “access violation exception” when the function does WaitForSingleObject on already locked mutex.
I also tried
while(WaitForSingleObject(resources[i], INFINITE) != WAIT_OBJECT_0)
and got the same result.
Why do I get the exception?
I tried vc 2003, 2008 and 2010. I can’t use boost/pthreads/etc.
Thank you.
The error lies in the constructor of
You should have
instead !