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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:28:15+00:00 2026-05-24T06:28:15+00:00

I have a situation where thread 1 is waiting on a condition variable A,

  • 0

I have a situation where thread 1 is waiting on a condition variable A, which should be woken up by thread 2. Now thread 2 is waiting on a condition variable B , which should be woken up by thread 1. In the scenario I am using the condition variable, I cannot avoid such a deadlock situation. I detect cycle(deadlock) and terminate one of the threads which is a participant in the deadlock.

Now, what I am not sure is how to simply terminate a thread say thread 1 which is waiting on a condition variable.

Would be grateful for some pointers.
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-05-24T06:28:16+00:00Added an answer on May 24, 2026 at 6:28 am

    Condition variables aren’t like mutexes. By that I mean they aren’t only usable by a single thread controlling them. The mutex that protects the condition variable is treated that way but that’s only locked for short periods of time, unlocked manually by a thread after kicking (signalling) the condition variable, and automatically by a thread waiting for such a kick.

    You can have a totally separate thread (like your deadlock detector, let’s call it thread 3) simply kick one of the condition variables and it will wake up the thread waiting for it.

    The usual use case for condition variables is for threads to wait for the kick then check to ensure you have work anyway (don’t assume there is work simply because the variable was kicked). That’s to take care of spurious wake-ups.

    One possibility is to have a “global” deadlock_occurred flag which thread 3 sets when it detects deadlock, then also have thread 3 kick all the condition variables.

    The first thing that threads 1 and 2 should do after being woken, should be to check that flag and take appropriate action (probably exit the thread).

    You’ll find you get into a lot less deadlock-type trouble if you architect your applications so that threads are responsible for their own lifetime. It’s too easy to externally kill threads when they’re not in a state amenable to being terminated. Don’t get me wrong, there are other ways to handle it (such as with cancel points), but my tried and tested solution is by far the easiest I’ve ever found.

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

Sidebar

Related Questions

I have a situation where I would like to have the main thread waiting,
Let's say I have a situation in Silverlight where there is a background thread
I have situation in which I read a record from a database. And if
According to an article on IBM.com, a race condition is a situation in which
I have an interesting situation on my hands. For a few years now we've
I have written an application which is using NHibernate to provide the underlying object
I have a situation where I expect adding to the thread pool to be
Here is the scenario: I have a java main process, which uses JMS to
I really have a strange situation. I'm making a Linux multi-threaded C application using
i have situation like this: class IData { virtual void get() = 0; virtual

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.