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

  • Home
  • SEARCH
  • 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 6698499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:35:06+00:00 2026-05-26T06:35:06+00:00

Here is what I essentially have: I have thread A that periodically checks for

  • 0

Here is what I essentially have:

I have thread A that periodically checks for messages and processes them.

Threads B and C need to send messages to A.

The problem arises when B and C or B or C try to send a message to A while A is processing a message and thus accessing the queue.

How is this problem usually solved?

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-26T06:35:07+00:00Added an answer on May 26, 2026 at 6:35 am

    This is normally solved using mutexes, or other multi-thread protection mechanisms.

    If you are working on windows, MFC provides the CMutex class for this problem.

    If you are working on a posix system, the posix api provides the pthread_mutex_lock, pthread_mutex_unlock, and pthread_mutex_trylock functions.

    Some basic pseudocode would be handy to demonstrate their use in your case:

    pthread_mutex_t mutex; *or* CMutex mutex;
    Q queue;  // <-- both mutex and queue are global state, whether they are
              //     global variables, or passed in as parameters, they must
              //     be the shared by all threads.
    
    int threadA(/* params */){
        while( threadAStillRunning ){
            // perform some non-critical actions ...
            pthread_mutex_lock(mutex) *or* mutex.Lock()
            // perform critical actions ...
            msg = queue.receiveMessage()
            pthread_mutex_unlock(mutex) *or* mutex.Unlock()
            // perform more non-critical actions
        }
    }
    
    int threadBorC(/* params */){
        while( theadBorCStillRunning ){
            // perform some non-critical actions ...
            pthread_mutex_lock(mutex) *or* mutex.Lock()
            // perform critical actions ...
            queue.sendMessage(a_msg)
            pthread_mutex_unlock(mutex) *or* mutex.Unlock()
        }
    }
    

    For all three threads, their ability to act on the queue hinges on their ability to acquire the mutex – they will simply block and wait until the mutex is acquired. This prevents conflicts arising from the use of that resource.

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

Sidebar

Related Questions

I have essentially the same problem discussed here: http://khason.net/blog/dependency-property-getters-and-setters-in-multithreaded-environment/ public static readonly DependencyProperty MyPropertyProperty
What I'm talking about here are nested classes. Essentially, I have two classes that
I have a mockup layout for something here . Essentially there are sections, columns
We have a reverse proxy here, running Apache in version 2.2.x Essentially I want
Here's the setup. I have web site which is essentially a simple CMS. Here
Here's a problem I ran into recently. I have attributes strings of the form
Here is the issue I am having: I have a large query that needs
I have a very large, spread out class that is essentially a very contrived
We have a system in which each thread (there can be dozens of them)
Since upgrading to 2008 I and many people here have noticed that randomly VS

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.