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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:08:40+00:00 2026-06-12T08:08:40+00:00

Thread 1: putQ setFlag Thread 2: while (1) { waitFlag processQ clearFlag } This

  • 0

Thread 1:

putQ
setFlag

Thread 2:

while (1) {
waitFlag
processQ
clearFlag
}

This is an interview question. I am not sure whether there is a while loop for Thread 1.
But the answer is like when Thread 2 re-enter the while loop, two threads go into deadlock.

Can anyone tell me what’s condition for deadlock? Thanks.

  • 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-12T08:08:41+00:00Added an answer on June 12, 2026 at 8:08 am

    I think the OP’s comments are correct:

    1. @Gray: This is most likely NOT a dead-lock, but a race condition. Yet the application is stuck not processing the queue, so it’s a lock.

    2. @Wug: It is probably the “trivial case” where while thread 1 has put in the queue and updated the flag, thread 2 will never notice. See course of action below.

    Potential lock situation:

    • T2: ProcessQ Element 0 (processing a former element)
    • T1: PutQ Element 1
    • T1: SetFlag
    • T2: ClearFlag
    • T2: waitFlag

    Element 1 might never be processed.

    EDIT:

    Of course, the next question would be: how do you fix that?

    The answer would be with a Conditional Variable (see pthread_cond_init and others for details):

    Thread 1:

    PutQ
    LockMutex
    SetFlag
    CondSignal
    UnlockMutex
    

    Thread 2:

    while (1) {
        LockMutex
        while (FlagIsUnset)
           CondWait
        FlagUnset
        UnlockMutex
        ProcessQ
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Thread 1: is executing this loop while(running) { // Do Task() } println(Done); Thread
This thread talks about the audio tag on the Android, but I'm not sure
This thread is a little related too: framework or not and Spring or Java
This thread got too confusing (for me) so I am asking the question again.
For thread-safety reasons it is argumented: Do not allow the this reference to escape
This thread inspired the question. Here are the code samples again. I'm looking for
This thread is a continuation of Is there a reason to check for null
Application has an auxiliary thread. This thread is not meant to run all the
Thread A: a UI thread where the Gtkmm's message loop runs. Thread B: receives
Main thread of application is doing some work. There also exists a timer, which

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.