Emeric Vigier | 2f62582 | 2012-08-06 11:09:52 -0400 | [diff] [blame] | 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 | cout << "child start" << endl; |
| 20 | Thread::sleep(3000); |
| 21 | cout << "child end" << endl; |
| 22 | } |
| 23 | void final() { |
| 24 | // delete this; |
| 25 | } |
| 26 | }; |
| 27 | |
| 28 | class Father: public Thread |
| 29 | { |
| 30 | public: |
| 31 | Father() |
| 32 | { } |
| 33 | void run() { |
| 34 | cout << "starting child thread" << endl; |
| 35 | Thread *th = new Child(); |
| 36 | th->detach(); |
| 37 | Thread::sleep(1000); |
| 38 | cout << "father end" << endl; |
| 39 | } |
| 40 | void final() { |
| 41 | // delete this; - not used since detached threads self delete |
| 42 | // reset memory to test access violation |
| 43 | memset(this,0,sizeof(*this)); |
| 44 | } |
| 45 | }; |
| 46 | |
| 47 | int main(int argc, char* argv[]) |
| 48 | { |
| 49 | cout << "starting father thread" << endl; |
| 50 | Father *th = new Father(); |
| 51 | th->start(); |
| 52 | Thread::sleep(10000); |
| 53 | |
| 54 | return 0; |
| 55 | } |
| 56 | |