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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T07:23:35+00:00 2026-05-30T07:23:35+00:00

I’m writing a C program composed of one dispatcher thread and N worker thread,

  • 0

I’m writing a C program composed of one dispatcher thread and N worker thread, the responsible of which are described below:

dispatcher thread:
listen on a TCP port;
do epoll_wait() repeatedly on that port;
when connection established, accept it and pass the new file descriptor(i.e. what the “accept” function return) to one of the N worker thread;

worker thread:
upon each new connection, do read repeatedly until no data received;
using all the data received as parameter to call the decode function which will decode the data to a message structure (i.e. an RTSP message);

what I wonder is that, if the data that worker thread read is incomplete, should I cache it which means that I should maintain a global list to cache the unused data(i.e. received but not of full message, so not used yet) for each connection?

  • 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-30T07:23:37+00:00Added an answer on May 30, 2026 at 7:23 am

    If you use a worker per socket I guess there is no problem, you just block until you get all the message.. I’m assuming this is not your case.

    If you use a worker for handling several sockets in a non-blocking manner, you could use this approach:

    1. Start reading the data in a pre-determined buffer size. (Try to match the size of the buffer to the maximum possible length of the message, this will save you copies).

    2. Determine the total message length (from the header of your protocol) and calculate how much you need to continue reading to finish the whole message. In this case, you may have already read “too much”, so you should allocate another buffer for the “next” message, and if you want to be more generic, you could keep n such buffers (based on the minimal message length and the assigned buffer to read).
      You could also choose to always read only the header and continue from there (this will make sure you do not read too much), but it will be more wasteful (you need two reads per each message).

    3. If the message is fully read, process it, otherwise, keep the buffer and the amount of bytes to read for this message and loop again through the sockets (your epool).

    4. On your next handling of the same socket, you will check if you currently have a partial message and continue reading into the same buffer from the location you finished the last time. You need to read here the next x bytes, and you need to be prepared to have less than what you expect.
      Here you could add also an optimization, reading all that you have (left in the buffer) on this socket in one shot (not only the next x bytes left, saving you some system calls). If you do that, you’ll need to use vectors (readv() or similar).

    If you go without the optimization stuff, it is pretty simple to handle.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
Does anyone know how can I replace this 2 symbol below from the string
I am reading a book about Javascript and jQuery and using one of the
I am writing an app with both english and french support. The app requests
I would like to run a str_replace or preg_replace which looks for certain words

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.