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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:40:00+00:00 2026-05-24T23:40:00+00:00

Main thread reads data from sockets and stores it in queue. Second thread write

  • 0

Main thread reads data from sockets and stores it in queue. Second thread write this data to disk file. But if second writes faster than first reads data, then how to organize waiting.

I wanna do it with semaphore, but how i can increase semaphore counter then next data chunk reads into queue. How to decreasde it in second thread i know.

  • 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-24T23:40:00+00:00Added an answer on May 24, 2026 at 11:40 pm

    Sounds like a ‘classic’ producer-consumer queue. You need a thread-safe queue and a semaphore, initialized to 0, to count objects in it. If you have no thread-safe queue, another semaphore initialized to 1, (or a mutex), will do to protect the queue from multiple access.

    Essentially, in the producer, malloc a buffer struct, read data from socket into it, push its address onto the thread-safe queue, signal the semaphore. In the consumer, wait on the semaphore, pop a buffer address from the queue, write the data to your disk file, free the buffer.

    You could, with C++, substitute a buffer class and new/dispose for the buffer struct and malloc/free. The socket/disk operations could then be methods of the buffer class.

    Rgds,
    Martin

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

Sidebar

Related Questions

I spin off a new thread that reads data from a database. Every X
Apart from main thread, I've ThinkerThread object _thinker whose finished() signal is connected to
I have the main thread from which I start a window using invokeLater .
I've inherited code where BeginInvoke is called from the main thread (not a background
Given a System.Timers.Timer, is there a way from the main thread to tell if
my C# application reads data from special USB device. The data are read as
I am trying to launch a background thread to retrieve XML data from a
I have a thread reading data from a bluetooth stream that sends the data
Hi there I have to read data from text file into the array in
I'm using chcsvparser to parse data from a csv file on my apps launch.

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.