3 #include <msp/core/thread.h>
4 #include <msp/time/units.h>
5 #include <msp/time/utils.h>
6 #include <msp/test/test.h>
11 class ThreadTests: public Test::RegisteredTest<ThreadTests>
14 std::vector<int> data;
19 static const char *get_name() { return "Thread"; }
27 class TestThread: public Thread
37 TestThread(int *, unsigned, unsigned, int);
43 bool is_done() const { return done; }
47 ThreadTests::ThreadTests():
50 add(&ThreadTests::single, "Single thread");
51 add(&ThreadTests::multiple, "Multiple threads");
54 void ThreadTests::single()
56 fill(data.begin(), data.end(), -1);
58 TestThread thread(&data[0], data.size(), 1, 1);
60 while(wait && !thread.is_done())
62 Time::sleep(100*Time::msec);
67 fail("Thread did not finish");
71 for(vector<int>::iterator i=data.begin(); i!=data.end(); ++i)
76 void ThreadTests::multiple()
78 fill(data.begin(), data.end(), -1);
80 list<TestThread *> threads;
81 for(unsigned i=0; i<10; ++i)
82 threads.push_back(new TestThread(&data[i], data.size()/10, 10, i+1));
84 while(wait && !threads.empty())
86 Time::sleep(100*Time::msec);
87 for(list<TestThread *>::iterator i=threads.begin(); i!=threads.end();)
102 fail("Threads did not finish");
104 for(unsigned i=0; i<data.size(); ++i)
105 if(data[i]!=static_cast<int>(i%10+1))
106 fail("Invalid data");
110 TestThread::TestThread(int *s, unsigned c, unsigned t, int v):
120 void TestThread::main()
123 for(unsigned i=0; i<count; ++i)