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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T11:42:07+00:00 2026-06-16T11:42:07+00:00

If you have a thread (thread1) blocking on a sem_wait() and another thread (thread2)

  • 0

If you have a thread (thread1) blocking on a sem_wait() and another thread (thread2) destroying that very semaphore, using sem_destroy(), then what happens to thread1?

A quick search on the internet tells me that it produces undefined behavior:

Destroying a semaphore that other processes or threads are currently blocked on (in sem_wait(3)) produces undefined behavior.

But, I happened to see this being used in many multi-threaded c++ applications.

My main questions:

  • Could there be any purpose in this?
  • What were they trying to achieve (e.g. will this implicitly terminate the thread)?
  • Shouldn’t that be very unsafe?
  • 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-16T11:42:08+00:00Added an answer on June 16, 2026 at 11:42 am

    I can’t think of a single case in any API I’ve ever heard of where destroying something while it is in the middle of being used is sane or defined. So in my opinion the answers to your questions are:

    So what were they trying to achieve?

    I don’t know.

    shouldn’t that be very unsafe?

    Yes!

    Maybe the authors of those other programs you’ve looked at are aware of what the implementations actually do and are relying on it. But they have to be prepared for the possibility that it will change in the future. Maybe they have weighed the risk of such a change breaking their programs against the savings they achieved by taking a shortcut and relying on the undefined behaviour and have judged it to be worth it. You have to make that judgement for yourself.

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

Sidebar

Related Questions

I have a thread that sits in a blocking recv() loop and I want
I have a thread that is blocking on a Networkstream Read call. How do
I have one thread that add() s objects into a LinkedList queue, and another
I have an application processing network communication with blocking calls. Each thread manages a
I have a thread from a thread pool in a service that does quite
I have a problem - i can't compile SqlCipher. I'm using this http://groups.google.com/group/sqlcipher/browse_thread/thread/55c6296b56bf4533/c792bbec6df7d4f4?tvc=2#c792bbec6df7d4f4 instructions
Suppose I have a C# thread doing some blocking IO and waiting for it
I have a thread that waits on TcpListener.AcceptTcpClient() , which blocks , which I
I have a thread: void threadCode(object o) { doStuffHere(o); // Blocking call. Sometimes hangs.
I have a thread that reads messages from a named pipe. It is a

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.