File: threads.cpp

package info (click to toggle)
mldemos 0.5.1-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 32,224 kB
  • ctags: 46,525
  • sloc: cpp: 306,887; ansic: 167,718; ml: 126; sh: 109; makefile: 2
file content (128 lines) | stat: -rw-r--r-- 3,717 bytes parent folder | download | duplicates (6)
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright (C) 2006  Davis E. King (davis@dlib.net)
// License: Boost Software License   See LICENSE.txt for the full license.


#include <sstream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <dlib/misc_api.h>
#include <dlib/threads.h>

#include "tester.h"

namespace  
{
    using namespace test;
    using namespace dlib;
    using namespace std;

    logger dlog("test.threads");

    class threads_tester : public tester
    {
    public:
        threads_tester (
        ) :
            tester ("test_threads",
                    "Runs tests on the threads component."),
            sm(cm)
        {}

        thread_specific_data<int> tsd;
        rmutex cm;
        rsignaler sm;
        int count;
        bool failure;

        void perform_test (
        )
        {
            failure = false;
            print_spinner();


            count = 10;
            if (!create_new_thread<threads_tester,&threads_tester::thread1>(*this)) failure = true;
            if (!create_new_thread<threads_tester,&threads_tester::thread2>(*this)) failure = true;
            if (!create_new_thread<threads_tester,&threads_tester::thread3>(*this)) failure = true;
            if (!create_new_thread<threads_tester,&threads_tester::thread4>(*this)) failure = true;
            if (!create_new_thread<threads_tester,&threads_tester::thread5>(*this)) failure = true;
            if (!create_new_thread<threads_tester,&threads_tester::thread6>(*this)) failure = true;
            if (!create_new_thread<threads_tester,&threads_tester::thread7>(*this)) failure = true;
            if (!create_new_thread<threads_tester,&threads_tester::thread8>(*this)) failure = true;
            if (!create_new_thread<threads_tester,&threads_tester::thread9>(*this)) failure = true;
            if (!create_new_thread<threads_tester,&threads_tester::thread10>(*this)) failure = true;

            thread(66);

            // this should happen in the main program thread
            if (is_dlib_thread())
                failure = true;

            auto_mutex M(cm);
            while (count > 0 && !failure)
                sm.wait();


            DLIB_TEST(!failure);
        }

        void thread_end_handler (
        )
        {
            auto_mutex M(cm);
            --count;
            if (count == 0)
                sm.signal();
        }

        void thread1() { thread(1); }
        void thread2() 
        { 
            thread(2); 
            if (is_dlib_thread() == false)
                failure = true;
        }
        void thread3() { thread(3); }
        void thread4() { thread(4); }
        void thread5() { thread(5); }
        void thread6() { thread(6); }
        void thread7() { thread(7); }
        void thread8() { thread(8); }
        void thread9() { thread(9); }
        void thread10() { thread(10); }

        void thread (
            int num
        )
        {
            dlog << LTRACE << "starting thread num " << num;
            if (is_dlib_thread())
                register_thread_end_handler(*this,&threads_tester::thread_end_handler);
            tsd.data() = num;
            for (int i = 0; i < 0x3FFFF; ++i)
            {
                if ((i&0xFFF) == 0)
                {
                    print_spinner();
                    dlib::sleep(10);
                }
                // if this isn't equal to num then there is a problem with the thread specific data stuff
                if (tsd.data() != num)
                {
                    auto_mutex M(cm);
                    failure = true;
                    sm.signal();
                }
            }
            dlog << LTRACE << "ending of thread num " << num;

        }
    } a;


}