summaryrefslogtreecommitdiff
path: root/libs/thread/test/test_11053.cpp
blob: a8b6dfa976738bc3a5c5ff04e0350153aac833c0 (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
// Copyright (C) 2015 Vicente Botet
//
//  Distributed under the Boost Software License, Version 1.0. (See accompanying
//  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#define BOOST_THREAD_VERSION 4
#include <boost/thread.hpp>
#include <boost/thread/tss.hpp>
#include <boost/shared_ptr.hpp>
#include <iostream>

struct A
{
    void DoWork()
    {
        std::cout << "A: doing work\n";
        if (!m_ptr.get())
            m_ptr.reset(new WorkSpace());
        // do not very much
        for (size_t i = 0; i < 10; ++i)
            m_ptr->a += 10;
    }

private:
    struct WorkSpace
    {
        int a;
        WorkSpace() : a(0) {}
    };
    boost::thread_specific_ptr<WorkSpace> m_ptr;
};

struct B
{
    void DoWork()
    {
        std::cout << "B: doing work\n";
        if (!m_ptr.get())
            m_ptr.reset(new A());
        m_ptr->DoWork();
    }
private:
    boost::thread_specific_ptr<A> m_ptr;
};

struct C
{
    void DoWork()
    {
        std::cout << "C: doing work\n";
        if (!m_ptr.get())
            m_ptr.reset(new B());
        m_ptr->DoWork();
    }
private:
    boost::thread_specific_ptr<B> m_ptr;
};

int main(int ac, char** av)
{
    std::cout << "test starting\n";
    boost::shared_ptr<C> p_C(new C);
    boost::thread cWorker(&C::DoWork, p_C);
    cWorker.join();
    std::cout << "test stopping\n";
    return 0;
}