Interview Question

  • -
    0
    of 0 votes
    1
    Answer

    static HANDLE mutex[2];

    static unsigned __stdcall threadFunc1(void *v){
    while(1)
    {
    WaitForSingleObject(mutex[0], INFINITE);
    if (k>=10)
    return 0;
    cout<<"\n\tWELL ThreadID : 1 : ";
    cout<<GetCurrentThreadId()<<" :: "<<k++<<endl;
    cout<<flush;
    ReleaseMutex(mutex[1]);
    }
    return 0;
    };

    static unsigned __stdcall threadFunc2(void *v){
    while(1)
    {
    WaitForSingleObject(mutex[1], INFINITE);
    if (k>=10)
    return 0;
    cout<<"\n\tWELL ThreadID : 2 : ";
    cout<<GetCurrentThreadId()<<" :: "<<k++<<endl;
    cout<<flush;
    ReleaseMutex(mutex[0]);
    }
    return 0;
    };

    int main()
    {
    unsigned int t1;
    HANDLE h[2];
    int *ii=&i;
    mutex[0] = CreateMutex(0, 0, 0);
    mutex[1] = CreateMutex(0, 0, 0);
    h[0] = (HANDLE)_beginthreadex(0,0,&threadFunc1,(void *)ii,0,&t1);
    h[1] = (HANDLE)_beginthreadex(0,0,&threadFunc2,(void *)ii,0,&t1);
    WaitForMultipleObjects(2, h, true, INFINITE);
    CloseHandle(h[0]);
    CloseHandle(h[1]);
    CloseHandle(mutex[0]);
    CloseHandle(mutex[1]);
    cin>>i;
    return 0;
    }


    Output :


    WELL ThreadID : 1 :
    WELL ThreadID : 2 : 47124264 :: :: 01


    WELL ThreadID : 1 :
    WELL ThreadID : 2 : 47124264 :: :: 23


    WELL ThreadID : 1 :
    WELL ThreadID : 2 : 47124264 :: :: 45


    WELL ThreadID : 1 :
    WELL ThreadID : 2 : 47124264 :: :: 67


    WELL ThreadID : 1 :
    WELL ThreadID : 2 : 47124264 :: :: 89



    why is output not syncronized ?

    - gs on November 24, 2012 in United States Report Duplicate | Flag

Country: United States


Comment hidden because of low score. Click to expand.
0
of 0 vote

Your code doing this:


thread1:
mutex0.lock();
working with cout
mutex1.unlock();

thread2:
mutex1.lock();
working with cout
mutex0.unlock();

"cout" is buffered output and not thread safe. So, it's not safe to write from different threads simultaneously to cout.

- m@}{ on November 26, 2012 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book walking you through getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More