Sophie

Sophie

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

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

\section{thread2.cpp}


\footnotesize\begin{verbatim}1 #include <cc++/thread.h>
2 #include <cstdio>
3 #include <cstring>
4 #include <iostream>
5 
6 #ifdef  CCXX_NAMESPACES
7 using namespace std;
8 using namespace ost;
9 #endif
10 
11 // Test child thread destroying before father
12 // 
13 class Child: public Thread
14 {
15 public:
16         Child()
17         { }
18         void run()
19         {
20                 cout << "child start" << endl;
21                 Thread::sleep(3000);
22                 cout << "child end" << endl;
23         }
24         void final()
25         {
26 //              delete this;
27         }
28 };
29 
30 class Father: public Thread
31 {
32 public:
33         Father()
34         { }
35         void run()
36         {
37                 cout << "starting child thread" << endl;
38                 Thread *th = new Child();
39                 th->detach();
40                 Thread::sleep(1000);
41                 cout << "father end" << endl;
42         }
43         void final()
44         {
45                 // delete this; - not used since detached threads self delete
46                 // reset memory to test access violation
47                 memset(this,0,sizeof(*this));
48         }
49 };
50 
51 int main(int argc, char* argv[])
52 {
53         cout << "starting father thread" << endl;
54         Father *th = new Father();
55         th->start();
56         Thread::sleep(10000);
57 
58         return 0;
59 }
60 
\end{verbatim}
\normalsize