fork download
  1. #include <iostream>
  2. #include <atomic>
  3. #include <memory>
  4. #include <thread>
  5. #include <mutex>
  6. #include <chrono>
  7. #include <list>
  8. #include <string>
  9.  
  10. using namespace std::chrono_literals;
  11.  
  12. namespace test
  13. {
  14. // Taken from boost examples
  15. class SpinLock
  16. {
  17. public:
  18. SpinLock() : m_state(Unlocked) {}
  19.  
  20. void lock()
  21. {
  22. while (m_state.exchange(Locked, std::memory_order_acquire) == Locked)
  23. {
  24. /* busy-wait and no yield */
  25. }
  26. }
  27. void unlock()
  28. {
  29. m_state.store(Unlocked, std::memory_order_release);
  30. }
  31.  
  32. private:
  33. enum LockState
  34. {
  35. Locked,
  36. Unlocked
  37. };
  38.  
  39. std::atomic<LockState> m_state;
  40. };
  41.  
  42. template<typename T>
  43. class ObjectHolder
  44. {
  45. public:
  46. using Holder = std::shared_ptr<T>;
  47. ObjectHolder() = default;
  48. explicit ObjectHolder(T* ptr)
  49. : m_object(ptr)
  50. {
  51. }
  52. ~ObjectHolder() = default;
  53.  
  54. Holder Lock() const
  55. {
  56. std::lock_guard<SpinLock> lg(m_guard);
  57. return m_object;
  58. }
  59.  
  60. void Reset(T* obj = nullptr)
  61. {
  62. Holder newHolder(obj);
  63. {
  64. std::lock_guard<SpinLock> lg(m_guard);
  65. m_object.swap(newHolder);
  66. }
  67. }
  68.  
  69. void Reset(Holder newHolder)
  70. {
  71. std::lock_guard<SpinLock> lg(m_guard);
  72. m_object.swap(newHolder);
  73. }
  74.  
  75. private:
  76. mutable SpinLock m_guard;
  77. Holder m_object;
  78. };
  79. } // test_with_mutex
  80.  
  81. int main()
  82. {
  83. std::cout << "Hello from TestSharedPointer. sizeof(m) = " << std::endl;
  84.  
  85. volatile bool stop = false;
  86. std::list<std::unique_ptr<std::thread>> threads;
  87. test::ObjectHolder<std::string> obj;
  88.  
  89.  
  90. for (int n = 0; n < 10; ++ n)
  91. {
  92. threads.push_back(std::make_unique<std::thread>([n, &obj, &stop]()
  93. {
  94. int idx = 0;
  95. for (; !stop; ++ idx)
  96. {
  97. obj.Reset(std::make_shared<std::string>(std::to_string(n) + "_" + std::to_string(idx)));
  98. }
  99. }));
  100. }
  101.  
  102. threads.push_back(std::make_unique<std::thread>([&obj, &stop]()
  103. {
  104. int i = 0;
  105. while(!stop)
  106. {
  107. auto sp = obj.Lock();
  108. if (sp)
  109. std::cout << (++i) << ", " << *sp << std::endl;
  110.  
  111. std::this_thread::sleep_for(10ms);
  112. }
  113. }));
  114.  
  115. std::this_thread::sleep_for(5s);
  116. stop = true;
  117.  
  118. for (auto &item: threads)
  119. item->join();
  120. }
  121.  
Success #stdin #stdout 4.98s 33176KB
stdin
Standard input is empty
stdout
Hello from TestSharedPointer. sizeof(m) = 
1, 9_5416
2, 3_14463
3, 2_5991
4, 9_11322
5, 4_14773
6, 8_20537
7, 6_16000
8, 6_21588
9, 0_21678
10, 3_34907
11, 5_26438
12, 0_27796
13, 9_32516
14, 6_37346
15, 4_42255
16, 1_38973
17, 8_44139
18, 4_48228
19, 2_45275
20, 7_45892
21, 4_54370
22, 1_59552
23, 6_69423
24, 2_66595
25, 1_67156
26, 7_77506
27, 8_74191
28, 9_76663
29, 7_83291
30, 3_97937
31, 5_95878
32, 6_104145
33, 1_91645
34, 4_105233
35, 2_99586
36, 8_102981
37, 1_105684
38, 8_116324
39, 0_105484
40, 1_111782
41, 8_124214
42, 7_126748
43, 2_123299
44, 4_132792
45, 7_132553
46, 0_119205
47, 1_125980
48, 8_140394
49, 4_138502
50, 3_150705
51, 7_148207
52, 9_153981
53, 2_143955
54, 5_153808
55, 9_160644
56, 1_152604
57, 4_156252
58, 6_173051
59, 5_177555
60, 1_175893
61, 9_187025
62, 6_187522
63, 5_185051
64, 9_194954
65, 7_190945
66, 4_180210
67, 1_191957
68, 9_203091
69, 4_188456
70, 3_203098
71, 9_210170
72, 4_196558
73, 5_209482
74, 9_218112
75, 3_214827
76, 1_211250
77, 0_199155
78, 3_220873
79, 1_218223
80, 5_224827
81, 3_227777
82, 4_215990
83, 8_237908
84, 5_232501
85, 9_242651
86, 4_221823
87, 3_242371
88, 9_248220
89, 7_244056
90, 5_246363
91, 9_253931
92, 4_233807
93, 8_261458
94, 7_258074
95, 2_249479
96, 1_256008
97, 9_267879
98, 5_263162
99, 1_262136
100, 8_276878
101, 2_265259
102, 0_250589
103, 8_284740
104, 2_273112
105, 9_289753
106, 1_279758
107, 2_280553
108, 5_288624
109, 1_287825
110, 8_307702
111, 9_302591
112, 0_270327
113, 8_313424
114, 6_310421
115, 2_297926
116, 3_283730
117, 9_320483
118, 0_282916
119, 7_316423
120, 2_309907
121, 4_296643
122, 7_324482
123, 2_315941
124, 8_337273
125, 7_334192
126, 5_330292
127, 4_304275
128, 9_344885
129, 5_336320
130, 3_317660
131, 5_342407
132, 2_337782
133, 4_312201
134, 5_350251
135, 2_345863
136, 0_324200
137, 8_370367
138, 4_324517
139, 1_356436
140, 3_333049
141, 4_332174
142, 6_370675
143, 0_339677
144, 4_338251
145, 3_340768
146, 7_381482
147, 1_378771
148, 3_349020
149, 7_388812
150, 8_402271
151, 6_393360
152, 2_384956
153, 0_363092
154, 6_399185
155, 7_400152
156, 4_359901
157, 6_405273
158, 7_405103
159, 2_401062
160, 3_375591
161, 7_421691
162, 6_417359
163, 2_408782
164, 5_429403
165, 6_429392
166, 3_387130
167, 7_437270
168, 5_437693
169, 9_440540
170, 2_422926
171, 0_399044
172, 5_445291
173, 2_429070
174, 0_405091
175, 3_413588
176, 9_456474
177, 7_457658
178, 3_419772
179, 9_472822
180, 3_425252
181, 7_481833
182, 5_473317
183, 9_482536
184, 8_489391
185, 6_474336
186, 9_490547
187, 2_472423
188, 8_497299
189, 1_470783
190, 2_478102
191, 7_503575
192, 1_476787
193, 3_454616
194, 9_506481
195, 1_483892
196, 8_520259
197, 6_504824
198, 9_512664
199, 7_520214
200, 5_517774
201, 9_520465
202, 4_469110
203, 5_525266
204, 9_528082
205, 6_524582
206, 7_535782
207, 2_515039
208, 4_480789
209, 2_523234
210, 1_530891
211, 4_488148
212, 8_558993
213, 3_498834
214, 1_536589
215, 8_566679
216, 4_496092
217, 6_553781
218, 3_510463
219, 8_574677
220, 1_552566
221, 9_574676
222, 1_558481
223, 3_522513
224, 5_581969
225, 6_578790
226, 3_528564
227, 2_565938
228, 6_584740
229, 3_534612
230, 7_594488
231, 6_592773
232, 0_526450
233, 7_600645
234, 2_581893
235, 0_533610
236, 7_606386
237, 1_588169
238, 7_614537
239, 8_626867
240, 5_618903
241, 3_560521
242, 9_624249
243, 0_547165
244, 3_568004
245, 9_632198
246, 2_620434
247, 3_576006
248, 9_637674
249, 5_640374
250, 6_640859
251, 8_655564
252, 0_568324
253, 3_583623
254, 7_653773
255, 4_585412
256, 0_575030
257, 3_589420
258, 8_678173
259, 4_592473
260, 5_661767
261, 7_669245
262, 3_602106
263, 6_677822
264, 7_677152
265, 0_590899
266, 8_695771
267, 3_613553
268, 4_613643
269, 8_702826
270, 3_619485
271, 1_658177
272, 8_708944
273, 3_627433
274, 4_620719
275, 9_692544
276, 2_685077
277, 4_627849
278, 6_709973
279, 2_692973
280, 4_633307
281, 9_708540
282, 2_700641
283, 0_624692
284, 9_715319
285, 2_707881
286, 4_646917
287, 9_720798
288, 5_724424
289, 2_714711
290, 9_728420
291, 0_641693
292, 8_753720
293, 4_660478
294, 1_705755
295, 4_666406
296, 3_675378
297, 8_768052
298, 1_716924
299, 3_682152
300, 6_761840
301, 5_754041
302, 0_660697
303, 9_758731
304, 1_729171
305, 7_777756
306, 5_770100
307, 8_794058
308, 4_706126
309, 6_785542