-
Notifications
You must be signed in to change notification settings - Fork 28
/
testefc.cpp
10503 lines (8804 loc) · 233 KB
/
testefc.cpp
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#define TEST_BOOST_SHARED_PTR 0 //0-efc::shared_ptr | 1-boost::shared_ptr
#define _S(s) #s
#if TEST_BOOST_SHARED_PTR
#include <boost/shared_ptr.hpp> //for test boost::shared_ptr
#define spnamespace boost
#else
#define spnamespace efc
#define shared_ptr sp
#endif
//#include <memory> //for test std::shared_ptr
#include "es_main.h"
#include "Efc.hh"
#define LOG(fmt,...) ESystem::out->printfln(fmt, ##__VA_ARGS__)
namespace efc {
es_alogger_t* alogger;
}
#define ALOG(fmt,...) {if (alogger) {eso_alogger_logfmt(alogger, fmt, ##__VA_ARGS__);}}
static EAtomicLLong gLockTestCount1;
static EAtomicLLong gLockTestCount2;
static EAtomicLLong gSumAdd;
static EAtomicLLong gSumPull;
static llong gDeadlineTimestamp = 0L;
class Count {
static volatile int count1;// = 1;
static volatile int count2;// = 1;
public:
int getcount1() {
return count1;
}
int getcount2() {
return count2;
}
void addcount1() {
eso_atomic_add_and_fetch32(&count1, 1);
}
void addcount2() {
eso_atomic_add_and_fetch32(&count2, 1);
}
};
volatile int Count::count1 = 1;
volatile int Count::count2 = 1;
static void test_null(void) {
EInteger* i1 = NULL;
if (i1 == null) {
LOG("i1 == null");
}
EInteger* i2 = null;
if (i2 == NULL) {
LOG("i2 == NULL");
}
if (null == NULL) {
LOG("null == NULL");
}
if (null != NULL) {
LOG("null != NULL");
}
if (NULL == null) {
LOG("NULL == null");
}
if (NULL != null) {
LOG("NULL != null");
}
}
static void test_interface() {
interface IA : virtual public EObject {
virtual void func() = 0;
};
class A : virtual public IA {
public:
void func() {
LOG("A::func()");
}
virtual int hashCode() {
return 1;
}
virtual boolean equals(EObject* obj) {
return true;
}
};
class AA : virtual public EObject, public A {
public:
virtual int hashCode() {
return 2;
}
virtual boolean equals(EObject* obj) {
return false;
}
};
AA aa1, aa2;
if (aa1.equals(&aa2)) {
LOG("aa1==aa2");
}
LOG("aa1.toString():%s", aa1.toString().c_str());
A *a1, *a2;
a1 = &aa1;
a2 = &aa2;
if (a1->equals(a2)) {
LOG("a1==a2");
}
}
static void test_vc6bug() {
//class A {
//};
class AAAA : public EThread {
public:
AAAA() {
}
virtual void run() {
}
};
}
template <typename T>
class TraitTest {
public:
TraitTest(T t): t_(t) {
LOG("111 - %s", t_.c_str());
}
T& get() {
return t_;
}
private:
T t_;
};
template <typename T>
class TraitTest<T*> {
public:
TraitTest(T* t): t_(t) {
LOG("222 - %s", t_->c_str());
}
T* get() {
return t_;
}
private:
T* t_;
};
template <typename T>
class TraitTest<sp<T> > {
public:
TraitTest(sp<T> t): t_(t) {
LOG("333 - %s", t_->c_str());
}
sp<T> get() {
return t_;
}
private:
sp<T> t_;
};
static void test_traits() {
TraitTest<EString*> test1(new EString("p"));
delete test1.get();
TraitTest<EString> test2(EString::valueOf("str"));
TraitTest<sp<EString> > test3(new EString("sp"));
}
static void f_string(const EString& s) {
LOG("s=%s", s.c_str());
}
static void test_string() {
{
EString s("0123456789");
int n = s.indexOf('\0');
LOG("n=%d", n);
ES_ASSERT(n==-1);
n = s.lastIndexOf('\0');
LOG("n=%d", n);
ES_ASSERT(n==-1);
n = s.indexOf("\0");
LOG("n=%d", n);
ES_ASSERT(n==-1);
n = s.lastIndexOf("\0");
LOG("n=%d", n);
ES_ASSERT(n==-1);
}
{
ullong v = -1;
EString vs(v);
LOG(vs.c_str());
}
LOG((EString("xxx") + 2 + '\t' + "xxxxxxxxx").c_str());
//string join
{
EString x_y("xxx" + EString("|") + 10 + "yyy");
LOG(x_y.c_str());
x_y = "ccc" + EString("yyy");
LOG(x_y.c_str());
x_y = EString("yyy") + "ccc";
LOG(x_y.c_str());
x_y = EString("yyy") + 9;
LOG(x_y.c_str());
x_y = EString("yyy") + 'c';
LOG(x_y.c_str());
EString x_y2("222 | ");
x_y2 << &x_y << " | ";
x_y2 += &x_y;
EString xx_yy(x_y + "| " + &x_y2);
LOG(xx_yy.c_str());
EString s_1("s");
s_1 << 'x';
s_1 << 10;
EString s_2 = s_1 + 'y' + 9;
LOG(s_2.c_str());
}
//as hex
{
byte buf[10];
eso_memcpy(buf + 5, "\5", 1);
EString hex = EString::toHexString(buf, sizeof(buf));
LOG("hex=%s", hex.c_str());
}
f_string("sssssssssssssss");
EString str("127.0.0.1");
{
EString x1("x");
x1<<"y"<<1020;
LOG("x1=%s", x1.c_str());
}
int i;
//1.
{
EArray<EString*> arr = EPattern::split("\\.", str.c_str(), 0);
LOG("arr.size=%d", arr.size());
for (i=0; i<arr.size(); i++) {
LOG("s=%s", arr[i]->c_str());
}
EString a = str.splitAt(".", 1);
LOG("a=%s", a.c_str());
EString b = str.splitAt(".", 4);
LOG("b=%s", b.c_str());
EPattern pattern("\\.");
EArray<EString*> arr2 = pattern.split(str.c_str());
LOG("arr2.size=%d", arr2.size());
for (i = 0; i < arr2.size(); i++) {
LOG("s=%s", arr2[i]->c_str());
}
}
//2.
{
EString str2("AAAAAAAAA");
str << "++++++" << "abc" << str2 << 'X';
LOG("s=%s", str.c_str());
}
//3.
{
str.append(88823728, 16);
LOG("s=%s", str.c_str());
str.append(9.88327832);
LOG("s=%s", str.c_str());
EString s = "abcd'efg +++fdjska";
s = s.substring(0, 10);
int n = s.lastIndexOf("cd", 2);
LOG(s.substring(n).c_str());
boolean r = s.regionMatches(true, 2, "Cd", 0, 2);
if (r) {
LOG("matched 'Cd'!");
}
}
//4.
{
EString x("xxx");
for (int i=0; i<10000; i++) {
x.append("xxxxxx");
}
}
//5. null
{
EString n5(null);
LOG("n5=%s", n5.c_str());
}
//6. pointer
{
EString s = "x";
EString p6(&s);
LOG("p6=%s", p6.c_str());
}
}
static void test_simpleMap() {
ESimpleMap map(true, false);
map.put("1", new EString("aaa"));
map.put("1", new EString("bbb"));
map.put("1", new EString("ccc"));
map.put("2", new EString("ddd"));
ESimpleEnumeration* e = map.elements();
for (; e && e->hasMoreElements();) {
es_emap_elem_t elem;
e->nextElement(&elem);
EString* s = dynamic_cast<EString*>(elem.data);
LOG("s=%s", s->c_str());
}
EString* aaa = dynamic_cast<EString*>(map.get("1"));
LOG("1=%s", aaa->c_str());
EString* bbb = dynamic_cast<EString*>(map.get("1", 1));
LOG("1=%s", bbb->c_str());
EString* ccc = dynamic_cast<EString*>(map.get("1", -1));
LOG("1=%s", ccc->c_str());
}
static void test_random() {
ERandom random;
int n = random.nextInt();
LOG("nextInt %d", n);
n = random.nextInt();
LOG("nextInt %d", n);
n = random.nextInt();
LOG("nextInt %d", n);
n = random.nextInt();
LOG("nextInt %d", n);
n = random.nextInt();
LOG("nextInt %d", n);
n = random.nextInt();
LOG("nextInt %d", n);
n = random.nextInt();
LOG("nextInt %d", n);
double d = random.nextDouble();
LOG("nextDouble %.20lf", d);
EA<byte> bb1(10);
random.nextBytes(&bb1);
for (int i=0; i<bb1.length(); i++) {
printf(" %d ", bb1.getAt(i));
}
LOG("");
byte bb2[10] = {0};
random.nextBytes(bb2, sizeof(bb2));
for (int i=0; i<sizeof(bb2); i++) {
printf(" %d ", bb2[i]);
}
LOG("");
}
static void test_secureRandom() {
ESecureRandom sr;
double d = sr.nextDouble();
LOG("d=%lf", d);
}
static void test_lock(int flag) {
//1. ESynchronizeable
class A : public ESynchronizeable
{
public:
void testLock(int flag) {
for (int i=0; i<10; i++) {
SYNCHRONIZED(this) {
printf("SYNCHRONIZED 1 ##%d\n", flag);
SYNCHRONIZED(this) {
printf("SYNCHRONIZED 2 ##%d\n", flag);
//if (i == 5) continue; //!
if (i == 5) break; //!
printf("i=%d\n", i);
}}
}}
}
printf("end of testLock()\n");
}
};
A a;
a.testLock(flag);
printf("\n");
//2. EReentrantLock
ELock* lock = new EReentrantLock();
lock->lock();
printf("lock1\n");
lock->lock();
printf("lock2\n");
lock->unlock();
lock->unlock();
delete lock;
printf("\n");
//3. ESpinLock
lock = new ESpinLock();
lock->lock();
printf("lock1\n");
#if 0
lock->lock(); //blocked!
printf("lock2\n");
lock->unlock();
#endif
lock->unlock();
delete lock;
printf("test_lock(int) end.\n");
}
class ErrHandler: public EThread::UncaughtExceptionHandler {
public:
void uncaughtException(EThread* t, EThrowable* e) {
LOG("This is:%s,Message:%s", t->getName(), e->getMessage());
e->printStackTrace();
}
};
class XXX : public ESynchronizeable {
public:
void printf() {
// LOG("xxx");
}
};
static void test_thread() {
class TestThread: public EThread {
public:
TestThread(const char* name) : EThread(name) {
v = 0;
}
virtual ~TestThread() {
LOG("TestThread::~TestThread()...");
}
virtual void run() {
while (v < 10) {
EThread* current = EThread::currentThread();
if (current != this) {
printf("current thread: %s\n", current->getName());
}
llong n = gLockTestCount1.incrementAndGet();
if (n % 999999 == 0) {
printf("TestThread_%s....%lld.\n", getName(), n);
}
v++;
}
// printf("this thread: %s\n", this->getName());
}
private:
int v;
};
// printf("test_thread start.\n");
#if 0
TestThread t1("#1"), t2("#2"), t3("#3");
t1.start();
t2.start();
t3.start();
t1.join();
t2.join();
t3.join();
#else
EArray<EThread*> arr;
int i;
for (i = 0; i < 1000; i++) {
TestThread* pct = new TestThread(EString(i).c_str());
pct->start();
arr.add(pct);
}
for (i = 0; i < arr.length(); i++) {
arr.getAt(i)->join();
}
#endif
// printf("test_thread end.\n");
}
class XX : public EThreadLocal {
public:
~XX() {
// removeAll();
}
protected:
void destructor(EInteger* v) {
delete v;
}
};
class TestThread: public EThread
{
public:
static EReentrantLock *lock_;
ELock *lock2_;
public:
TestThread(const char *name, EThreadLocalVariable<XX, EInteger>* threadLocal1, EThreadLocalVariable<XX, EInteger>* threadLocal2, ELock* lock) : EThread(name){
v = 0;
this->threadLocal1 = threadLocal1;
this->threadLocal2 = threadLocal2;
this->lock2_ = lock;
}
virtual ~TestThread() {
}
virtual void run() {
try {
delete threadLocal1->set(new EInteger(1));
delete threadLocal2->set(new EInteger(8888888));
while(v < 100) {
EThread* current = EThread::currentThread();
// LOG("current thread: #%d, %s", eso_os_thread_current(), current->getName());
if (1) {
// SYNCBLOCK(TestThread::lock_) {
// ECondition* cond_ = TestThread::lock_->newCondition();
// delete cond_;
// }}
SYNCBLOCK(lock2_) {
//
}}
// XXX xxx;
// SYNCHRONIZED(&xxx) {
// try {
// SYNCHRONIZED(&xxx) {
// xxx.printf();
// }}
// } catch(...) {
// }
// }}
}
//====
#if 0
#if 1
LOG("thread#%s v=%d", this->getName(), v++);
if (v%12 == 1 && eso_strcmp(this->getName(), "#1")==0) {
LOG("thread#%s sleep begin...", this->getName());
EThread::sleep(2000);
LOG("thread#%s sleep end.", this->getName());
}
#else
test_lock((int)EThread::currentThread());
#endif
#else
EInteger* i = threadLocal1->get();
i->value++;
// LOG("thread#%d i1=%d", eso_os_thread_current(), i->intValue());
delete threadLocal1->set(new EInteger(i->value));
EInteger* i2 = threadLocal2->get();
i2->value++;
// LOG("thread#%d i2=%d", eso_os_thread_current(), i2->intValue());
// long c = threadLocal2->get();
// threadLocal2->set(++c);
// LOG("thread#%d c=%d", eso_os_thread_current(), c);
llong n = gLockTestCount1.incrementAndGet();
if (n % 999999 == 0) {
printf("XXX_%s....%d.\n", this->getName(), v);
}
v++;
#endif
}
// threadLocal1->remove(); //!free this thread local object.
} catch (EThrowable& e) {
e.printStackTrace();
throw e;
}
// printf("end of thread.\n");
}
private:
int v;
EThreadLocalVariable<XX, EInteger>* threadLocal1;
EThreadLocalVariable<XX, EInteger>* threadLocal2;
};
EReentrantLock* TestThread::lock_ = new EReentrantLock();
static void test_thread1() {
EThreadLocalVariable<XX, EInteger>* threadLocal1 = new EThreadLocalVariable<XX, EInteger>();
EThreadLocalVariable<XX, EInteger>* threadLocal2 = new EThreadLocalVariable<XX, EInteger>();
// {
ErrHandler handle;
EThread::setDefaultUncaughtExceptionHandler(&handle);
EReentrantLock lock;
#if 0
TestThread t1("#1", threadLocal1, threadLocal2, &lock), t2("#2", threadLocal1, threadLocal2, &lock), t3("#3", threadLocal1, threadLocal2, &lock);
t1.setUncaughtExceptionHandler(&handle);
t1.start();
t2.start();
t3.start();
t1.join();
t2.join();
t3.join();
#else
EArray<EThread*> arr;
int i;
for (i = 0; i < 5; i++) {
TestThread* pct = new TestThread(EString(i).c_str(), threadLocal1, threadLocal2, &lock);
pct->start();
arr.add(pct);
}
for (i = 0; i < arr.length(); i++) {
arr.getAt(i)->join();
}
#endif
// TestThread t1("#1");
// t1.start();
// t1.join();
// }
delete threadLocal1;
delete threadLocal2;
// LOG("after thread join.");
}
static void test_thread2() {
class AAAA : public EThread {
public:
AAAA() {
}
virtual void run() {
for (int i=0; i<100; i++) {
LOG("i=%d", i);
EThread::sleep(10);
}
}
};
sp<AAAA> t = new AAAA();
EThread::setDaemon(t, true); //success!
t->start();
//EThread::setDaemon(t, true); //error!
t->join();
// EThread::sleep(10);
LOG("end of test_thread2()");
}
static void test_thread3() {
struct X {
EReentrantLock mainLock;
// ESimpleLock mainLock;
ECondition* termination;
EAtomicCounter aliveCount0;
EAtomicCounter aliveCount1;
volatile int state; //0=init | 1-shutdown | 2-terminated
X() {
termination = mainLock.newCondition();
}
~X() {
delete termination;
}
} x;
class AAAA : public EThread {
private:
X& x;
public:
AAAA(X& x) : x(x) {
}
virtual ~AAAA() {
LOG("AAAA::~AAAA()...t=%p", this);
}
virtual void run() {
x.aliveCount1++;
// for (int i=0; i<100000; i++) {
//// printf("i=%d\n", i);
//// EThread::sleep(10000);
// }
LOG(EString::formatOf("thread %p finished", this).c_str());
}
static void test_thread3_exit_callback(void* arg) {
AAAA *t = static_cast<AAAA *>(arg);
LOG("thread %p, t->name=%s", t, t->getName());
SYNCBLOCK(&t->x.mainLock) {
t->x.aliveCount0--;
t->x.aliveCount1--;
int m = t->x.aliveCount0.value();
int n = t->x.aliveCount1.value();
if (t->x.state == 1 && m == 0 && n == 0) {
t->x.state = 2;
t->x.termination->signalAll();
// printf("signalAll()...\n");
}
// printf("state=%d, m=%d, n=%d\n", t->x.state, m, n);
}}
// EThread::sleep(500);
}
};
EArrayList<AAAA*> arr;
x.state = 0;
for (int i=0; i<50; i++) {
AAAA *t = new AAAA(x);
arr.add(t);
t->injectExitCallback(AAAA::test_thread3_exit_callback, t);
x.aliveCount0++;
t->start();
}
SYNCBLOCK(&x.mainLock) {
x.state = 1;
for (;;) {
if (x.aliveCount0.value() == 0) {
break;
}
x.termination->await();
}
}}
for (int i=0; i<arr.size(); i++) {
AAAA* t = arr.getAt(i);
t->join();
}
llong n = gLockTestCount1.incrementAndGet();
if (n >= 4096) {
EThread::sleep(10);
}
LOG("end of test_thread3()...");
}
static void test_thread4() {
struct X {
EReentrantLock mainLock;
// ESimpleLock mainLock;
ECondition* termination;
EAtomicCounter aliveCount0;
EAtomicCounter aliveCount1;
volatile int state; //0=init | 1-shutdown | 2-terminated
X() {
termination = mainLock.newCondition();
}
~X() {
delete termination;
}
} x;
class AAAA : public EThread {
private:
X& x;
public:
AAAA(X& x) : x(x) {
}
virtual ~AAAA() {
LOG("AAAA::~AAAA()...t=%p", static_cast<EThread*>(this));
}
virtual void run() {
x.aliveCount1++;
// for (int i=0; i<100000; i++) {
//// printf("i=%d\n", i);
//// EThread::sleep(10000);
// }
LOG(EString::formatOf("thread %p finished", static_cast<EThread*>(this)).c_str());
}
static void test_thread3_exit_callback(void* arg) {
AAAA *t = static_cast<AAAA *>(arg);
LOG("thread %p, t->name=%s", t, t->getName());
SYNCBLOCK(&t->x.mainLock) {
t->x.aliveCount0--;
t->x.aliveCount1--;
int m = t->x.aliveCount0.value();
int n = t->x.aliveCount1.value();
if (t->x.state == 1 && m == 0 && n == 0) {
t->x.state = 2;
t->x.termination->signalAll();
// printf("signalAll()...\n");
}
// printf("state=%d, m=%d, n=%d\n", t->x.state, m, n);
}}
// EThread::sleep(500);
}
};
x.state = 0;
for (int i=0; i<5; i++) {
AAAA* t = new AAAA(x);
t->injectExitCallback(AAAA::test_thread3_exit_callback, t);
EThread::setDaemon(t, true);
x.aliveCount0++;
t->start();
}
SYNCBLOCK(&x.mainLock) {
x.state = 1;
for (;;) {
if (x.aliveCount0.value() == 0) {
break;
}
x.termination->await();
}
}}
EThread::sleep(500); //FIXME: wait all thread destroyed.
llong n = gLockTestCount1.incrementAndGet();
if (n >= 4096) {
EThread::sleep(10);
}
LOG("end of test_thread4()...");
}
static void test_thread5() {
class MyThread : public EThread, public enable_shared_from_this<MyThread> {
public:
virtual ~MyThread() {
LOG("%012s", getName());
}
virtual void run() {
sp<MyThread> self = shared_from_this();
class MyThread2 : public EThread {
public:
sp<MyThread> self;
virtual ~MyThread2() {
LOG("%012s", getName());
}
MyThread2(sp<MyThread> s) : self(s) {
}
virtual void run() {
self->getName();
}
};
sp<MyThread2> thread = new MyThread2(self);
thread->setName(EString(gSumAdd.addAndGet(1)).c_str());
EThread::setDaemon(thread, true); //!!!
thread->start();
}
};
sp<MyThread> ths1 = new MyThread();
ths1->setName(EString(gSumAdd.addAndGet(1)).c_str());
EThread::setDaemon(ths1, true); //!!!
ths1->start();
LOG("end of test_thread5().");
}
static void test_threadJoin() {
class T1 : public EThread {
public:
virtual void run() {
// EThread::sleep(2000);
LOG("t1 running...");
}
};
T1 t1;
t1.start();
class T2 : public EThread {
public:
T2(T1* t1) {
this->t1 = t1;
}
virtual void run() {
// EThread::sleep(500);
t1->join();
LOG("after join at t2");
}
private:
T1* t1;
};
T2 t2(&t1);
t2.start();
t1.join();
t2.join();
// printf("t1 cv=%d\n", t1.parkCount.value());
// printf("t2 cv=%d\n", t2.parkCount.value());
LOG("after join at main");
}
static void test_threadState() {
class MyThread: public EThread {
private:
ELock* lock;
public:
MyThread(ELock* lock) {
this->lock = lock;
}
virtual void run() {
try {
LOG("sleep...");
EThread::sleep(1000);
LOG("...");
lock->lock();
LOG("get lock.");
lock->unlock();
} catch (EInterruptedException& e) {
LOG("%s%s", EThread::currentThread()->getName(),
" interrupted.");
}
}
};
EReentrantLock* lock = new EReentrantLock();
EThread *t1 = new MyThread(lock);
t1->start();
LOG("thread stat=%d", t1->getState());
lock->lock();
EThread::sleep(10);
LOG("thread stat0=%d", t1->getState());
EThread::sleep(2000);
LOG("thread stat1=%d", t1->getState());
EThread::sleep(2000);
t1->interrupt();
LOG("thread stat2=%d", t1->getState());
EThread::sleep(2000);
LOG("thread stat3=%d", t1->getState());
lock->unlock();
t1->join();
delete t1;