Sophie

Sophie

distrib > Mandriva > 2007.0 > i586 > by-pkgid > ad1ba1135a9c9eeffc2e538163e00373 > files > 884

libCommonC++2_1.4-devel-1.4.1-1mdv2007.0.i586.rpm

\section{thread1.cpp}


\footnotesize\begin{verbatim}1 #include <cc++/thread.h>
2 #include <cstdio>
3 
4 #ifdef  CCXX_NAMESPACES
5 using namespace ost;
6 #endif
7 
8 // This is a little regression test
9 // 
10 
11 class ThreadTest: public Thread
12 {
13 public:
14         ThreadTest();
15         void run();
16 };
17 
18 volatile int n = 0;
19 
20 bool WaitNValue(int value)
21 {
22         for(int i=0;; ++i)
23         {
24                 if (n == value)
25                         break;
26                 if (i >= 100)
27                         return false;
28                 Thread::sleep(10);
29         }
30         return true;
31 }
32 
33 bool WaitChangeNValue(int value)
34 {
35         for(int i=0;; ++i)
36         {
37                 if (n != value)
38                         break;
39                 if (i >= 100)
40                         return false;
41                 Thread::sleep(10);
42         }
43         return true;
44 }
45 
46 ThreadTest::ThreadTest()
47 {
48 }
49 
50 void ThreadTest::run()
51 {
52         setCancel(Thread::cancelDeferred);
53         n = 1;
54 
55         // wait for main thread
56         if (!WaitNValue(2)) return;
57 
58         // increment infinitely
59         for(;;)
60         {
61                 yield();
62                 n = n+1;
63         }
64 }
65 
66 bool TestChange(bool shouldChange)
67 {
68         if (shouldChange)
69                 printf("- thread should change n...");
70         else
71                 printf("- thread should not change n...");
72         if (WaitChangeNValue(n) == shouldChange)
73         {
74                 printf("ok\n");
75                 return true;
76         }
77         printf("ko\n");
78         return false;
79 }
80 
81 #undef ERROR
82 #undef OK
83 #define ERROR {printf("ko\n"); return 1; }
84 #define OK {printf("ok\n"); }
85 
86 #define TEST_CHANGE(b) if (!TestChange(b)) return 1;
87 
88 int main(int argc, char* argv[])
89 {
90         ThreadTest test;
91 
92         // test only thread, without sincronization
93         printf("***********************************************\n");
94         printf("* Testing class Thread without syncronization *\n");
95         printf("***********************************************\n");
96 
97         printf("Testing thread creation\n\n");
98         n = 0;
99         test.start();
100 
101         // wait for n == 1
102         printf("- thread should set n to 1...");
103         if (WaitNValue(1)) OK
104         else ERROR;
105 
106         // increment number in thread
107         printf("\nTesting thread is working\n\n");
108         n = 2;
109         TEST_CHANGE(true);
110         TEST_CHANGE(true);
111         
112         // suspend thread, variable should not change
113         printf("\nTesting suspend & resume\n\n");
114         test.suspend();
115         TEST_CHANGE(false);
116         TEST_CHANGE(false);
117 
118         // resume, variable should change
119         test.resume();
120         TEST_CHANGE(true);
121         TEST_CHANGE(true);
122 
123         printf("\nTesting recursive suspend & resume\n\n");
124         test.suspend();
125         test.suspend();
126         TEST_CHANGE(false);
127         TEST_CHANGE(false);
128         
129         test.resume();
130         TEST_CHANGE(false);
131         TEST_CHANGE(false);
132         test.resume();
133         TEST_CHANGE(true);
134         TEST_CHANGE(true);
135 
136         printf("\nTesting no suspend on resume\n\n");
137         test.resume();
138         TEST_CHANGE(true);
139         TEST_CHANGE(true);
140                 
141         // suspend thread, variable should not change
142         printf("\nTesting resuspend\n\n");
143         test.suspend();
144         TEST_CHANGE(false);
145         TEST_CHANGE(false);
146 
147         printf("\nNow program should finish... :)\n");
148         test.resume();
149 
150         return 0;
151 }
\end{verbatim}
\normalsize