English 中文(简体)
为什么睡觉使我的穆特克失去作用
原标题:Why does Sleep function disable my Mutex

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 . ).

该守则如下:

#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<<"
SleepVal: "<<*sleepVal<<" at: "<<currentChar;
        cout<<currentChar;
    }//end for
}//end threadProc

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

    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;

}

因此,我的问题是:为什么不把mut子法 void倒。

最佳回答

请注意,您的打印说明不受中文本的保护,因此,每读一个读本可以免费印刷,而另一个读本可以自由修改。 不要睡觉,你就重新允许订票人根据透镜数量确定印刷订单。

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.

问题回答

此外,作为今后使用相互排斥的警示,在Windows,最佳用户modemutex是SRWLock,其次是关键科。 使用一个以操作为本的nch子的速度要慢得多。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签