Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6899905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:28:49+00:00 2026-05-27T07:28:49+00:00

Here is a code below. The code is not complete, I omitted releasing resources

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T07:28:50+00:00Added an answer on May 27, 2026 at 7:28 am

    The error lies in the constructor of

    User(ResourceManager& res_holder_, int res_num) : resource_holder(resource_holder), resource_to_query(res_num)    {   }
    

    You should have

    User(ResourceManager& res_holder_, int res_num) : resource_holder(**res_holder_**), resource_to_query(res_num)    {   }
    

    instead !

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I commented my code below to reflect what I am attempting to do here.
Hey all. Here is the scenario. I have the below code, and I'm needing
I'm trying to update an element in the XML document below: Here's the code:
Ok the error is showing up somewhere in this here code if($error==false) { $query
Here is code from MSDN . I don't understand why the work isn't just
Here a code to demonstrate an annoying problem: class A { public: A(): m_b(1),
enter code here I have a table on SQL server 2005 with bigint primary
The code here is X++. I know very little about it, though I am
enter code here Hi All, I have a simple windows service application that connects
Edit: The code here still has some bugs in it, and it could do

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.