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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:23:48+00:00 2026-06-09T15:23:48+00:00

I am tasked to modify a synchronous C program so that it can run

  • 0

I am tasked to modify a synchronous C program so that it can run in parallel. The goal is to have it be as portable as possible as it is an open source program that many people use. Because of this, I thought it would be best to wrap the program in a C++ layer so that I could take advantage of the portable boost libraries. I have already done this and everything seems to work as expected.

The problem I am having is deciding on what is the best approach to pass messages between the threads. Luckily, the architecture of the program is that of a multiple producer and single consumer. Even better, the order of the messages is not important. I have read that single-producer/single-consumer (SPSC) queues would benefit from this architecture. Those experienced with multi-threaded programming have any advice? I’m quite new to this stuff. Also any code examples using boost to implement SPSC would be greatly appreciated.

  • 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-09T15:23:50+00:00Added an answer on June 9, 2026 at 3:23 pm

    Below is the technique I used for my Cooperative Multi-tasking / Multi-threading library (MACE) http://bytemaster.github.com/mace/. It has the benefit of being lock-free except for when the queue is empty.

    struct task {
       boost::function<void()> func;
       task* next;
    };
    
    
    boost::mutex                     task_ready_mutex;
    boost::condition_variable        task_ready;
    boost::atomic<task*>             task_in_queue;
    
    // this can be called from any thread
    void thread::post_task( task* t ) {
         // atomically post the task to the queue.
         task* stale_head = task_in_queue.load(boost::memory_order_relaxed);
         do { t->next = stale_head;
         } while( !task_in_queue.compare_exchange_weak( stale_head, t, boost::memory_order_release ) );
    
       // Because only one thread can post the 'first task', only that thread will attempt
       // to aquire the lock and therefore there should be no contention on this lock except
       // when *this thread is about to block on a wait condition.  
        if( !stale_head ) { 
            boost::unique_lock<boost::mutex> lock(task_ready_mutex);
            task_ready.notify_one();
        }
    }
    
    // this is the consumer thread.
    void process_tasks() {
      while( !done ) {
       // this will atomically pop everything that has been posted so far.
       pending = task_in_queue.exchange(0,boost::memory_order_consume);
       // pending is a linked list in 'reverse post order', so process them
       // from tail to head if you want to maintain order.
    
       if( !pending ) { // lock scope
          boost::unique_lock<boost::mutex> lock(task_ready_mutex);                
          // check one last time while holding the lock before blocking.
          if( !task_in_queue ) task_ready.wait( lock );
       }
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been tasked with writing a program which will run a set of
I have been tasked with refactoring some components that used xmlbeans to now make
I am new to WIX and have been tasked with creating an installer that
i have been tasked with changing a bit of css for a site that
I have been tasked for university to program a mobile phone application. I have
I have a Windows shell script that opens an application. I'd like to modify
im tasked to create a virtual database that clients can create from our web
I have been tasked with creating a SQL query (limited knowledge) that will show
I have been tasked with testing some XSLT code to ensure that it is
I have been tasked with integrating PayPal into a website that is in development.

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.