File: thread2.cpp

package info (click to toggle)
libcommoncpp2 1.8.1-10
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 5,300 kB
  • sloc: cpp: 29,221; sh: 10,352; ansic: 1,134; makefile: 238; xml: 5
file content (56 lines) | stat: -rw-r--r-- 932 bytes parent folder | download | duplicates (7)
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
#include <cc++/thread.h>
#include <cstdio>
#include <cstring>
#include <iostream>

#ifdef	CCXX_NAMESPACES
using namespace std;
using namespace ost;
#endif

// Test child thread destroying before father
//
class Child: public Thread
{
public:
	Child()
	{ }
	void run() {
		cout << "child start" << endl;
		Thread::sleep(3000);
		cout << "child end" << endl;
	}
	void final() {
//		delete this;
	}
};

class Father: public Thread
{
public:
	Father()
	{ }
	void run() {
		cout << "starting child thread" << endl;
		Thread *th = new Child();
		th->detach();
		Thread::sleep(1000);
		cout << "father end" << endl;
	}
	void final() {
		// delete this; - not used since detached threads self delete
		// reset memory to test access violation
		memset(this,0,sizeof(*this));
	}
};

int main(int argc, char* argv[])
{
	cout << "starting father thread" << endl;
	Father *th = new Father();
	th->start();
	Thread::sleep(10000);

	return 0;
}