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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:20:56+00:00 2026-05-31T16:20:56+00:00

Could I close tcp::socket in different thread from the sync-reading thread? It looks like:

  • 0

Could I close tcp::socket in different thread from the sync-reading thread?
It looks like:

boost::asio::ip::tcp::socket* tcp_socket;  //blocking mode

thread1:

while(true){
   try{
       std::vector<char> read_buffer(10);
       tcp_socket->read_some( boost::asio::buffer( read_buffer ) );
  }
  catch(boost::system::system_error& e){
  //TODO
  break;
  }
}

thread2:

tcp_socket->shutdown(boost::asio::ip::tcp::socket::shutdown_both);
tcp_socket->close();

I saw the document of tcp::socket. They say the object is thread-unsafty.But the demo code seems working well.
So Is it safe? What about tcp::acceptor? Could I invoke close and accept in multithreading
on same tcp::acceptor?

  • 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-31T16:20:57+00:00Added an answer on May 31, 2026 at 4:20 pm

    The documentation states that tcp::socket is not thread safe for shared objects.

    Don’t count on it seemingly working as a guarantee that it will always work.

    Moreover, closing a socket from another thread, at the socket layer, is not a portable way of getting the blocking thread to unblock.

    Here’s what I would suggest:

    • Use the asynchronous APIs for asio.
    • Protect the socket with a mutex to prevent concurrent access or use an asio strand to serialize the access to it.

    Quoting the author on a similar question:

    Actually…

    In practice, it will probably work on the platforms asio currently
    supports**. However, I have deliberately specified the interface such
    that it is not thread safe. This is to permit implementations to
    store additional state in the socket object without needing explicit
    synchronisation.

    If you want to run multiple concurrent operations on the same socket,
    the safe, portable way is to use the asynchronous operations.

    **Unless you make the socket non-blocking.

    Cheers, Chris

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

Sidebar

Related Questions

What is the recommended way to gracefully close a TCP socket? I've learned that
My app reads bytes from a TCP socket and needs to buffer them up,
Back in the days of Unix, you couldn't even close a software without reading
I'm working on an existing DLL where a TCP listening socket is created and
I'm having trouble connecting a socket from my android phone to my computer running
I have a client TCP socket that writes a few bytes every five seconds,
I am using the C++ boost asio library, where I listen to new connections
I'm sending sensor data from an android device to a TCP server in C#.
HTTP is an application protocol and the underlying TCP connection could be closed and
I'm using TCP socket connetion between a server program and a client program. Multiple

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.