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 7809993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T03:37:04+00:00 2026-06-02T03:37:04+00:00

I found code online that displays how to use threads from a tutorial by

  • 0

I found code online that displays how to use threads from a tutorial by redKyle. In the ‘Race Condition’ tutorial, he basically shows how two threads are sent to a function. The objective of the function is to print ‘.’ and ‘#’ in sequence one hundred times each. He provides the code to get this to work, he does NOT provide the code for the mutex. I have modified the code to include the mutex so that to prevent one thread from accessing the variable that holds the last character printed while another thread is accessing it.
I got the code to work. Great! However, I kept changing the sleep value between 1 and 50. The mutex code works fine. However, when i set sleep to 0 (or just comment it out) the mutex no longer works and the values are no longer printed in the correct manner (I no longer see 200 characters of strictly alternating ‘#’ and ‘.’).

The following is the code:

#include "stdafx.h"

#include <iostream>
#include <windows.h>
using namespace std;

static char lastChar='#';

//define a mutex
HANDLE mutexHandle = NULL;

//flag to specify if thread has begun
bool threadStarted = false;

void threadProc(int *sleepVal, int *threadID)
{

    cout<<"sleepVal: "<<*sleepVal<<endl;

    for (int i=0; i<100; i++)
    {

        char currentChar;

        threadStarted = true;

        while(!threadStarted){}

        //lock mutex
        WaitForSingleObject(mutexHandle, INFINITE);

        if (lastChar == '#')
            currentChar = '.';
        else
            currentChar = '#';

        Sleep(*sleepVal);

        lastChar = currentChar;

        ReleaseMutex(mutexHandle);

        threadStarted = false;


        //      cout<<"\nSleepVal: "<<*sleepVal<<" at: "<<currentChar;
        cout<<currentChar;
    }//end for
}//end threadProc

int main()
{
    cout<<"Race conditions by redKlyde \n";

    int sleepVal1 = 50;
    int sleepVal2 = 30;

    //create mutex
    mutexHandle = CreateMutex(NULL, false, NULL);

    //create thread1
    HANDLE threadHandle;
    threadHandle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) threadProc, &sleepVal1, 0, NULL);

    //create thread2
    HANDLE threadHandle2;
    threadHandle2 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) threadProc, &sleepVal2, 0, NULL);

    WaitForSingleObject(threadHandle, INFINITE);
    WaitForSingleObject(threadHandle2, INFINITE);

    cout<<endl<<endl;

    CloseHandle(mutexHandle);

    system("pause");

    return 0;

}

So my question is: why does setting sleep to 0 void the mutex code.

  • 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-06-02T03:37:06+00:00Added an answer on June 2, 2026 at 3:37 am

    Take notice that your print statement is not protected by the mutex, so one thread is free to print while the other is free to modify. By not sleeping, you’re allowing the scheduler to determine the print order based upon the quantum of the thread.

    There are some things wrong:

    1) You should not be sleeping inside a held lock. This is almost never correct.
    2) Any place your data is shared, you should be guarding with a lock. This means that the print statement should be in the lock, too.

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

Sidebar

Related Questions

I found this code online and am surprised that it basically does the opposite
A coworker recently showed me some code that he found online. It appears to
Im trying to adapt some code that I have found online that uses this
I found this code online that has a procedure inside a procedure. I cannot
I've found some C# code online to impersonating a user and it appears to
I found some code in a project which looks like that : int main(int
I found the code from the net in which i cant understand this line:-
I want to make a datagridview from sqlite db i found below code in
CRM 2011 online. I have a requirement to color code entries that appear in
I'm working on a simple WP7 app that displays BART arrivals from their real

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.