Note that there are some explanatory texts on larger screens.

plurals
  1. POIs there a way to get non-locking stream insertion/extraction on basic_iostream in Windows?
    primarykey
    data
    text
    <p>I'm a C++ developer who has primarily programmed on Solaris and Linux until recently, when I was forced to create an application targeted to Windows.</p> <p>I've been using a communication design based on C++ I/O stream backed by TCP socket. The design is based on a single thread reading continuously from the stream (most of the time blocked in the socket read waiting for data) while other threads send through the same stream (synchronized by mutex).</p> <p>When moving to windows, I elected to use the boost::asio::ip::tcp::iostream to implement the socket stream. I was dismayed to find that the above multithreaded design resulted in deadlock on Windows. It appears that the <code>operator&lt;&lt;(std::basic_ostream&lt;...&gt;,std::basic_string&lt;...&gt;)</code> declares a 'Sentry' that locks the entire stream for both input and output operations. Since my read thread is always waiting on the stream, send operations from other threads deadlock when this Sentry is created. </p> <p>Here is the relevant part of the call stack during operator&lt;&lt; and Sentry construction:</p> <pre><code> ... ntdll.dll!7c901046() CAF.exe!_Mtxlock(_RTL_CRITICAL_SECTION * _Mtx=0x00397ad0) Line 45 C CAF.exe!std::_Mutex::_Lock() Line 24 + 0xb bytes C++ CAF.exe!std::basic_streambuf&lt;char,std::char_traits&lt;char&gt; &gt;::_Lock() Line 174 C++ CAF.exe!std::basic_ostream&lt;char,std::char_traits&lt;char&gt; &gt;::_Sentry_base::_Sentry_base(std::basic_ostream&lt;char,std::char_traits&lt;char&gt; &gt; &amp; _Ostr={...}) Line 78 C++ CAF.exe!std::basic_ostream&lt;char,std::char_traits&lt;char&gt; &gt;::sentry::sentry(std::basic_ostream&lt;char,std::char_traits&lt;char&gt; &gt; &amp; _Ostr={...}) Line 95 + 0x4e bytes C++ &gt; CAF.exe!std::operator&lt;&lt;&lt;char,std::char_traits&lt;char&gt;,std::allocator&lt;char&gt; &gt;(std::basic_ostream&lt;char,std::char_traits&lt;char&gt; &gt; &amp; _Ostr={...}, const std::basic_string&lt;char,std::char_traits&lt;char&gt;,std::allocator&lt;char&gt; &gt; &amp; _Str="###") Line 549 + 0xc bytes C++ ... </code></pre> <p>I would be fine if the istream and ostream components were locked separately, but that is not the case.</p> <p>Is there an alternate implementation of the stream operators that I can use? Can I direct it not to lock? Should I implement my own (not sure how to do this)?</p> <p>Any suggestions would be appreciated.</p> <p>(Platform is Windows 32- and 64-bit. Behavior observed with Visual Studio 2003 Pro and 2008 Express)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload