Fixes to PDirectory
[pwlib.git] / samples / sortedlist / SortedListTest.cxx
blob79f168d77a55be0b0191d8276d76879a45ae0946
2 #include <ptlib.h>
3 #include "SortedListTest.h"
4 #include <ptclib/random.h>
7 PCREATE_PROCESS(SortedListTest);
9 PMutex coutMutex;
11 SortedListTest::SortedListTest():PProcess("Reitek S.p.A.", "SortedListTest", 0, 0, BetaCode, 0)
15 void SortedListTest::Main() {
17 PINDEX i;
18 for (i = 0; i < 10; i++) {
19 new DoSomeThing(i);
22 for (PINDEX j = i; j < i + 5; j++) {
23 new DoSomeThing2(j);
26 Suspend();
30 DoSomeThing::DoSomeThing(PINDEX _index)
31 :PThread(1000, AutoDeleteThread, NormalPriority, psprintf("DoSomeThing %u", _index)), index(_index) {
32 Resume();
36 void DoSomeThing::Main() {
38 list.AllowDeleteObjects();
40 PRandom rand(PRandom::Number());
42 PINDEX i;
44 for (i = 0; i < 5000; i++) {
45 PString * p = new PString(rand.Generate());
46 list.Append(p);
47 // coutMutex.Wait();
48 // cout << GetThreadName() << ": Added " << *p << " element to sorted list" << endl;
49 // coutMutex.Signal();
52 for (;;) {
54 PINDEX remove = rand.Generate() % (list.GetSize() + 1);
55 for (i = 0; i < remove; i++) {
56 PINDEX index = rand.Generate() % list.GetSize();
57 coutMutex.Wait();
58 cout << GetThreadName() << ": Removing element " << list[index] << " at index position " << index << endl;
59 coutMutex.Signal();
60 if (index%2)
61 list.Remove(&list[index]);
62 else
63 list.RemoveAt(index);
66 PINDEX add = rand.Generate() % 1000 + 300;
67 for (i = 0; i < add; i++) {
68 PString * p = new PString(rand.Generate());
69 coutMutex.Wait();
70 cout << GetThreadName() << ": Adding element " << *p << "to sorted list" << endl;
71 coutMutex.Signal();
72 list.Append(p);
78 PSafeString::PSafeString(const PString & _string):string(_string) {
82 void PSafeString::PrintOn(ostream &strm) const {
83 strm << string;
87 DoSomeThing2::DoSomeThing2(PINDEX _index)
88 :PThread(1000, AutoDeleteThread, NormalPriority, psprintf("DoSomeThing2 %u", _index)), index(_index) {
89 Resume();
93 void DoSomeThing2::Main() {
95 PRandom rand(PRandom::Number());
97 PINDEX i;
99 for (i = 0; i < 5000; i++) {
100 PSafeString * p = new PSafeString(rand.Generate());
101 list.Append(p);
102 // coutMutex.Wait();
103 // cout << GetThreadName() << ": Added " << *p << " element to sorted list" << endl;
104 // coutMutex.Signal();
107 for (;;) {
109 PINDEX remove = rand.Generate() % (list.GetSize() + 1);
110 for (i = 0; i < remove; i++) {
111 PINDEX index = rand.Generate() % list.GetSize();
112 coutMutex.Wait();
113 PSafePtr<PSafeString> str = list.GetWithLock(index, PSafeReference);
114 cout << GetThreadName() << ": Removing element " << *str << " at index position " << index << endl;
115 coutMutex.Signal();
116 list.Remove(&(*str));
119 PINDEX add = rand.Generate() % 1000 + 300;
120 for (i = 0; i < add; i++) {
121 PSafeString * p = new PSafeString(rand.Generate());
122 coutMutex.Wait();
123 cout << GetThreadName() << ": Adding element " << *p << "to sorted list" << endl;
124 coutMutex.Signal();
125 list.Append(p);
128 list.DeleteObjectsToBeRemoved();