aboutsummaryrefslogtreecommitdiff
path: root/src/tests/threadid.cpp
blob: dfea504978d9c86c7ba47c9e7f61ad18746b4c3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <bu/thread.h>
#include <bu/sio.h>

#define BU_TRACE
#include <bu/trace.h>

using namespace Bu;

class CopyThing
{
public:
    CopyThing()
    {
        TRACE();
        tidHome = Thread::currentThread();
    }

    CopyThing( const CopyThing &rSrc )
    {
        TRACE();
        tidHome = Thread::currentThread();
        sio << "Same thread?  " << (tidHome == rSrc.tidHome) << sio.nl;
    }

    void doThings()
    {
        TRACE();
        if( tidHome != Thread::currentThread() )
            sio << "Different threads, hard copy here." << sio.nl;
        else
            sio << "Same thread, everything is cool." << sio.nl;
    }

private:
    ThreadId tidHome;
};

class SubThread : public Thread
{
public:
    SubThread( CopyThing &src ) :
        src( src )
    {
        src.doThings();
    }

protected:
    void run()
    {
        src.doThings();
        sio << "run-Child is me? " << (getId() == Thread::currentThread()) << sio.nl;
    }

private:
    CopyThing src;
};

int main( int argc, char *argv[] )
{
    CopyThing a;

    SubThread st( a );
    st.start();

    sio << "Child is me? " << (st.getId() == Thread::currentThread()) << sio.nl;

    st.join();


    return 0;
}