<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[C++ Threads: try_lock_for]]></title><description><![CDATA[<p>Wenn ich</p>
<pre><code>std::timed_mutex mux;

void hello()
{
    if ( mux.try_lock_for(std::chrono::seconds(10)) )
    {
        cout &lt;&lt; &quot;Acquired lock\n&quot;;
        mux.unlock();
    }
    else
        cout &lt;&lt; &quot;Timeout\n&quot;;

}

int main()
{
    std::thread t1(hello), t2(hello);
    t1.join();
    t2.join();
}
</code></pre>
<p>ausführe bekomme ich die Ausgabe</p>
<blockquote>
<p>Acquired lock<br />
Timeout</p>
</blockquote>
<p>Ich würde allerdings</p>
<blockquote>
<p>Acquired lock<br />
Acquired lock</p>
</blockquote>
<p>erwarten.</p>
<p>Compiler: g++ 4.8.1</p>
<p>Irgendwo habe ich gelesen dass der g++ einen Bug drin hat oder hatte. Sieht das für euch nach einem Bug aus oder habe ich <code>try_lock_for</code> falsch verstanden?</p>
<p>PS:<br />
Ich weiss dass ich den Zugriff auf <code>std::cout</code> auch noch synchronisieren müsste. Aber daran liegt es wohl nicht.</p>
<p>*Edit<br />
Habe <a href="http://en.cppreference.com/w/cpp/thread/timed_mutex/try_lock_for" rel="nofollow">hier..</a> gerade folgendes gesehen:</p>
<blockquote>
<p>As with try_lock(), this function is allowed to fail spuriously and return false even if the mutex was not locked by any other thread at some point during timeout_duration.</p>
</blockquote>
]]></description><link>https://www.c-plusplus.net/forum/topic/327746/c-threads-try_lock_for</link><generator>RSS for Node</generator><lastBuildDate>Wed, 08 Jul 2026 23:52:57 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/327746.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 01 Sep 2014 21:48:39 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to C++ Threads: try_lock_for on Mon, 01 Sep 2014 22:02:08 GMT]]></title><description><![CDATA[<p>Wenn ich</p>
<pre><code>std::timed_mutex mux;

void hello()
{
    if ( mux.try_lock_for(std::chrono::seconds(10)) )
    {
        cout &lt;&lt; &quot;Acquired lock\n&quot;;
        mux.unlock();
    }
    else
        cout &lt;&lt; &quot;Timeout\n&quot;;

}

int main()
{
    std::thread t1(hello), t2(hello);
    t1.join();
    t2.join();
}
</code></pre>
<p>ausführe bekomme ich die Ausgabe</p>
<blockquote>
<p>Acquired lock<br />
Timeout</p>
</blockquote>
<p>Ich würde allerdings</p>
<blockquote>
<p>Acquired lock<br />
Acquired lock</p>
</blockquote>
<p>erwarten.</p>
<p>Compiler: g++ 4.8.1</p>
<p>Irgendwo habe ich gelesen dass der g++ einen Bug drin hat oder hatte. Sieht das für euch nach einem Bug aus oder habe ich <code>try_lock_for</code> falsch verstanden?</p>
<p>PS:<br />
Ich weiss dass ich den Zugriff auf <code>std::cout</code> auch noch synchronisieren müsste. Aber daran liegt es wohl nicht.</p>
<p>*Edit<br />
Habe <a href="http://en.cppreference.com/w/cpp/thread/timed_mutex/try_lock_for" rel="nofollow">hier..</a> gerade folgendes gesehen:</p>
<blockquote>
<p>As with try_lock(), this function is allowed to fail spuriously and return false even if the mutex was not locked by any other thread at some point during timeout_duration.</p>
</blockquote>
]]></description><link>https://www.c-plusplus.net/forum/post/2415738</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2415738</guid><dc:creator><![CDATA[icarus2]]></dc:creator><pubDate>Mon, 01 Sep 2014 22:02:08 GMT</pubDate></item></channel></rss>