$include_dir="/home/hyper-archives/boost-commit/include"; include("$include_dir/msg-header.inc") ?>
From: hinnant_at_[hidden]
Date: 2007-12-09 10:53:22
Author: hinnant
Date: 2007-12-09 10:53:22 EST (Sun, 09 Dec 2007)
New Revision: 41921
URL: http://svn.boost.org/trac/boost/changeset/41921
Log:
Added thread_id example.
Added:
   sandbox/committee/LWG/examples/
   sandbox/committee/LWG/examples/thread_id.cpp   (contents, props changed)
   sandbox/committee/LWG/examples/thread_id.html   (contents, props changed)
   sandbox/committee/LWG/examples/thread_id.out   (contents, props changed)
Added: sandbox/committee/LWG/examples/thread_id.cpp
==============================================================================
--- (empty file)
+++ sandbox/committee/LWG/examples/thread_id.cpp	2007-12-09 10:53:22 EST (Sun, 09 Dec 2007)
@@ -0,0 +1,91 @@
+#include <iostream>
+#include <thread>
+#include <mutex>
+#include <condition_variable>
+
+std::mutex mut;
+std::condition_variable cv;
+enum {mains_turn, f1s_turn, f2s_turn};
+int turn = mains_turn;
+std::thread tmp;
+typedef std::unique_lock<std::mutex> Lock;
+
+void f2(std::thread& tf1)
+{
+    Lock lk(mut);
+    std::thread f1 = move(tf1);
+    while (turn != f2s_turn)
+        cv.wait(lk);
+    std::cout << "f2 executing\n";
+    std::cout << "f2 sees f1::id as " << f1.get_id()
+              << " which is the same value both main and f1 saw earlier\n";
+    std::cout << "f2 sees std::this_thread::get_id() as "
+              << std::this_thread::get_id()
+              << " which is the same value main saw earlier\n";
+    f1.detach();
+    std::cout << "f1 detached by f2\n";
+    turn = f1s_turn;
+    std::cout << "f2 asking f1 to execute\n";
+    cv.notify_all();
+    while (turn != f2s_turn)
+        cv.wait(lk);
+    std::cout << "f2 executing\n";
+    std::cout << "f2 sees f1::id as " << f1.get_id() << " because it has detached f1\n";
+    std::cout << "f2 sees std::this_thread::get_id() as " << std::this_thread::get_id() << '\n';
+    turn = mains_turn;
+    std::cout << "f2 asking main to execute\n";
+    cv.notify_one();
+    std::cout << "f2 ending\n";
+}
+
+void f1()
+{
+    Lock lk(mut);
+    while (turn != f1s_turn)
+        cv.wait(lk);
+    std::cout << "f1 executing\n";
+    std::cout << "f1 sees std::this_thread::get_id() as "
+              << std::this_thread::get_id()
+              << " which is the same value main saw earlier\n";
+    turn = mains_turn;
+    std::cout << "f1 asking main to execute\n";
+    cv.notify_all();
+    while (turn != f1s_turn)
+        cv.wait(lk);
+    std::cout << "f1 executing\n";
+    std::cout << "f1 sees std::this_thread::get_id() as "
+              << std::this_thread::get_id()
+              << " which is the same value both main and f2 saw earlier\n";
+    turn = f2s_turn;
+    std::cout << "f1 asking f2 to execute\n";
+    cv.notify_all();
+    std::cout << "f1 ending\n";
+}
+
+int main()
+{
+    std::cout << "main executing\n";
+    std::thread tf1(f1);
+    std::cout << "main sees f1::id as " << tf1.get_id() << '\n';
+    Lock lk(mut);
+    turn = f1s_turn;
+    std::cout << "main asking f1 to execute\n";
+    cv.notify_one();
+    tmp = move(tf1);
+    std::thread tf2(f2, std::ref(tmp));
+    while (turn != mains_turn)
+        cv.wait(lk);
+    std::cout << "main executing\n";
+    std::cout << "main sees f1::id as " << tf1.get_id() << " because it has moved f1 to the f2 thread\n";
+    std::cout << "main sees f2::id as " << tf2.get_id() << '\n';
+    tf2.detach();
+    std::cout << "f2 detached by main\n";
+    std::cout << "main sees f2::id as " << tf2.get_id() << " because it has detached f2\n";
+    turn = f2s_turn;
+    std::cout << "main asking f2 to execute\n";
+    cv.notify_all();
+    while (turn != mains_turn)
+        cv.wait(lk);
+    std::cout << "main executing\n";
+    std::cout << "main ending\n";
+}
Added: sandbox/committee/LWG/examples/thread_id.html
==============================================================================
--- (empty file)
+++ sandbox/committee/LWG/examples/thread_id.html	2007-12-09 10:53:22 EST (Sun, 09 Dec 2007)
@@ -0,0 +1,23 @@
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
+        "http://www.w3.org/TR/html4/loose.dtd">
+<html>
+<head>
+	<title>thread_id example</title>
+</head>
+<body>
+<p>
+This example compares <code>thread::get_id</code> with <code>this_thread::get_id</code>, and how  
+it plays with <code>detach()</code>.  In one spot <code>main</code> attempts to move a <code>std::thread</code>
+from itself to another scope.  Because my <code>bind</code> and <code>tuple</code> haven't been  
+"moved" yet, I had to fake it with an ugly <code>std::ref</code> followed by an  
+inappropriate move.
+</p>
+<p>
+If one follows the output and source together, as if you were a human  
+source-level debugger, the logic should become clear.    Also note  
+that <code>detach</code> is used (twice!) and process termination is guaranteed to  
+be orderly (in this example).
+</p>
+
+</body>
+</html>
Added: sandbox/committee/LWG/examples/thread_id.out
==============================================================================
--- (empty file)
+++ sandbox/committee/LWG/examples/thread_id.out	2007-12-09 10:53:22 EST (Sun, 09 Dec 2007)
@@ -0,0 +1,28 @@
+main executing
+main sees f1::id as 0xb0081000
+main asking f1 to execute
+f1 executing
+f1 sees std::this_thread::get_id() as 0xb0081000 which is the same value main saw earlier
+f1 asking main to execute
+main executing
+main sees f1::id as 0 because it has moved f1 to the f2 thread
+main sees f2::id as 0xb0103000
+f2 detached by main
+main sees f2::id as 0 because it has detached f2
+main asking f2 to execute
+f2 executing
+f2 sees f1::id as 0xb0081000 which is the same value both main and f1 saw earlier
+f2 sees std::this_thread::get_id() as 0xb0103000 which is the same value main saw earlier
+f1 detached by f2
+f2 asking f1 to execute
+f1 executing
+f1 sees std::this_thread::get_id() as 0xb0081000 which is the same value both main and f2 saw earlier
+f1 asking f2 to execute
+f1 ending
+f2 executing
+f2 sees f1::id as 0 because it has detached f1
+f2 sees std::this_thread::get_id() as 0xb0103000
+f2 asking main to execute
+f2 ending
+main executing
+main ending