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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:07:05+00:00 2026-06-04T22:07:05+00:00

I am doing serial IO on linux in c++ using multi-threading. Currently I am

  • 0

I am doing serial IO on linux in c++ using multi-threading. Currently I am using a blocking read. That leaves me with no way stop the thread cought in the blocking read() except to forcefully terminate or interrupt the thread or use something like pthread cancellation. Now all over the net i see people scream at people suggesting they need to terminate there threads from a blocking IO. Usually its concerning memory leaks. Is there some magic memory leak that can appear out of a thread interruption as long as you clean up properly?

try
{
    while(true)
    {
        blocking_read(fd,buffer,512);
    }
}catch(interrupt_exception)
{

}
//clean up, close fd, release heap memory, usual stuff

Or is my only alternative something like the below or implement a higher level protocol ensuring the blocking read receives a sign off input enabling it to shut itself down.

try
{
    while(running)
    {
        nonblocking_read(fd,buffer,512);

        if(cancel)
            running = false; //break return etc
    }
}

//clean up, close fd, release heap memory, usual stuff

So again, do there happen some magic memory leak in read() if you interrupt the thread causing it to throw a exception.

Or should i simply not care and let the destructor kill the thread(i assume thread terminates when you delete your object holding the thread)? and do clean up there? like

class MyClass{
    int fd;    
    Thread* myThread;
    ~MyClass(){
        delete myThread;
        close(fd);
    }
};

Thanks for any help!

  • 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-04T22:07:07+00:00Added an answer on June 4, 2026 at 10:07 pm

    read() should not leak memory. With both blocking and nonblocking reads, the application code is still responsible for the management of the memory provided as the buf argument. Interrupting read() via a signal will not throw an exception, so if use signals, you will need to check the result and errno.

    • If read() is interrupted by a signal before reading data, -1 will be returned with errno set to EINTR.
    • If read() is interrupted by a signal after reading some data, POSIX allows for either -1 to be returned with errno set to EINTR, or for read() to return the number of bytes already read.

    If you use pthread_cancel() then an exception will be thrown. With this approach, you have the following options:

    • Perform cleanup by registering cleanup functions with pthread_cleanup_push().
    • Allocate dynamic memory, storing into thread specific storage via pthread_setspecific().
    • Manage the memory via an auto_ptr/unique_ptr.
    • Catch the abi::__forced_unwind exception, perform cleanup, and rethrow.

    In general, consider avoiding thread cancellation. When possible, it is better, and more manageable, to have a shared flag that is used to break out of the loop. This allows the thread to perform any necessary cleanup, and prevents your implementation from being dependent on your threading library’s implementation and any of its quirks.

    For your case, where you are using a blocking read, consider polling the fd to see if data is available via select() with a timeout, and only call read() if the fd has data. This allows you to periodically check if the thread flag is set to no longer run, and prevents you from needing to deal with signals to interrupt the thread from read(), as read() should no longer block waiting on data.

    Also, the behavior that occurs when deleting a thread object is dependent on the thread library. For example, deleting a pthread_t or boost::thread has no effect on the associated thread’s execution.

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

Sidebar

Related Questions

I have CPU bound application that I wish to speedup using multiprocessing+threading instead of
I had read this: Simple serial point-to-point communication protocol and I'm doing the same
I am writing text to a multi-line text box using data from the serial
Doing a custom Tumblr theme that will have a mix of the usual Tumblr
Doing a Google search I found several command-line SCEP clients that I can download
Doing an ajax get request works as expected using the following code: $.ajax({ type:
I am working on robot which has to control using wireless serial communication. The
I am trying to figure out how to start and stop a serial interface.
I'm writing a console application under Ubuntu that uses the serial port. It needs
I wrote a simple Tkinter based Python application that reads text from a serial

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.