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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:24:34+00:00 2026-05-24T03:24:34+00:00

I am working on a little multi-threading project.The system could be divided into 2

  • 0

I am working on a little multi-threading project.The system could be divided into 2 sub parts, A and B. the data flows from A to B.
A part keeps fetching raw data from outside world, do some transformation and and then generate thousands of new data, let’s call it A_OUTPUT.
B part do some calculations based on each A_OUTPUT and then generate even more data, may be ten times the number of A_OUTPUT.

I am confused about how to synchronize the 2 parts.
My own design is to create a work queue as well as a lock protecting the queue between the two sub parts. Also create a event to indicate whether the work queue is empty or not.

A part consists multiple threads, each thread fetch data from outside and generate A_OUTPUT, each time a single A thread generate a A_OUTPUT, the thread obtain the queue lock, push the A_OUTPUT into the queue, release the lock, and then trigger the event.

B part consists a supervisor thread and several worker threads, the supervisor thread was first blocked on the event. after the event was triggered, the supervisor thread lock the queue, fetch all A_OUTPUTS of the queue, release the lock, dispatch A_OUTPUTS to worker threads, and then wait on the event again.

The problem of this design is obvious, the supervisor thread of B will be racing with multiple threads of A to win the queue lock. maybe when B finally own the lock, there was already ten or more A_OUTPUTs in the queue, and the most aged A_OUTPUT was generated a long time ago. I want each A_OUTPUT to be processed as fast as possible.

I know I could divide the work queue into several smaller queues or add more B supervisor threads into the lock battle to shorten the average time each A_OUTPUT wait before it get processed. but might there exist a more appropriate design?

And another question, does there exists any paradigm or design pattern for different purpose multi-thread programs?

  • 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-24T03:24:34+00:00Added an answer on May 24, 2026 at 3:24 am

    It’s rather classical problem that is well described in wikipedia

    I can recommend following approach:

    Synchronize access to the queue by mutex. keep two condition variables, one to signal that queue is not full (you need to handle cases when Producer produces more data than Consumer can consume) and another one to signal that queue has any data.

    Producer checks if queue is not full. If full – waits for condition “not full”, otherwise produces some data, puts it into queue, notifies “has data” condition.

    Consumer checks if queue has any data, consumes it and notifies “not full” condition

    Also you can use lock-free queue for better performance. Check TBB or recently announced Boost.Lockless (under review at the moment). By the way, using TBB the whole task is much simpler, just use their dispatcher and containers and forgot about
    explicit synchronization

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

Sidebar

Related Questions

I'm working on a little project where I'd like to parse some data, and
I'm working on a little web project that needs to keeps to a NumViews
I'm working on a little web crawler that will run in the system tray
I am working on a little pinball-game project for a hobby and am looking
I am currently working in a .Net project but from university I have also
I'm working on a little server app with Java. So, I'm getting informations from
I am working a little project for myself using ASP.Net MVC 2 Preview 2
I'm working on a little project to make a 'memeplayer' in Excel with YouTube
I'm working on a little part of my program, handling the input, basically I
Im working on a little hobby project. I already have written the code to

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.