diff options
Diffstat (limited to 'src/tests/threadid.cpp')
-rw-r--r-- | src/tests/threadid.cpp | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/src/tests/threadid.cpp b/src/tests/threadid.cpp new file mode 100644 index 0000000..9ff99df --- /dev/null +++ b/src/tests/threadid.cpp | |||
@@ -0,0 +1,72 @@ | |||
1 | #include <bu/thread.h> | ||
2 | #include <bu/sio.h> | ||
3 | |||
4 | #define BU_TRACE | ||
5 | #include <bu/trace.h> | ||
6 | |||
7 | using namespace Bu; | ||
8 | |||
9 | class CopyThing | ||
10 | { | ||
11 | public: | ||
12 | CopyThing() | ||
13 | { | ||
14 | TRACE(); | ||
15 | tidHome = Thread::currentThread(); | ||
16 | } | ||
17 | |||
18 | CopyThing( const CopyThing &rSrc ) | ||
19 | { | ||
20 | TRACE(); | ||
21 | tidHome = Thread::currentThread(); | ||
22 | sio << "Same thread? " << (tidHome == rSrc.tidHome) << sio.nl; | ||
23 | } | ||
24 | |||
25 | void doThings() | ||
26 | { | ||
27 | TRACE(); | ||
28 | if( tidHome != Thread::currentThread() ) | ||
29 | sio << "Different threads, hard copy here." << sio.nl; | ||
30 | else | ||
31 | sio << "Same thread, everything is cool." << sio.nl; | ||
32 | } | ||
33 | |||
34 | private: | ||
35 | ThreadId tidHome; | ||
36 | }; | ||
37 | |||
38 | class SubThread : public Thread | ||
39 | { | ||
40 | public: | ||
41 | SubThread( CopyThing &src ) : | ||
42 | src( src ) | ||
43 | { | ||
44 | src.doThings(); | ||
45 | } | ||
46 | |||
47 | protected: | ||
48 | void run() | ||
49 | { | ||
50 | src.doThings(); | ||
51 | sio << "run-Child is me? " << (getId() == Thread::currentThread()) << sio.nl; | ||
52 | } | ||
53 | |||
54 | private: | ||
55 | CopyThing src; | ||
56 | }; | ||
57 | |||
58 | int main( int argc, char *argv[] ) | ||
59 | { | ||
60 | CopyThing a; | ||
61 | |||
62 | SubThread st( a ); | ||
63 | st.start(); | ||
64 | |||
65 | sio << "Child is me? " << (st.getId() == Thread::currentThread()) << sio.nl; | ||
66 | |||
67 | st.join(); | ||
68 | |||
69 | |||
70 | return 0; | ||
71 | } | ||
72 | |||