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

  • SEARCH
  • Home
  • 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 6726125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:53:22+00:00 2026-05-26T09:53:22+00:00

I expect to get numbers from 0 to 4 in random order, but instead,

  • 0

I expect to get numbers from 0 to 4 in random order, but instead, I have some unsynchronized mess

What i do wrong?

#include <iostream>
#include <windows.h>
#include <process.h>

using namespace std;

void addQuery(void *v );

HANDLE ghMutex;

int main()
{
    HANDLE hs[5];
    ghMutex = CreateMutex( NULL, FALSE, NULL);         
    for(int i=0; i<5; ++i)
    {
        hs[i] = (HANDLE)_beginthread(addQuery, 0, (void *)&i);
        if (hs[i] == NULL) 
        {
            printf("error\n"); return -1;
        }
    }

    printf("WaitForMultipleObjects return: %d error: %d\n",
         (DWORD)WaitForMultipleObjects(5, hs, TRUE, INFINITE), GetLastError());


    return 0;
}

void addQuery(void *v )
{
    int t = *((int*)v);
    WaitForSingleObject(ghMutex, INFINITE);

    cout << t << endl;

    ReleaseMutex(ghMutex);
    _endthread();
}
  • 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-26T09:53:23+00:00Added an answer on May 26, 2026 at 9:53 am

    You have to read and write the shared variable inside the lock. You are reading it outside of the lock and thus rendering the lock irrelevant.

    But even that’s not enough since your shared variable is a loop variable that you are writing to without protection of the lock. A much better example would run like this:

    #include <iostream>
    #include <windows.h>
    #include <process.h>
    
    using namespace std;
    
    void addQuery(void *v );
    
    HANDLE ghMutex;
    int counter = 0;
    
    int main()
    {
        HANDLE hs[5];
        ghMutex = CreateMutex( NULL, FALSE, NULL);         
        for(int i=0; i<5; ++i)
        {
            hs[i] = (HANDLE)_beginthread(addQuery, 0, NULL);
            if (hs[i] == NULL) 
            {
                printf("error\n"); return -1;
            }
        }
    
        printf("WaitForMultipleObjects return: %d error: %d\n",
             (DWORD)WaitForMultipleObjects(5, hs, TRUE, INFINITE), GetLastError());
    
    
        return 0;
    }
    
    void addQuery(void *v)
    {
        WaitForSingleObject(ghMutex, INFINITE);
    
        cout << counter << endl;
        counter++;
    
        ReleaseMutex(ghMutex);
        _endthread();
    }
    

    If you can, use a critical section rather than a mutex because they are simpler to use and more efficient. But they have the same semantics in that they only protect code inside the locking block.

    Note: Jerry has pointer out some other problems, but I’ve concentrated on the high level trheading and serialization concerns.

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

Sidebar

Related Questions

I am trying to find a way to get a random selection from a
For ScoreOption , I expect to get the following input A, B, and T_(state)
I'm trying to get an expect script to work, and when I use the
Since I am using get I expect to see the submitted values appended to
I don't expect a straightforward silver bullet answer to this, but what are the
Is it alright to expect that the user using the back end will have
This code from my developer below... does it expect something like 1 - January
I'm having some problems with launching threads from a list of functions. They are
I have a simple wrapper for an Mersenne twister random number generator. The purpose
What am I doing wrong here? I am trying to extract from this list

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.