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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:00:37+00:00 2026-06-10T15:00:37+00:00

I am using c++ with boost threads, locks mutexs and all that good stuff.

  • 0

I am using c++ with boost threads, locks mutexs and all that good stuff.

Two threads A and B. A is a networking thread where there is information coming back and forth from the client. B is a worker with variables which needs to be thread safe.

There are multiple threads from the class A and B is just a single thread. Instances of A will be added to B’s list of jobs and A will lock until B has completed the task. (The thread safe list of jobs works fine)

A adds the job to B and then locks, however in most cases before A has locked. B will have processed the job and have (tried to) release the lock before A has applied it. Resulting in As dead lock.

I am not sure what I should be doing here. I could post code but I would imagine is easier to discuss conceptually.

  • 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-10T15:00:39+00:00Added an answer on June 10, 2026 at 3:00 pm

    The actual problem is in that the B gets the task and starts to execute it before the thread from A locks. The result you observer can be classified as “missed notification”. Quite often when trying to implement two-way synchronization with i.e. mutexes or other simple synchronizers.

    You have to make sure that the B does not notify unless A is already waiting, OR you can make sure that the notification is not missed.

    While the first way may be a quite tricky to peform , the latter is very simple: use a semaphore (or manual-reset-event or (…)).

    On semaphores:

    • thread A wants to put a job onto queue
    • thread A creates a semaphore with COUNT=ZERO and MAX=1
    • thread A somehow adds the semaphore to the job
    • thread A puts the job on queue
    • thread A waits on the semaphore
    • thread A destroys the semaphore

    while:

    • thread B observes the queue
    • thread B gets the task
    • thread B performs the job
    • thread B unlocks the semaphore ONCE

    Note that the semaphore UNLOCK is nonblocking if count

    Instead of using a semaphore, you can use also events with manual set/reset. Just let the A create-and-lock on the event that is OFF and let the B set it ON.

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

Sidebar

Related Questions

I started using boost::thread recently (WinXP, VS10, BoostPro) and found that mutex can be
I am having some trouble using the pantheios logging library with boost::threads. It seems
What are the differences in using boost:thread, Posix Thread library and the new C++11
I am using wxwidgets together with boost::thread. The Thread is a worker thread which
I've been using Boost::regex and Boost::regex_search and found that when I run the regex
I am using boost::any to store pointers and was wondering if there was a
I'm writing a multi-threaded server using boost::asio (for sockets), boost::thread (for threading), libconfig++ (for
Code: #include <iostream> #include stdafx.h #include <boost/thread.hpp> #include <boost/thread/mutex.hpp> using namespace std; boost::mutex mut;
I have the below simple program using boost threads, what would be the changes
I'm having a weird problem with a threaded class using Boost::threads. Here 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.