source: xplcommon/test/testthread.cc@ 39:f9c840ac038d

Last change on this file since 39:f9c840ac038d was 35:f8a5b321d0c3, checked in by István Váradi <ivaradi@…>, 12 years ago

Added support for detached threads

File size: 3.8 KB
RevLine 
[35]1// Copyright (c) 2012 by István Váradi
2
3// This file is part of libxplcommon, a common utility library for
4// development related to X-Plane
5
6// Redistribution and use in source and binary forms, with or without
7// modification, are permitted provided that the following conditions are met:
8
9// 1. Redistributions of source code must retain the above copyright notice, this
10// list of conditions and the following disclaimer.
11// 2. Redistributions in binary form must reproduce the above copyright notice,
12// this list of conditions and the following disclaimer in the documentation
13// and/or other materials provided with the distribution.
14
15// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
19// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
26// The views and conclusions contained in the software and documentation are those
27// of the authors and should not be interpreted as representing official policies,
28// either expressed or implied, of the FreeBSD Project.
29
30//------------------------------------------------------------------------------
31
32#include <xplcommon/Thread.h>
33#include <xplcommon/Waiter.h>
34#include <xplcommon/LocalServerSocket.h>
35#include <xplcommon/LocalAcceptor.h>
36#include <xplcommon/LocalSocket.h>
37#include <xplcommon/LocalClientSocket.h>
38#include <xplcommon/LocalConnector.h>
39#include <xplcommon/DataStream.h>
40#include <xplcommon/WaitableEvent.h>
41#include <xplcommon/PseudoRandom.h>
42
43#include <cstdlib>
44#include <cstdio>
45#include <cassert>
46
47#include <signal.h>
48
49#include <sys/time.h>
50
51//------------------------------------------------------------------------------
52
53using xplcommon::Thread;
54using xplcommon::Waiter;
55using xplcommon::LocalServerSocket;
56using xplcommon::LocalAcceptor;
57using xplcommon::LocalSocket;
58using xplcommon::LocalClientSocket;
59using xplcommon::LocalConnector;
60using xplcommon::BufferedStream;
61using xplcommon::DataStream;
62using xplcommon::PseudoRandom;
63
64using std::min;
65
66//------------------------------------------------------------------------------
67
68class TestThread : public Thread
69{
70private:
71 unsigned long id;
72
73public:
74 TestThread(unsigned long id);
75
76 virtual void run();
77};
78
79//------------------------------------------------------------------------------
80
81inline TestThread::TestThread(unsigned long id) :
82 Thread(true),
83 id(id)
84{
85}
86
87//------------------------------------------------------------------------------
88
89void TestThread::run()
90{
91 printf("TestThread: id=%lu\n", id);
92}
93
94//------------------------------------------------------------------------------
95//------------------------------------------------------------------------------
96
97int main()
98{
99 static const size_t numThreads = 1000;
100
101 TestThread** threads = new TestThread*[numThreads];
102
103 unsigned long id = 1;
104 while(true) {
105 for(size_t i = 0; i<numThreads; ++i) {
106 threads[i] = new TestThread(id++);
107 threads[i]->start();
108 }
109
110 Thread::sleep(2000);
111
112 // for(size_t i = 0; i<numThreads; ++i) {
113 // delete threads[i];
114 // }
115 }
116
117 return 0;
118}
119
120//------------------------------------------------------------------------------
121
122// Local Variables:
123// mode: C++
124// c-basic-offset: 4
125// indent-tabs-mode: nil
126// End:
Note: See TracBrowser for help on using the repository browser.