mirror of
https://github.com/boostorg/thread.git
synced 2026-02-03 09:42:16 +00:00
Compare commits
949 Commits
boost-1.25
...
boost-1.53
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4735c2f81f | ||
|
|
8416d3e8bb | ||
|
|
8b89d21814 | ||
|
|
8804ab8e73 | ||
|
|
0ba92626f0 | ||
|
|
ce46c31555 | ||
|
|
e160cf0254 | ||
|
|
67da33a182 | ||
|
|
d07835908f | ||
|
|
e618900149 | ||
|
|
e012b8abb2 | ||
|
|
8ee9ed1904 | ||
|
|
565a022a5a | ||
|
|
0f7d406b9f | ||
|
|
4d5474d600 | ||
|
|
233484f254 | ||
|
|
ad3247dd29 | ||
|
|
0ae81b8d4c | ||
|
|
05f02e1476 | ||
|
|
23ea174056 | ||
|
|
ce19b13c43 | ||
|
|
8262d61ff5 | ||
|
|
4d013af927 | ||
|
|
2d8852199b | ||
|
|
12a1c7c71c | ||
|
|
89e944914b | ||
|
|
7bc8c437ab | ||
|
|
c4420d7591 | ||
|
|
6ef2dade3a | ||
|
|
c594f5d9ae | ||
|
|
a73cf83971 | ||
|
|
67a5a6f39e | ||
|
|
348bd080ef | ||
|
|
bca4c3d5fa | ||
|
|
9e6384bf79 | ||
|
|
dcd544082e | ||
|
|
1013ce68c1 | ||
|
|
aea84b03fc | ||
|
|
7608db4b0e | ||
|
|
62f1c4b086 | ||
|
|
ab6864f3b4 | ||
|
|
b8bd80a5f4 | ||
|
|
fffc5ddd1f | ||
|
|
cd4c858048 | ||
|
|
3fb1bd3d1d | ||
|
|
75aff7f1a6 | ||
|
|
3f3c5c8ac5 | ||
|
|
40b66a83f7 | ||
|
|
0b15d66dc0 | ||
|
|
ceacda6c6a | ||
|
|
0a22ac9209 | ||
|
|
ba1d54d672 | ||
|
|
b5a18dc71a | ||
|
|
4a0141511d | ||
|
|
f0a6e49984 | ||
|
|
f60e1d1230 | ||
|
|
625db78abf | ||
|
|
b470ffdb56 | ||
|
|
78095afdbc | ||
|
|
e83cdc6721 | ||
|
|
7ba000fe45 | ||
|
|
cae6a36c19 | ||
|
|
8549895373 | ||
|
|
efa907881e | ||
|
|
744cae8270 | ||
|
|
28899243b1 | ||
|
|
267288ba19 | ||
|
|
3487d05fac | ||
|
|
031186a8e6 | ||
|
|
f970c9fddc | ||
|
|
e9ceaaa2bb | ||
|
|
0e7c436df5 | ||
|
|
745e23f2c3 | ||
|
|
1f7b8a6583 | ||
|
|
b2790c6df5 | ||
|
|
f5e3c1c348 | ||
|
|
ba955f003e | ||
|
|
75e0ffbbce | ||
|
|
9168dd7d1d | ||
|
|
ee5f871f1a | ||
|
|
c896c8fda4 | ||
|
|
0e895a1e28 | ||
|
|
d95081094f | ||
|
|
011dda9816 | ||
|
|
34c377328f | ||
|
|
382204f702 | ||
|
|
19aed55e52 | ||
|
|
9d1e1fb64a | ||
|
|
d75cda0cc3 | ||
|
|
a4c7f68320 | ||
|
|
547431da0b | ||
|
|
c3fdc098fe | ||
|
|
6ed276190d | ||
|
|
59265265d9 | ||
|
|
8d9370b005 | ||
|
|
fd97c6e7a7 | ||
|
|
297da0745f | ||
|
|
cb231e02a6 | ||
|
|
298b51fefa | ||
|
|
80befa1c94 | ||
|
|
11dce20534 | ||
|
|
56bd079f96 | ||
|
|
733b49ae42 | ||
|
|
08ed4c4201 | ||
|
|
09d5125278 | ||
|
|
6e1a3f3c27 | ||
|
|
5b3743d9a6 | ||
|
|
ec135b1b8e | ||
|
|
60e34cff11 | ||
|
|
da8dc9f5aa | ||
|
|
27aa44acb5 | ||
|
|
ff1d051359 | ||
|
|
85e32534fa | ||
|
|
bc49b7d03a | ||
|
|
ee97068208 | ||
|
|
0f7e069dd9 | ||
|
|
3c3c6d5efe | ||
|
|
f163054557 | ||
|
|
fb80bea056 | ||
|
|
abad5c3028 | ||
|
|
d0c164220d | ||
|
|
34b1a86a68 | ||
|
|
06f5da7f5b | ||
|
|
1e80ccb8d3 | ||
|
|
8ad34a689a | ||
|
|
a421e10e3b | ||
|
|
1c4b42bb95 | ||
|
|
bba3be457b | ||
|
|
3abfbb8ba1 | ||
|
|
331a35070c | ||
|
|
99ad690382 | ||
|
|
4301b21702 | ||
|
|
fceab582fe | ||
|
|
e8a4ed40a5 | ||
|
|
b698c1437b | ||
|
|
14cea92e06 | ||
|
|
3a8e04cac6 | ||
|
|
5b01721440 | ||
|
|
aad2b35ac9 | ||
|
|
f8371daeb8 | ||
|
|
74519977fd | ||
|
|
c0bea158d4 | ||
|
|
70686b4913 | ||
|
|
14502dd715 | ||
|
|
8ee986536b | ||
|
|
8ed82798d2 | ||
|
|
ebfe10b7df | ||
|
|
4aa26180ca | ||
|
|
89496448d9 | ||
|
|
dfa0a3979a | ||
|
|
396cd7db4f | ||
|
|
0351d59060 | ||
|
|
6f0b0e976d | ||
|
|
dd5d687014 | ||
|
|
5d8b0891bd | ||
|
|
7dc95f63d3 | ||
|
|
a7adc5865b | ||
|
|
cdbe5766fa | ||
|
|
b1cac0731c | ||
|
|
fc8de511c6 | ||
|
|
defdb8ff1c | ||
|
|
9a08a8478f | ||
|
|
a6f7a0180b | ||
|
|
5a59df4476 | ||
|
|
1df9d7c575 | ||
|
|
65008b11f7 | ||
|
|
b18314878a | ||
|
|
11a951c679 | ||
|
|
c67e3ff7b9 | ||
|
|
e5a633cc41 | ||
|
|
3724d847cf | ||
|
|
b6063b5c60 | ||
|
|
09362f0eac | ||
|
|
32b3f3f569 | ||
|
|
0d08362291 | ||
|
|
9f120a80a7 | ||
|
|
2eb6fd754e | ||
|
|
9f4a8973d0 | ||
|
|
a4d9355060 | ||
|
|
5a7545afbd | ||
|
|
8affa33718 | ||
|
|
4d25ea1760 | ||
|
|
6ec8e2ccec | ||
|
|
97d0ae6527 | ||
|
|
50a74d0eda | ||
|
|
f9e03b5eaa | ||
|
|
ad571bd898 | ||
|
|
de8ef9aee4 | ||
|
|
21f75da2f6 | ||
|
|
233dbf8075 | ||
|
|
d8f1ba9b3d | ||
|
|
1241f18215 | ||
|
|
b6604882eb | ||
|
|
a9c9d5c499 | ||
|
|
9a827d937e | ||
|
|
38594f889a | ||
|
|
04c17e45b3 | ||
|
|
730a8de024 | ||
|
|
7eac2fe3e4 | ||
|
|
267243d959 | ||
|
|
f587262c8e | ||
|
|
ebf458dbd0 | ||
|
|
f64b5559dd | ||
|
|
2ddcd5f678 | ||
|
|
cac715937a | ||
|
|
39f43feb11 | ||
|
|
58d65b17ea | ||
|
|
4314f0cac3 | ||
|
|
d4da369930 | ||
|
|
6f1876b618 | ||
|
|
c6e872ceb0 | ||
|
|
72d809819f | ||
|
|
dd09ef3362 | ||
|
|
aa7941fae2 | ||
|
|
35af4a8f35 | ||
|
|
a01fd3dd76 | ||
|
|
d220da89d1 | ||
|
|
55c75e9299 | ||
|
|
616ea87a0a | ||
|
|
319ba2fe75 | ||
|
|
d79eeff779 | ||
|
|
26d38748db | ||
|
|
5c124234bb | ||
|
|
b991c9a8a0 | ||
|
|
681af396b8 | ||
|
|
cac0eaa6c3 | ||
|
|
a64fa2c18f | ||
|
|
f07640850b | ||
|
|
ab665c8c56 | ||
|
|
e43586ffac | ||
|
|
9cc243837d | ||
|
|
de7e3baabc | ||
|
|
7ec9804540 | ||
|
|
5e29afcb57 | ||
|
|
0a1085d9be | ||
|
|
0439d53704 | ||
|
|
7c9116af2e | ||
|
|
5ac2ff4521 | ||
|
|
8565a3e472 | ||
|
|
3648bc8cb0 | ||
|
|
70584af9c0 | ||
|
|
73121eda9d | ||
|
|
381554f8bc | ||
|
|
768e92b0e9 | ||
|
|
98333b7dcf | ||
|
|
4dc1cb1ba1 | ||
|
|
4e0007780c | ||
|
|
506019dd62 | ||
|
|
10f0c3e08e | ||
|
|
fa2950a04b | ||
|
|
ebfb62ca49 | ||
|
|
96023e81af | ||
|
|
e30be60bc4 | ||
|
|
7bfafec128 | ||
|
|
9c07d0ff5d | ||
|
|
e12d2bc486 | ||
|
|
72a85b396c | ||
|
|
87786091bb | ||
|
|
784494274b | ||
|
|
68012dd92c | ||
|
|
a37d2a1364 | ||
|
|
e40be775fe | ||
|
|
64e6924132 | ||
|
|
4bbf47086d | ||
|
|
7c674bc255 | ||
|
|
cc662c102c | ||
|
|
6b9a2d791b | ||
|
|
4551e8759b | ||
|
|
9442976bdb | ||
|
|
8d07df176f | ||
|
|
4b22aff33e | ||
|
|
93dee254d0 | ||
|
|
a29b598205 | ||
|
|
e3b20eaae9 | ||
|
|
d369fb0f94 | ||
|
|
d816bca42f | ||
|
|
d6bb11c4e9 | ||
|
|
2fdcefac05 | ||
|
|
044c3cc11e | ||
|
|
bd9223b525 | ||
|
|
347703dab2 | ||
|
|
f9a0e450e1 | ||
|
|
f6b8cdd1f5 | ||
|
|
6727013302 | ||
|
|
cda12a2660 | ||
|
|
c3c2072472 | ||
|
|
bfc226fdc0 | ||
|
|
fd28e1a7fb | ||
|
|
b11911f5e5 | ||
|
|
a1587d070f | ||
|
|
df2f43bc61 | ||
|
|
895e8eea52 | ||
|
|
97d6249f3b | ||
|
|
7a8ed98eb5 | ||
|
|
d611eece19 | ||
|
|
a99320f5a4 | ||
|
|
c97484943a | ||
|
|
547d9bd844 | ||
|
|
1a65aab05a | ||
|
|
2e869aeb86 | ||
|
|
d729776575 | ||
|
|
895c436405 | ||
|
|
4ae2932792 | ||
|
|
a52be2bdbb | ||
|
|
31c4792216 | ||
|
|
39fd9c0b47 | ||
|
|
9c25df3402 | ||
|
|
fb150b5038 | ||
|
|
8cff3a167e | ||
|
|
2be1431f60 | ||
|
|
255b7ed7f6 | ||
|
|
58fd27399e | ||
|
|
65d2898ff0 | ||
|
|
5f88ba1e47 | ||
|
|
ab569461d8 | ||
|
|
7093fc670b | ||
|
|
6f2b030253 | ||
|
|
0e61e679af | ||
|
|
b40998e1b5 | ||
|
|
174d701bc3 | ||
|
|
f2143d08b9 | ||
|
|
1273e2620d | ||
|
|
c719f6e37e | ||
|
|
37922d8ce0 | ||
|
|
7b79a31f40 | ||
|
|
9a09406f77 | ||
|
|
9bdb778478 | ||
|
|
9621dafe46 | ||
|
|
d7c9837844 | ||
|
|
9087fd904d | ||
|
|
27bb7803ae | ||
|
|
66ac6942b6 | ||
|
|
c0e1086f2c | ||
|
|
ffa751c617 | ||
|
|
b8ad60a2d6 | ||
|
|
5db0aac816 | ||
|
|
20980fe54d | ||
|
|
3fae7c5184 | ||
|
|
fb54acfe69 | ||
|
|
47889a8f22 | ||
|
|
8d22c3869b | ||
|
|
0e69edd066 | ||
|
|
235ed4afe0 | ||
|
|
627cb7f774 | ||
|
|
09021af350 | ||
|
|
31c280d1fa | ||
|
|
629f344f34 | ||
|
|
db5f924e24 | ||
|
|
9be3eb282a | ||
|
|
9255a035f4 | ||
|
|
effd891a16 | ||
|
|
fbdc23f482 | ||
|
|
8ab0d5acdd | ||
|
|
13db35cbf5 | ||
|
|
0f2d480e3c | ||
|
|
9edc61e37b | ||
|
|
f4dab6aac5 | ||
|
|
5af323102a | ||
|
|
9e0550d140 | ||
|
|
0997fad8ec | ||
|
|
0d1701c509 | ||
|
|
f2f62f93ea | ||
|
|
8a329f66fb | ||
|
|
05d4c52918 | ||
|
|
8fd0dd0cc0 | ||
|
|
8eea5811ba | ||
|
|
a154c2adab | ||
|
|
10bf4ed576 | ||
|
|
60d12dd395 | ||
|
|
b4e9be3c52 | ||
|
|
dcebae6d4a | ||
|
|
8749696538 | ||
|
|
0d776bcd26 | ||
|
|
2d6ed47cf2 | ||
|
|
9beea23f63 | ||
|
|
ea06434425 | ||
|
|
6508eff95e | ||
|
|
69930684a9 | ||
|
|
b1931a3eda | ||
|
|
63b44d4e32 | ||
|
|
f7cb8d8141 | ||
|
|
48c857e02c | ||
|
|
2978d43a5d | ||
|
|
a264766584 | ||
|
|
442dc58e0f | ||
|
|
25460c652c | ||
|
|
31a98f0a1e | ||
|
|
36c44b6f45 | ||
|
|
27426b18d1 | ||
|
|
3ea9ce1c8c | ||
|
|
f03a9bfcf3 | ||
|
|
4dfc636c84 | ||
|
|
5fe4312c6c | ||
|
|
63e675a6bb | ||
|
|
e92aeac7d7 | ||
|
|
f1f7eac1f2 | ||
|
|
eff0c84553 | ||
|
|
58c8ce61c7 | ||
|
|
6ac5e6953a | ||
|
|
5d9ad59af2 | ||
|
|
3c48a05437 | ||
|
|
4462124ff2 | ||
|
|
373f557ef7 | ||
|
|
495e561398 | ||
|
|
d24a579033 | ||
|
|
77130424b4 | ||
|
|
eb30688937 | ||
|
|
880bac0633 | ||
|
|
60fdcddcb5 | ||
|
|
851d6a987f | ||
|
|
9bebd7b35f | ||
|
|
309acb9597 | ||
|
|
a56887167e | ||
|
|
e984dff4e4 | ||
|
|
685e4d446b | ||
|
|
8af680f307 | ||
|
|
6c60cce60d | ||
|
|
5882a675bb | ||
|
|
a5e95845b3 | ||
|
|
5b83d81e40 | ||
|
|
c8e5ad564d | ||
|
|
5edfa273ff | ||
|
|
4db57bcb10 | ||
|
|
3f13340903 | ||
|
|
6abb53c9d3 | ||
|
|
fdd20a519e | ||
|
|
67cc49f333 | ||
|
|
31a34cd0b5 | ||
|
|
ef8c08ba99 | ||
|
|
2991ca6c6f | ||
|
|
52bace18b2 | ||
|
|
767d14ae4f | ||
|
|
1a5c911e36 | ||
|
|
6e42a04e43 | ||
|
|
28be2cfeef | ||
|
|
8be168fd87 | ||
|
|
eee95fef57 | ||
|
|
9ea179b052 | ||
|
|
6868280409 | ||
|
|
e00b764454 | ||
|
|
999613c686 | ||
|
|
c2661d7eb5 | ||
|
|
4d21dd1f47 | ||
|
|
a0a0e57527 | ||
|
|
d8af0d0b4e | ||
|
|
113288e3b0 | ||
|
|
afecfd7c2d | ||
|
|
94d89aac5f | ||
|
|
8831b13efc | ||
|
|
01f99da03a | ||
|
|
080654e3ef | ||
|
|
2ac2eb2a61 | ||
|
|
61b940b705 | ||
|
|
4a4f87e017 | ||
|
|
6d5e7f63a7 | ||
|
|
f77285f375 | ||
|
|
dc5d03a6dc | ||
|
|
ea0961b7f6 | ||
|
|
33d9f9774c | ||
|
|
86097fa038 | ||
|
|
70d9dbc45a | ||
|
|
3926fd3a20 | ||
|
|
7861cf1146 | ||
|
|
0516b86a6e | ||
|
|
ec735d3e9b | ||
|
|
1c5c070983 | ||
|
|
a5c02b73dc | ||
|
|
918b920670 | ||
|
|
de67d2e27e | ||
|
|
bc89df04cb | ||
|
|
c26a4cf082 | ||
|
|
6e1a866b13 | ||
|
|
f91986ad0d | ||
|
|
795cc23f3e | ||
|
|
a3695bd4a0 | ||
|
|
08dc521daf | ||
|
|
8b916d21b1 | ||
|
|
c40f47a78a | ||
|
|
e9fb470b06 | ||
|
|
343d049772 | ||
|
|
86f9480da4 | ||
|
|
8696b610ca | ||
|
|
6f13227eda | ||
|
|
525d190f91 | ||
|
|
58d5110e61 | ||
|
|
1e0154335b | ||
|
|
76e53c7bc5 | ||
|
|
413c29a5e4 | ||
|
|
30bb6143c1 | ||
|
|
cfb08be1a8 | ||
|
|
b5bbb7fb1c | ||
|
|
a76c33f8cc | ||
|
|
991ac727c6 | ||
|
|
810306b8f3 | ||
|
|
6c22bdb3bd | ||
|
|
569a78649f | ||
|
|
6a0d3e98bc | ||
|
|
7caec1ec33 | ||
|
|
7fd3fb48b1 | ||
|
|
3809321037 | ||
|
|
eef695bdf0 | ||
|
|
ab01ab1e4d | ||
|
|
a32a3b37db | ||
|
|
c8d8a108a7 | ||
|
|
7afd9efcc5 | ||
|
|
56ded87ad2 | ||
|
|
82e503339b | ||
|
|
713d0c7ace | ||
|
|
25ad6e3f8f | ||
|
|
df0197b617 | ||
|
|
a89c4f01ad | ||
|
|
ae67099633 | ||
|
|
57542d3a5c | ||
|
|
9a1da14116 | ||
|
|
ed050d753d | ||
|
|
8bec363710 | ||
|
|
7c68e190a9 | ||
|
|
7ebf5ea3d1 | ||
|
|
11e0435a4b | ||
|
|
d15ee57cd1 | ||
|
|
56d660b7fd | ||
|
|
792958e693 | ||
|
|
914e67dc04 | ||
|
|
b50a7ccb61 | ||
|
|
f827709d42 | ||
|
|
36abb42175 | ||
|
|
40f3b1b4c8 | ||
|
|
4f35e25688 | ||
|
|
270e88edd7 | ||
|
|
5ded171247 | ||
|
|
88f6076f3c | ||
|
|
b4d12e08dd | ||
|
|
332dd988e4 | ||
|
|
bce8db41d7 | ||
|
|
f6fd70245d | ||
|
|
4ff0a055d6 | ||
|
|
c9140267a5 | ||
|
|
72fcee4e5e | ||
|
|
9c8e512edd | ||
|
|
3c191af34a | ||
|
|
5e0b2d7370 | ||
|
|
5994abd453 | ||
|
|
67a2d119c0 | ||
|
|
114215088a | ||
|
|
a78e2b793e | ||
|
|
519ed3834e | ||
|
|
22647135fa | ||
|
|
58c741e9ca | ||
|
|
ef9083089e | ||
|
|
5de1582a0a | ||
|
|
39c864e31f | ||
|
|
320cb63df4 | ||
|
|
c246222ded | ||
|
|
b7edb2873c | ||
|
|
89f2032c0d | ||
|
|
d2f8230093 | ||
|
|
9f6b5d169a | ||
|
|
e56708d4aa | ||
|
|
304156c20e | ||
|
|
31e1566e1d | ||
|
|
3908637056 | ||
|
|
abee301f3d | ||
|
|
9b1d3f8f3c | ||
|
|
3513eaf701 | ||
|
|
08a840afe4 | ||
|
|
370f5d461c | ||
|
|
8efc8458e1 | ||
|
|
6485717c52 | ||
|
|
1d5bbd11a8 | ||
|
|
bc403742b5 | ||
|
|
c7f963f57e | ||
|
|
afb6684bde | ||
|
|
ee3d772235 | ||
|
|
1af08f7085 | ||
|
|
ccf23fa273 | ||
|
|
f701defc5f | ||
|
|
c606f05bf8 | ||
|
|
a646153615 | ||
|
|
60380afe15 | ||
|
|
d4b0a977c9 | ||
|
|
f86156ad10 | ||
|
|
1836ee854f | ||
|
|
c37cdeec9f | ||
|
|
b0b2b17908 | ||
|
|
2918732481 | ||
|
|
5a4d5ddb9d | ||
|
|
55afcf678d | ||
|
|
16c7cf9b5e | ||
|
|
432bd29c1c | ||
|
|
a87914ef23 | ||
|
|
041530a953 | ||
|
|
9d4c55161a | ||
|
|
a706d1df00 | ||
|
|
b15b2e666f | ||
|
|
5d4678364e | ||
|
|
1c0f470032 | ||
|
|
9590526430 | ||
|
|
1c6dfda83c | ||
|
|
a8be12940e | ||
|
|
4b5046366b | ||
|
|
a0fff90c26 | ||
|
|
a8daedac5e | ||
|
|
5fa26fb3ac | ||
|
|
ea3e297175 | ||
|
|
92b8789532 | ||
|
|
8f61694057 | ||
|
|
67f7de5305 | ||
|
|
6faecefb73 | ||
|
|
68c5bd44e8 | ||
|
|
3656277053 | ||
|
|
19846ff356 | ||
|
|
a11bd6ebd9 | ||
|
|
db2aaa04fd | ||
|
|
9889bf50a2 | ||
|
|
b48f9aa609 | ||
|
|
d75fb2deda | ||
|
|
7915ab1ec6 | ||
|
|
f0faf88d66 | ||
|
|
7dd7537f5f | ||
|
|
f51680e8d9 | ||
|
|
a6bc072c6d | ||
|
|
85f2508157 | ||
|
|
ebb6c8d637 | ||
|
|
ddc83e270c | ||
|
|
0173148a2e | ||
|
|
69a4ec6c00 | ||
|
|
2d52219af2 | ||
|
|
6355a5b28d | ||
|
|
1f87a9e4c0 | ||
|
|
ba8afde42b | ||
|
|
93f677cba6 | ||
|
|
595bbee41e | ||
|
|
dfd865d67d | ||
|
|
cb3f3a1f64 | ||
|
|
96a04402db | ||
|
|
0e44838905 | ||
|
|
78e644c7c1 | ||
|
|
89cc7fc34e | ||
|
|
974754598e | ||
|
|
87acbb406d | ||
|
|
597517157c | ||
|
|
a0b816be8c | ||
|
|
64cd268fc7 | ||
|
|
4a056924d2 | ||
|
|
d5a81f990c | ||
|
|
f048dd81f2 | ||
|
|
5746f2214c | ||
|
|
099af669d4 | ||
|
|
79cac706a7 | ||
|
|
df229074ac | ||
|
|
191c27e856 | ||
|
|
e5ee01b43c | ||
|
|
c46b040f6f | ||
|
|
da8c92f057 | ||
|
|
866b33c808 | ||
|
|
182daf0b17 | ||
|
|
2552febc2a | ||
|
|
eb9db9b683 | ||
|
|
11dbdfca4d | ||
|
|
f49de9ec10 | ||
|
|
3a7e569a65 | ||
|
|
c376c1a62a | ||
|
|
72e4794f5b | ||
|
|
fbbc52063a | ||
|
|
78b4fe3d07 | ||
|
|
c30b65a0ea | ||
|
|
b8c8b250b1 | ||
|
|
b26d01c8d7 | ||
|
|
1cb08ff60c | ||
|
|
4dbd8a66af | ||
|
|
cb4d739fd1 | ||
|
|
11f913e8fb | ||
|
|
0b6054a919 | ||
|
|
e7620a1050 | ||
|
|
d3d7fd9317 | ||
|
|
acf0f97663 | ||
|
|
34bd87cea7 | ||
|
|
228f11262e | ||
|
|
811a03f281 | ||
|
|
9683e0f1cc | ||
|
|
2528bd0b8f | ||
|
|
ed587be470 | ||
|
|
674ae6d571 | ||
|
|
690d44e2e6 | ||
|
|
55b48874a4 | ||
|
|
720ccdb474 | ||
|
|
a556ff6560 | ||
|
|
33c0af8253 | ||
|
|
86072f95ac | ||
|
|
c7b96bcd7d | ||
|
|
572c18302f | ||
|
|
efd1bdec23 | ||
|
|
ba86f9ff13 | ||
|
|
56b07cb5c0 | ||
|
|
358e32e98f | ||
|
|
01297016bd | ||
|
|
64b5b67661 | ||
|
|
b6f0ec7fd9 | ||
|
|
e9c0b5e0c5 | ||
|
|
4a005ea288 | ||
|
|
9658b69af4 | ||
|
|
e3c9446e29 | ||
|
|
aa240e61d9 | ||
|
|
2954e932ce | ||
|
|
5be79cc858 | ||
|
|
4a9d97d22d | ||
|
|
f4f3433854 | ||
|
|
26bffa3740 | ||
|
|
69e52a9882 | ||
|
|
cc8de48849 | ||
|
|
9d7c119f94 | ||
|
|
6ba9fd1b60 | ||
|
|
fb6250eb94 | ||
|
|
bc73368c96 | ||
|
|
3068f0c62c | ||
|
|
8e00803c83 | ||
|
|
087b69b629 | ||
|
|
3b237267fb | ||
|
|
b9dbb1ed45 | ||
|
|
41d3b29ec0 | ||
|
|
05ceb8b1e2 | ||
|
|
80d3925b8d | ||
|
|
2cd6cbeacc | ||
|
|
6382846f6c | ||
|
|
349d0fd74b | ||
|
|
9c88855bf4 | ||
|
|
f0e6cdfcb5 | ||
|
|
af9864a1b5 | ||
|
|
8ac145e667 | ||
|
|
39f7afc7d0 | ||
|
|
113b974bb7 | ||
|
|
c747a6ff4e | ||
|
|
107d11cfd5 | ||
|
|
a4d2cd94b9 | ||
|
|
3d9fb84fc9 | ||
|
|
e500bc075e | ||
|
|
25e8fa0e11 | ||
|
|
5f27fb2607 | ||
|
|
d977cedb78 | ||
|
|
454b58cdf0 | ||
|
|
82a632b0f9 | ||
|
|
43cbe3f1f8 | ||
|
|
d027cec8a6 | ||
|
|
e53c2c52ee | ||
|
|
5ff0ecc12d | ||
|
|
9c1f421ccb | ||
|
|
66850bc057 | ||
|
|
33da34b4bf | ||
|
|
792cd49310 | ||
|
|
37fdb5e2b0 | ||
|
|
88cd251db7 | ||
|
|
4038d18fc8 | ||
|
|
59bf92a183 | ||
|
|
57879155d2 | ||
|
|
96d43cebc0 | ||
|
|
8e13857b29 | ||
|
|
8c6e454697 | ||
|
|
4c7c7df89b | ||
|
|
515e6d8635 | ||
|
|
bbd941e2df | ||
|
|
3edba1bf19 | ||
|
|
4ad99d8242 | ||
|
|
c0aeaecc14 | ||
|
|
792be9e687 | ||
|
|
fd65337f43 | ||
|
|
9de9726e6f | ||
|
|
522037ca4a | ||
|
|
8fc3d1f718 | ||
|
|
cebaf27ee8 | ||
|
|
b62503f274 | ||
|
|
af50c640ab | ||
|
|
b5c5fbe0f5 | ||
|
|
b88ae8105e | ||
|
|
9ad04bb65e | ||
|
|
13bbaab1c4 | ||
|
|
09ca8d1728 | ||
|
|
9797a93d86 | ||
|
|
d29dae72de | ||
|
|
59fba2bff6 | ||
|
|
0350d4c501 | ||
|
|
d3e4a90e70 | ||
|
|
8ebb19fd18 | ||
|
|
02ddc33e6c | ||
|
|
410e8efeba | ||
|
|
e9f8e0bad9 | ||
|
|
f69e0313dc | ||
|
|
baa9b396d9 | ||
|
|
a82b0c516d | ||
|
|
43e2192aa2 | ||
|
|
4cd6453cac | ||
|
|
921d4c24c2 | ||
|
|
4fc7653b12 | ||
|
|
9d0e39a7c2 | ||
|
|
7aa979cf5b | ||
|
|
0aa50614d7 | ||
|
|
6f402c7362 | ||
|
|
2bf43a124d | ||
|
|
3573c53eda | ||
|
|
4546ec4ef7 | ||
|
|
2f7337aaf6 | ||
|
|
046698bcc2 | ||
|
|
06d7bf21d5 | ||
|
|
e7b9ccdf10 | ||
|
|
1e15b043a0 | ||
|
|
6c5f3d76e2 | ||
|
|
8679d6f6af | ||
|
|
f1c7d0f354 | ||
|
|
261e413500 | ||
|
|
094e41d7a7 | ||
|
|
e20299c8ee | ||
|
|
f8962b7ad2 | ||
|
|
c34f829c3e | ||
|
|
46264e4a4a | ||
|
|
096df68ea6 | ||
|
|
35f2055a1e | ||
|
|
e1353eefb3 | ||
|
|
4911a532bf | ||
|
|
96362e03aa | ||
|
|
049b4e09fe | ||
|
|
828c0e28af | ||
|
|
15a638edc0 | ||
|
|
fc8f1b1075 | ||
|
|
318a8e38c9 | ||
|
|
f0dbb02a9f | ||
|
|
649b777b76 | ||
|
|
6fad43670a | ||
|
|
e24b16229e | ||
|
|
21b4b81810 | ||
|
|
1096b1e28e | ||
|
|
03458fedef | ||
|
|
c1a2004344 | ||
|
|
2adb13a209 | ||
|
|
dba194ddb9 | ||
|
|
8179f041e6 | ||
|
|
a13c7a4d84 | ||
|
|
bbf92bb971 | ||
|
|
b33f413635 | ||
|
|
58ffb2bc16 | ||
|
|
0ed112631c | ||
|
|
9cfe8e9422 | ||
|
|
7d3fe72970 | ||
|
|
ac422138fa | ||
|
|
bf8746454a | ||
|
|
b61aa5b4ba | ||
|
|
c2bcd08168 | ||
|
|
48593b8868 | ||
|
|
83d4dc1831 | ||
|
|
0696f3cc41 | ||
|
|
515590495a | ||
|
|
7221bca909 | ||
|
|
ed64a8cd12 | ||
|
|
cbd30d22ff | ||
|
|
0c74dbd436 | ||
|
|
49356cc931 | ||
|
|
ceee6e8b17 | ||
|
|
61ab2754d2 | ||
|
|
2de3df61e8 | ||
|
|
b84d7aa06d | ||
|
|
ed48f900a3 | ||
|
|
d197a49707 | ||
|
|
b8ccaa3bf6 | ||
|
|
507a684b21 | ||
|
|
c969c9387a | ||
|
|
1709db4953 | ||
|
|
4d1c9ba316 | ||
|
|
45b0396355 | ||
|
|
1df2169e48 | ||
|
|
2c056b3621 | ||
|
|
680119006c | ||
|
|
c4ac4b7538 | ||
|
|
ff5d3b49ca | ||
|
|
e101c878f0 | ||
|
|
125193dcfa | ||
|
|
77efa9810d | ||
|
|
7f03d1917b | ||
|
|
55b4ca9350 | ||
|
|
7196a0f9d2 | ||
|
|
2caabde5ca | ||
|
|
137d7663c1 | ||
|
|
508b71a921 | ||
|
|
5d90820005 | ||
|
|
84727e90b1 | ||
|
|
9a1e3d3320 | ||
|
|
d33e0c8ee1 | ||
|
|
3332649480 | ||
|
|
c918b66199 | ||
|
|
dbbf56e17a | ||
|
|
c77500c15a | ||
|
|
75084aaa96 | ||
|
|
35714c8f1c | ||
|
|
3699cc97a6 | ||
|
|
5a7377acda | ||
|
|
6aaee629b5 | ||
|
|
b465fe569c | ||
|
|
5e6f72a688 | ||
|
|
51f80f6c15 | ||
|
|
45c314e594 | ||
|
|
cfce0892e0 | ||
|
|
05d1abf030 | ||
|
|
6c24a2626b | ||
|
|
870c75bd12 | ||
|
|
5fdd771708 | ||
|
|
06f39ac409 | ||
|
|
c92b0a2fb7 | ||
|
|
8a8d0e05ca | ||
|
|
74bae2baac | ||
|
|
4ba48676bd | ||
|
|
78480e7951 | ||
|
|
4cb9c412e8 | ||
|
|
75c83fed96 | ||
|
|
391de20ae0 | ||
|
|
1e2a9e8971 | ||
|
|
43cbd3a283 | ||
|
|
31cf6b5e64 | ||
|
|
99109ab78b | ||
|
|
a80d5f159d | ||
|
|
7ba4fc4aed | ||
|
|
9fb31e9868 | ||
|
|
3a2246de5b | ||
|
|
e7c4e2fa57 | ||
|
|
724ab285f0 | ||
|
|
d60e66fb00 | ||
|
|
97cdaca028 | ||
|
|
3044c8f905 | ||
|
|
2775a2a945 | ||
|
|
c43c1febba | ||
|
|
ceb6471d57 | ||
|
|
b99cc044f3 | ||
|
|
d91429dcec | ||
|
|
b1d1f7d8f1 | ||
|
|
86b608cf41 | ||
|
|
f263f75751 | ||
|
|
24bec05b86 | ||
|
|
ce1a5e9359 | ||
|
|
311525bc06 | ||
|
|
ecdfd96529 | ||
|
|
5aab32bc1a | ||
|
|
e152a1c6f2 | ||
|
|
e84fde78ec | ||
|
|
6ec4652bcf | ||
|
|
a5239c820b | ||
|
|
41b001b22c | ||
|
|
34e903b8b0 | ||
|
|
2b67e953fd | ||
|
|
aa0d3adf1d | ||
|
|
a1f57a8a80 | ||
|
|
6bc82a8580 | ||
|
|
c4c2e5d3a2 | ||
|
|
dd7d4b2173 |
@@ -1,49 +0,0 @@
|
||||
# (C) Copyright William E. Kempf 2001. Permission to copy, use, modify, sell and
|
||||
# distribute this software is granted provided this copyright notice appears
|
||||
# in all copies. This software is provided "as is" without express or implied
|
||||
# warranty, and with no claim as to its suitability for any purpose.
|
||||
#
|
||||
# Boost.Threads build and test Jamfile
|
||||
#
|
||||
# Declares the following targets:
|
||||
# 1. libboost_thread, a static link library.
|
||||
# 1a. On Win32, a dynamic link library libboost_threadmon,
|
||||
# which must be used in conjunction with libboost_thread.
|
||||
|
||||
# declare the location of this subproject relative to the root
|
||||
subproject libs/thread/build ;
|
||||
|
||||
#######################
|
||||
|
||||
#
|
||||
# Declare the Boost.Threads static link library.
|
||||
#
|
||||
|
||||
# For Win32 we need to build a special DLL, libboost_threadmon, to handle
|
||||
# TSS destruction.
|
||||
if $(NT)
|
||||
{
|
||||
if $(PTW32)
|
||||
{
|
||||
PTW32_REQUIREMENTS = <define>BOOST_HAS_PTHREADS <define>PtW32NoCatchWarn ;
|
||||
}
|
||||
else
|
||||
{
|
||||
dll libboost_threadmon : ../src/threadmon.cpp
|
||||
# requirements
|
||||
: <include>$(BOOST_ROOT)
|
||||
<threading>multi
|
||||
: debug release ;
|
||||
}
|
||||
}
|
||||
|
||||
# Base names of the source files for libboost_thread
|
||||
CPP_SOURCES =
|
||||
condition mutex recursive_mutex thread tss xtime once exceptions ;
|
||||
|
||||
lib libboost_thread : ../src/$(CPP_SOURCES).cpp
|
||||
# requirements
|
||||
: <include>$(BOOST_ROOT)
|
||||
$(PTW32_REQUIREMENTS)
|
||||
<threading>multi
|
||||
: debug release ;
|
||||
287
build/Jamfile.v2
Normal file
287
build/Jamfile.v2
Normal file
@@ -0,0 +1,287 @@
|
||||
# $Id$
|
||||
# Copyright 2006-2007 Roland Schwarz.
|
||||
# Copyright 2007 Anthony Williams
|
||||
# Copyright 2011-2012 Vicente J.Botet Escriba.
|
||||
# Distributed under the Boost Software License, Version 1.0. (See
|
||||
# accompanying file LICENSE_1_0.txt or copy at
|
||||
# http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#########################################################################
|
||||
# The boost threading library can be built on top of different API's
|
||||
# Currently this is the win32 API and the pthreads API.
|
||||
# Pthread is native on unix variants.
|
||||
# To get pthread on windows you need the pthread win32 library
|
||||
# http://sourceware.org/pthreads-win32 which is available under LGPL.
|
||||
#
|
||||
# You need to provide the include path and lib path in the variables
|
||||
# PTW32_INCLUDE and PTW32_LIB respectively. You can specify these
|
||||
# paths in site-config.jam, user-config.jam or in the environment.
|
||||
# A new feature is provided to request a specific API:
|
||||
# <threadapi>win32 and <threadapi)pthread.
|
||||
#
|
||||
# The naming of the resulting libraries is mostly the same for the
|
||||
# variant native to the build platform, i.e.
|
||||
# boost_thread and the boost specific tagging.
|
||||
# For the library variant that is not native on the build platform
|
||||
# an additional tag is applied:
|
||||
# boost_thread_pthread for the pthread variant on windows, and
|
||||
# boost_thread_win32 for the win32 variant (likely when built on cygwin).
|
||||
#
|
||||
# To request the pthread variant on windows, from boost root you would
|
||||
# say e.g:
|
||||
# bjam msvc-8.0 --with-thread install threadapi=pthread
|
||||
#########################################################################
|
||||
|
||||
import os ;
|
||||
import feature ;
|
||||
import indirect ;
|
||||
import path ;
|
||||
|
||||
project boost/thread
|
||||
: source-location ../src
|
||||
: requirements <threading>multi
|
||||
#<link>static:<define>BOOST_THREAD_STATIC_LINK=1
|
||||
#<link>shared:<define>BOOST_THREAD_DYN_LINK=1
|
||||
<link>static:<define>BOOST_THREAD_BUILD_LIB=1
|
||||
<link>shared:<define>BOOST_THREAD_BUILD_DLL=1
|
||||
-<tag>@$(BOOST_JAMROOT_MODULE)%$(BOOST_JAMROOT_MODULE).tag
|
||||
<tag>@$(__name__).tag
|
||||
<toolset>gcc:<cxxflags>-Wno-long-long
|
||||
#<define>BOOST_THREAD_THROW_IF_PRECONDITION_NOT_SATISFIED
|
||||
#<define>BOOST_SYSTEM_NO_DEPRECATED
|
||||
#<define>BOOST_THREAD_DONT_PROVIDE_INTERRUPTIONS
|
||||
|
||||
<library>/boost/system//boost_system
|
||||
#-pedantic -ansi -std=gnu++0x -Wextra -fpermissive
|
||||
<warnings>all
|
||||
<toolset>gcc:<cxxflags>-Wextra
|
||||
<toolset>gcc:<cxxflags>-pedantic
|
||||
<toolset>gcc:<cxxflags>-Wno-long-long
|
||||
#<toolset>gcc:<cxxflags>-ansi
|
||||
#<toolset>gcc:<cxxflags>-fpermissive
|
||||
|
||||
<toolset>darwin:<cxxflags>-Wextra
|
||||
<toolset>darwin:<cxxflags>-pedantic
|
||||
#<toolset>darwin:<cxxflags>-ansi
|
||||
<toolset>darwin:<cxxflags>-fpermissive
|
||||
<toolset>darwin:<cxxflags>-Wno-long-long
|
||||
|
||||
#<toolset>pathscale:<cxxflags>-Wextra
|
||||
<toolset>pathscale:<cxxflags>-Wno-long-long
|
||||
<toolset>pathscale:<cxxflags>-pedantic
|
||||
|
||||
<toolset>clang:<cxxflags>-Wextra
|
||||
<toolset>clang:<cxxflags>-pedantic
|
||||
#<toolset>clang:<cxxflags>-ansi
|
||||
#<toolset>clang:<cxxflags>-fpermissive
|
||||
<toolset>clang:<cxxflags>-Wno-long-long
|
||||
|
||||
<toolset>gcc-mingw-4.4.0:<cxxflags>-fdiagnostics-show-option
|
||||
<toolset>gcc-mingw-4.5.0:<cxxflags>-fdiagnostics-show-option
|
||||
<toolset>gcc-mingw-4.6.0:<cxxflags>-fdiagnostics-show-option
|
||||
<toolset>gcc-mingw-4.6.3:<cxxflags>-fdiagnostics-show-option
|
||||
<toolset>gcc-mingw-4.7.0:<cxxflags>-fdiagnostics-show-option
|
||||
<toolset>gcc-mingw-4.8.0:<cxxflags>-fdiagnostics-show-option
|
||||
|
||||
<toolset>darwin-4.6.2:<cxxflags>-Wno-delete-non-virtual-dtor
|
||||
<toolset>darwin-4.7.0:<cxxflags>-Wno-delete-non-virtual-dtor
|
||||
|
||||
#<toolset>clang-2.8:<cxxflags>-Wno-delete-non-virtual-dtor
|
||||
#<toolset>clang-2.8:<cxxflags>-Wno-unused-function
|
||||
#<toolset>clang-2.9:<cxxflags>-Wno-delete-non-virtual-dtor
|
||||
#<toolset>clang-2.9:<cxxflags>-Wno-unused-function
|
||||
<toolset>clang-3.0:<cxxflags>-Wno-delete-non-virtual-dtor
|
||||
#<toolset>clang-3.0:<cxxflags>-Wno-unused-function
|
||||
#<toolset>clang-3.0:<cxxflags>-Wno-unused-variable
|
||||
|
||||
# Note: Some of the remarks from the Intel compiler are disabled
|
||||
# remark #193: zero used for undefined preprocessing identifier "XXX"
|
||||
# remark #304: access control not specified ("public" by default)
|
||||
# remark #593: variable "XXX" was set but never used
|
||||
# remark #1418: external function definition with no prior declaration
|
||||
# remark #2415: variable "XXX" of static storage duration was declared but never referenced
|
||||
|
||||
<toolset>intel:<cxxflags>-wd193,304,383,444
|
||||
<toolset>intel:<cxxflags>-wd593,981
|
||||
<toolset>intel:<cxxflags>-wd1418
|
||||
<toolset>intel:<cxxflags>-wd2415
|
||||
|
||||
|
||||
# : default-build <threading>multi
|
||||
: usage-requirements # pass these requirement to dependents (i.e. users)
|
||||
#<link>static:<define>BOOST_THREAD_STATIC_LINK=1
|
||||
#<link>shared:<define>BOOST_THREAD_DYN_LINK=1
|
||||
<link>static:<define>BOOST_THREAD_BUILD_LIB=1
|
||||
<link>shared:<define>BOOST_THREAD_BUILD_DLL=1
|
||||
#<define>BOOST_THREAD_THROW_IF_PRECONDITION_NOT_SATISFIED
|
||||
#<define>BOOST_SYSTEM_NO_DEPRECATED
|
||||
#<define>BOOST_THREAD_DONT_PROVIDE_INTERRUPTIONS
|
||||
<library>/boost/system//boost_system
|
||||
;
|
||||
|
||||
local rule default_threadapi ( )
|
||||
{
|
||||
local api = pthread ;
|
||||
if [ os.name ] = "NT" { api = win32 ; }
|
||||
return $(api) ;
|
||||
}
|
||||
|
||||
feature.feature threadapi : pthread win32 : propagated ;
|
||||
feature.set-default threadapi : [ default_threadapi ] ;
|
||||
|
||||
rule tag ( name : type ? : property-set )
|
||||
{
|
||||
local result = $(name) ;
|
||||
|
||||
if $(type) in STATIC_LIB SHARED_LIB IMPORT_LIB
|
||||
{
|
||||
local api = [ $(property-set).get <threadapi> ] ;
|
||||
|
||||
# non native api gets additional tag
|
||||
if $(api) != [ default_threadapi ] {
|
||||
result = $(result)_$(api) ;
|
||||
}
|
||||
}
|
||||
|
||||
# forward to the boost tagging rule
|
||||
return [ indirect.call $(BOOST_JAMROOT_MODULE)%$(BOOST_JAMROOT_MODULE).tag
|
||||
$(result) : $(type) : $(property-set) ] ;
|
||||
}
|
||||
|
||||
rule win32_pthread_paths ( properties * )
|
||||
{
|
||||
local result ;
|
||||
local PTW32_INCLUDE ;
|
||||
local PTW32_LIB ;
|
||||
PTW32_INCLUDE = [ modules.peek : PTW32_INCLUDE ] ;
|
||||
PTW32_LIB = [ modules.peek : PTW32_LIB ] ;
|
||||
PTW32_INCLUDE ?= [ modules.peek user-config : PTW32_INCLUDE ] ;
|
||||
PTW32_LIB ?= [ modules.peek user-config : PTW32_LIB ] ;
|
||||
PTW32_INCLUDE ?= [ modules.peek site-config : PTW32_INCLUDE ] ;
|
||||
PTW32_LIB ?= [ modules.peek site-config : PTW32_LIB ] ;
|
||||
|
||||
if ! ( $(PTW32_INCLUDE) && $(PTW32_LIB) )
|
||||
{
|
||||
if ! $(.notified)
|
||||
{
|
||||
echo "************************************************************" ;
|
||||
echo "Trying to build Boost.Thread with pthread support." ;
|
||||
echo "If you need pthread you should specify the paths." ;
|
||||
echo "You can specify them in site-config.jam, user-config.jam" ;
|
||||
echo "or in the environment." ;
|
||||
echo "For example:" ;
|
||||
echo "PTW32_INCLUDE=C:\\Program Files\\ptw32\\Pre-built2\\include" ;
|
||||
echo "PTW32_LIB=C:\\Program Files\\ptw32\\Pre-built2\\lib" ;
|
||||
echo "************************************************************" ;
|
||||
.notified = true ;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
local include_path = [ path.make $(PTW32_INCLUDE) ] ;
|
||||
local lib_path = [ path.make $(PTW32_LIB) ] ;
|
||||
local libname = pthread ;
|
||||
if <toolset>msvc in $(properties)
|
||||
{
|
||||
libname = $(libname)VC2.lib ;
|
||||
}
|
||||
if <toolset>gcc in $(properties)
|
||||
{
|
||||
libname = lib$(libname)GC2.a ;
|
||||
}
|
||||
lib_path = [ path.glob $(lib_path) : $(libname) ] ;
|
||||
if ! $(lib_path)
|
||||
{
|
||||
if ! $(.notified)
|
||||
{
|
||||
echo "************************************************************" ;
|
||||
echo "Trying to build Boost.Thread with pthread support." ;
|
||||
echo "But the library" $(libname) "could not be found in path" ;
|
||||
echo $(PTW32_LIB) ;
|
||||
echo "************************************************************" ;
|
||||
.notified = true ;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
result += <include>$(include_path) ;
|
||||
result += <library>$(lib_path) ;
|
||||
}
|
||||
}
|
||||
return $(result) ;
|
||||
}
|
||||
|
||||
rule usage-requirements ( properties * )
|
||||
{
|
||||
local result ;
|
||||
if <threadapi>pthread in $(properties)
|
||||
{
|
||||
result += <define>BOOST_THREAD_POSIX ;
|
||||
if <target-os>windows in $(properties)
|
||||
{
|
||||
result += [ win32_pthread_paths $(properties) ] ;
|
||||
# TODO: What is for static linking? Is the <library> also needed
|
||||
# in that case?
|
||||
}
|
||||
}
|
||||
|
||||
if ! <toolset>vacpp in $(properties) || <toolset-vacpp:version>11.1 in $(properties) || <toolset-vacpp:version>12.1.0.1 in $(properties)
|
||||
{
|
||||
result += <library>/boost/chrono//boost_chrono ;
|
||||
}
|
||||
|
||||
return $(result) ;
|
||||
}
|
||||
|
||||
rule requirements ( properties * )
|
||||
{
|
||||
local result ;
|
||||
|
||||
if <threadapi>pthread in $(properties)
|
||||
{
|
||||
result += <define>BOOST_THREAD_POSIX ;
|
||||
if <target-os>windows in $(properties)
|
||||
{
|
||||
local paths = [ win32_pthread_paths $(properties) ] ;
|
||||
if $(paths)
|
||||
{
|
||||
result += $(paths) ;
|
||||
}
|
||||
else
|
||||
{
|
||||
result = <build>no ;
|
||||
}
|
||||
}
|
||||
}
|
||||
result += <define>BOOST_THREAD_DONT_USE_CHRONO ;
|
||||
|
||||
return $(result) ;
|
||||
}
|
||||
|
||||
alias thread_sources
|
||||
: ## win32 sources ##
|
||||
win32/thread.cpp
|
||||
win32/tss_dll.cpp
|
||||
win32/tss_pe.cpp
|
||||
: ## requirements ##
|
||||
<threadapi>win32
|
||||
;
|
||||
|
||||
alias thread_sources
|
||||
: ## pthread sources ##
|
||||
pthread/thread.cpp
|
||||
pthread/once.cpp
|
||||
: ## requirements ##
|
||||
<threadapi>pthread
|
||||
;
|
||||
|
||||
explicit thread_sources ;
|
||||
|
||||
lib boost_thread
|
||||
: thread_sources future.cpp
|
||||
: <conditional>@requirements
|
||||
:
|
||||
: <link>shared:<define>BOOST_THREAD_USE_DLL=1
|
||||
<link>static:<define>BOOST_THREAD_USE_LIB=1
|
||||
<conditional>@usage-requirements
|
||||
;
|
||||
32
doc/Jamfile.v2
Normal file
32
doc/Jamfile.v2
Normal file
@@ -0,0 +1,32 @@
|
||||
# (C) Copyright 2008-11 Anthony Williams
|
||||
# (C) Copyright 2011-12 Vicente J. Botet Escriba
|
||||
#
|
||||
# Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
path-constant boost-images : ../../../doc/src/images ;
|
||||
|
||||
xml thread : thread.qbk ;
|
||||
|
||||
boostbook standalone
|
||||
:
|
||||
thread
|
||||
:
|
||||
# HTML options first:
|
||||
# Use graphics not text for navigation:
|
||||
<xsl:param>navig.graphics=1
|
||||
# How far down we chunk nested sections, basically all of them:
|
||||
<xsl:param>chunk.section.depth=2
|
||||
# Don't put the first section on the same page as the TOC:
|
||||
<xsl:param>chunk.first.sections=1
|
||||
# How far down sections get TOC's
|
||||
<xsl:param>toc.section.depth=4
|
||||
# Max depth in each TOC:
|
||||
<xsl:param>toc.max.depth=2
|
||||
# How far down we go with TOC's
|
||||
<xsl:param>generate.section.toc.level=10
|
||||
# Path for links to Boost:
|
||||
<xsl:param>boost.root=../../../..
|
||||
;
|
||||
|
||||
|
||||
@@ -1,77 +0,0 @@
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content=
|
||||
"text/html; charset=windows-1252">
|
||||
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
|
||||
<meta name="ProgId" content="FrontPage.Editor.Document">
|
||||
|
||||
<title>Boost.Threads Acknowledgements</title>
|
||||
</head>
|
||||
|
||||
<body bgcolor="#FFFFFF" text="#000000">
|
||||
<table summary="header" border="0" cellpadding="7" cellspacing="0"
|
||||
width="100%">
|
||||
<tr>
|
||||
<td valign="top" width="300">
|
||||
<h3><img src="../../../c++boost.gif" alt="C++ Boost" width=
|
||||
"277" height="86"></h3>
|
||||
</td>
|
||||
|
||||
<td valign="top">
|
||||
<h1 align="center">Boost.Threads</h1>
|
||||
|
||||
<h2 align="center">Acknowledgements</h2>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<h2>Acknowledgments</h2>
|
||||
|
||||
<p><a href="../../../people/william_kempf.htm">William E. Kempf</a> was
|
||||
the architect, designer, and implementor of <b>Boost.Threads</b>.</p>
|
||||
|
||||
<p>Important contributions were also made by Jeremy Siek (lots of input
|
||||
on the design and on the implementation), Alexander Terekhov (lots of
|
||||
input on the Win32 implementation, especially in regards to
|
||||
boost::condition, as well as a lot of explanation of POSIX behavior),
|
||||
Greg Colvin (lots of input on the design), and Paul Mclachlan, Thomas
|
||||
Matelich and Iain Hanson (for help in trying to get the build to work
|
||||
on other platforms).</p>
|
||||
|
||||
<p>The documentation was written by William E. Kempf. Beman Dawes
|
||||
provided additional documentation material and editing.</p>
|
||||
|
||||
<p>Discussions on the boost.org mailing list were essential in the
|
||||
development of <b>Boost.Threads</b>. As of August 1, 2001, participants
|
||||
included Alan Griffiths, Albrecht Fritzsche, Aleksey Gurtovoy,
|
||||
Alexander Terekhov, Andrew Green, Andy Sawyer, Asger Alstrup Nielsen,
|
||||
Beman Dawes, Bill Klein, Bill Rutiser, Bill Wade, Branko Èibej,
|
||||
Brent Verner, Craig Henderson, Csaba Szepesvari, Dale Peakall, Damian
|
||||
Dixon, Dan Nuffer, Darryl Green, Daryle Walker, David Abrahams, David
|
||||
Allan Finch, Dejan Jelovic, Dietmar Kuehl, Doug Gregor, Douglas Gregor,
|
||||
Duncan Harris, Ed Brey, Eric Swanson, Eugene Karpachov, Fabrice
|
||||
Truillot, Frank Gerlach, Gary Powell, Gernot Neppert, Geurt Vos, Ghazi
|
||||
Ramadan, Greg Colvin, Gregory Seidman, HYS, Iain Hanson, Ian Bruntlett,
|
||||
J Panzer, Jeff Garland, Jeff Paquette, Jens Maurer, Jeremy Siek, Jesse
|
||||
Jones, Joe Gottman, John (EBo) David, John Bandela, John Maddock, John
|
||||
Max Skaller, John Panzer, Jon Jagger , Karl Nelson, Kevlin Henney, KG
|
||||
Chandrasekhar, Levente Farkas, Lie-Quan Lee, Lois Goldthwaite, Luis
|
||||
Pedro Coelho, Marc Girod, Mark A. Borgerding, Mark Rodgers, Marshall
|
||||
Clow, Matthew Austern, Matthew Hurd, Michael D. Crawford, Michael H.
|
||||
Cox , Mike Haller, Miki Jovanovic, Nathan Myers, Paul Moore, Pavel
|
||||
Cisler, Peter Dimov, Petr Kocmid, Philip Nash, Rainer Deyke, Reid
|
||||
Sweatman, Ross Smith, Scott McCaskill, Shalom Reich , Steve Cleary,
|
||||
Steven Kirk, Thomas Holenstein, Thomas Matelich, Trevor Perrin,
|
||||
Valentin Bonnard, Vesa Karvonen, Wayne Miller, and William Kempf.</p>
|
||||
|
||||
<p>Apologies for anyone inadvertently missed.</p>
|
||||
<hr>
|
||||
|
||||
<p>Revised
|
||||
<!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan -->05 November, 2001<!--webbot bot="Timestamp" endspan i-checksum="39359" --></p>
|
||||
|
||||
<p>© <i>Copyright <a href="mailto:williamkempf@hotmail.com">
|
||||
William E. Kempf</a> 2001</i></p>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
23
doc/acknowledgements.qbk
Normal file
23
doc/acknowledgements.qbk
Normal file
@@ -0,0 +1,23 @@
|
||||
[/
|
||||
(C) Copyright 2007-8 Anthony Williams.
|
||||
Distributed under the Boost Software License, Version 1.0.
|
||||
(See accompanying file LICENSE_1_0.txt or copy at
|
||||
http://www.boost.org/LICENSE_1_0.txt).
|
||||
]
|
||||
|
||||
[section:acknowledgements Acknowledgments]
|
||||
|
||||
The original implementation of __boost_thread__ was written by William Kempf, with contributions from numerous others. This new
|
||||
version initially grew out of an attempt to rewrite __boost_thread__ to William Kempf's design with fresh code that could be
|
||||
released under the Boost Software License. However, as the C++ Standards committee have been actively discussing standardizing a
|
||||
thread library for C++, this library has evolved to reflect the proposals, whilst retaining as much backwards-compatibility as
|
||||
possible.
|
||||
|
||||
Particular thanks must be given to Roland Schwarz, who contributed a lot of time and code to the original __boost_thread__ library,
|
||||
and who has been actively involved with the rewrite. The scheme for dividing the platform-specific implementations into separate
|
||||
directories was devised by Roland, and his input has contributed greatly to improving the quality of the current implementation.
|
||||
|
||||
Thanks also must go to Peter Dimov, Howard Hinnant, Alexander Terekhov, Chris Thomasson and others for their comments on the
|
||||
implementation details of the code.
|
||||
|
||||
[endsect]
|
||||
72
doc/barrier.qbk
Normal file
72
doc/barrier.qbk
Normal file
@@ -0,0 +1,72 @@
|
||||
[/
|
||||
(C) Copyright 2007-8 Anthony Williams.
|
||||
Distributed under the Boost Software License, Version 1.0.
|
||||
(See accompanying file LICENSE_1_0.txt or copy at
|
||||
http://www.boost.org/LICENSE_1_0.txt).
|
||||
]
|
||||
|
||||
[section:barriers Barriers]
|
||||
|
||||
A barrier is a simple concept. Also known as a ['rendezvous], it is a synchronization point between multiple threads. The barrier is
|
||||
configured for a particular number of threads (`n`), and as threads reach the barrier they must wait until all `n` threads have
|
||||
arrived. Once the `n`-th thread has reached the barrier, all the waiting threads can proceed, and the barrier is reset.
|
||||
|
||||
[section:barrier Class `barrier`]
|
||||
|
||||
#include <boost/thread/barrier.hpp>
|
||||
|
||||
class barrier
|
||||
{
|
||||
public:
|
||||
barrier(unsigned int count);
|
||||
~barrier();
|
||||
|
||||
bool wait();
|
||||
};
|
||||
|
||||
Instances of __barrier__ are not copyable or movable.
|
||||
|
||||
[heading Constructor]
|
||||
|
||||
barrier(unsigned int count);
|
||||
|
||||
[variablelist
|
||||
|
||||
[[Effects:] [Construct a barrier for `count` threads.]]
|
||||
|
||||
[[Throws:] [__thread_resource_error__ if an error occurs.]]
|
||||
|
||||
]
|
||||
|
||||
[heading Destructor]
|
||||
|
||||
~barrier();
|
||||
|
||||
[variablelist
|
||||
|
||||
[[Precondition:] [No threads are waiting on `*this`.]]
|
||||
|
||||
[[Effects:] [Destroys `*this`.]]
|
||||
|
||||
[[Throws:] [Nothing.]]
|
||||
|
||||
]
|
||||
|
||||
[heading Member function `wait`]
|
||||
|
||||
bool wait();
|
||||
|
||||
[variablelist
|
||||
|
||||
[[Effects:] [Block until `count` threads have called `wait` on `*this`. When the `count`-th thread calls `wait`, all waiting threads
|
||||
are unblocked, and the barrier is reset. ]]
|
||||
|
||||
[[Returns:] [`true` for exactly one thread from each batch of waiting threads, `false` otherwise.]]
|
||||
|
||||
[[Throws:] [__thread_resource_error__ if an error occurs.]]
|
||||
|
||||
]
|
||||
|
||||
[endsect]
|
||||
|
||||
[endsect]
|
||||
@@ -1,222 +0,0 @@
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content=
|
||||
"text/html; charset=windows-1252">
|
||||
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
|
||||
<meta name="ProgId" content="FrontPage.Editor.Document">
|
||||
|
||||
<title>Boost.Threads Bibliography</title>
|
||||
</head>
|
||||
|
||||
<body bgcolor="#FFFFFF" text="#000000">
|
||||
<table summary="header" border="0" cellpadding="7" cellspacing="0"
|
||||
width="100%">
|
||||
<tr>
|
||||
<td valign="top" width="300">
|
||||
<h3><img src="../../../c++boost.gif" alt="C++ Boost" width=
|
||||
"277" height="86"></h3>
|
||||
</td>
|
||||
|
||||
<td valign="top">
|
||||
<h1 align="center">Boost.Threads</h1>
|
||||
|
||||
<h2 align="center">Bibliography</h2>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<h2>Bibliography</h2>
|
||||
|
||||
<table summary="Bibliography" border="0" cellpadding="5" width="777">
|
||||
<tr>
|
||||
<td width="102" valign="top" align="left"><b>[<a name=
|
||||
"Andrews-83">Andrews 83</a>]</b></td>
|
||||
|
||||
<td width="645">
|
||||
Gregory R. Andrews, Fred B. Schneider, <cite>Concepts and
|
||||
Notations for Concurrent Programming</cite>, ACM Computing
|
||||
Surveys, Vol. 15, No. 1, March, 1983. <a href=
|
||||
"http://www.acm.org/pubs/citations/journals/surveys/1983-15-1/p3-andrews/">
|
||||
http://www.acm.org/pubs/citations/journals/surveys/1983-15-1/p3-andrews/</a>
|
||||
|
||||
|
||||
<p>Good general background reading. Includes descriptions
|
||||
of Path Expressions, Message Passing, and Remote Procedure
|
||||
Call in addition to the basics.</p>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td width="102" valign="top" align="left"><b>[<a name=
|
||||
"Boost">Boost</a>]</b></td>
|
||||
|
||||
<td width="645">
|
||||
The <cite>Boost</cite> world-wide web site. <a href=
|
||||
"http://www.boost.org">http://www.boost.org</a>
|
||||
|
||||
<p>Boost.Threads is one of many Boost libraries. The Boost
|
||||
web site includes a great deal of documentation and general
|
||||
information which applies to all Boost libraries. Current
|
||||
copies of the libraries including documentation and test
|
||||
programs may be downloaded from the web site.</p>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td width="102" valign="top" align="left"><b>[<a name=
|
||||
"Brinch-Hansen-73">Brinch Hansen 73</a>]</b></td>
|
||||
|
||||
<td width="645">
|
||||
Per Brinch Hansen, <cite>Concurrent Programming
|
||||
Concepts</cite>, ACM Computing Surveys, Vol. 5, No. 4,
|
||||
December, 1973. <a href=
|
||||
"http://www.acm.org/pubs/articles/journals/surveys/1973-5-4/p223-hansen/p223-hansen.pdf">
|
||||
http://www.acm.org/pubs/articles/journals/surveys/1973-5-4/p223-hansen/</a>
|
||||
|
||||
|
||||
<p>"This paper describes the evolution of language
|
||||
features for multiprogramming from event queues and
|
||||
semaphores to critical regions and monitors." Includes
|
||||
analysis of why <i>events</i> are considered error-prone.
|
||||
Also noteworthy because of an introductory quotation from
|
||||
Christopher Alexander; Brinch Hansen was years ahead of
|
||||
others in recognizing pattern concepts applied to software
|
||||
too.</p>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td width="102" valign="top" align="left"><b>]<a name=
|
||||
"Butenhof-97">Butenhof 97</a>]</b></td>
|
||||
|
||||
<td width="645">
|
||||
<p>David R. Butenhof, <cite>Programming with POSIX
|
||||
Threads</cite>, Addison-Wesley 1997, ISBN 0-201-63392-2 <a
|
||||
href="http://cseng.aw.com/book/0,3828,0201633922,00.html">
|
||||
http://cseng.aw.com/book/0,3828,0201633922,00.html</a></p>
|
||||
|
||||
<p>This is a very readable explanation of threads and how
|
||||
to use them. Many of the insights given apply to all
|
||||
multi-threaded programming, not just POSIX Threads.</p>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td width="102" valign="top" align="left"><b>[<a name=
|
||||
"Hoare-74">Hoare 74</a>]</b></td>
|
||||
|
||||
<td width="645">
|
||||
<p>C.A.R Hoare, <cite>Monitors: An Operating System
|
||||
Structuring Concept</cite>, Communications of the ACM, Vol.
|
||||
17, No. 10. October 1974, pp. 549-557 <a href=
|
||||
"http://www.acm.org/classics/feb96/">
|
||||
http://www.acm.org/classics/feb96/</a></p>
|
||||
|
||||
<p>Hoare and Brinch Hansen's work on Monitors is the
|
||||
basis for reliable multi-threading patterns. This is one of
|
||||
the most often referenced papers in all of computer
|
||||
science, and with good reason.</p>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td width="102" valign="top" align="left"><b>[<a name=
|
||||
"ISO-98">ISO 98</a>]</b></td>
|
||||
|
||||
<td width="645">
|
||||
<p>ISO/IEC 14882:1998(E) <cite>Programming Language
|
||||
C++</cite> <a href="http://www.ansi.org">
|
||||
http://www.ansi.org</a></p>
|
||||
|
||||
<p>This is the official C++ Standards document. Available
|
||||
from the ANSI (American National Standards Institute)
|
||||
Electronic Standards Store.</p>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td width="102" valign="top" align="left"><b>[<a name=
|
||||
"McDowell-89">McDowell 89</a>]</b></td>
|
||||
|
||||
<td width="645">
|
||||
Charles E McDowell, David P. Helmbold, <cite>Debugging
|
||||
Concurrent Programs</cite>, ACM Computing Surveys, Vol. 21,
|
||||
No. 2, December, 1989. <a href=
|
||||
"http://www.acm.org/pubs/citations/journals/surveys/1989-21-4/p593-mcdowell/">
|
||||
http://www.acm.org/pubs/citations/journals/surveys/1989-21-4/p593-mcdowell/</a>
|
||||
|
||||
|
||||
<p>Identifies many of the unique failure modes and
|
||||
debugging difficulties associated with concurrent
|
||||
programs.</p>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td width="102" valign="top" align="left"><b>[<a name=
|
||||
"Schmidt">Schmidt</a>]</b> </td>
|
||||
|
||||
<td width="645">
|
||||
<p>Douglas C. Schmidt and Irfan Pyarali, <cite>Strategies
|
||||
for Implementing POSIX Condition Variables on Win32</cite>,
|
||||
Department of Computer Science, Washington University, St.
|
||||
Louis, Missouri. <a href=
|
||||
"http://www.cs.wustl.edu/~schmidt/win32-cv-1.html">
|
||||
http://www.cs.wustl.edu/~schmidt/win32-cv-1.html</a></p>
|
||||
|
||||
<p>Rationale for understanding Boost.Threads condition
|
||||
variables. Note that Alexander Terekhov found some bugs in
|
||||
the implementation given in this article, so pthreads-win32
|
||||
and Boost.Threads are even more complicated yet.</p>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td width="102" valign="top" align="left"><b>[<a name=
|
||||
"Schmidt-00">Schmidt 00</a>]</b> </td>
|
||||
|
||||
<td width="645">
|
||||
<p>Douglas C. Schmidt, Michael Stal, Hans Rohnert and Frank
|
||||
Buschmann, <cite>Pattern-Oriented Software Architecture
|
||||
Volume 2 - Patterns for Concurrent and Networked
|
||||
Objects</cite>, Wiley 2000, ISBN 0-471-60695-2 <a href=
|
||||
"http://www.wiley.com/Corporate/Website/Objects/Products/0,9049,104671,00.html">
|
||||
http://www.wiley.com/Corporate/Website/Objects/Products/0,9049,104671,00.html</a></p>
|
||||
|
||||
<p>This is a very good explanation of how to apply several
|
||||
patterns useful for concurrent programming. Among the
|
||||
patterns documented is the Monitor Pattern mentioned
|
||||
frequently in the <b>Boost.Threads</b> documentation.</p>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td width="102" valign="top" align="left"><b>[<a name=
|
||||
"Stroustrup-00">Stroustrup 00</a>]</b></td>
|
||||
|
||||
<td width="645">
|
||||
Bjarne Stroustrup, <cite>The C++ Programming
|
||||
Language</cite>, Special Edition, Addison-Wesley 2000, ISBN
|
||||
0-201-70073-5 <a href=
|
||||
"http://cseng.aw.com/book/0,3828,0201700735,00.html">
|
||||
http://cseng.aw.com/book/0,3828,0201700735,00.html</a>
|
||||
|
||||
<p>The first book a C++ programmer should own. Note that
|
||||
the 3rd edition (and subsequent editions like the Special
|
||||
Edition) has been rewritten to cover the ISO standard
|
||||
language and library.</p>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<p>Note: The URL's above are provided in plain text form so that
|
||||
they will be visible on printed copies of this document.</p>
|
||||
<hr>
|
||||
|
||||
<p>Revised
|
||||
<!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %b %Y" startspan -->05 Nov 2001<!--webbot bot="Timestamp" endspan i-checksum="15246" --></p>
|
||||
|
||||
<p>© Copyright Beman Dawes, 2001</p>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -1,132 +0,0 @@
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content=
|
||||
"text/html; charset=iso-8859-1">
|
||||
<meta name="keywords" content=
|
||||
"threads, Boost.Threads, thread library, C++">
|
||||
<link rel="stylesheet" type="text/css" href="styles.css">
|
||||
|
||||
<title>Boost.Threads, call_once</title>
|
||||
</head>
|
||||
|
||||
<body bgcolor="#FFFFFF" link="#0000FF" vlink="#800080">
|
||||
<table summary="header" border="0" cellpadding="7" cellspacing="0"
|
||||
width="100%">
|
||||
<tr>
|
||||
<td valign="top" width="300">
|
||||
<h3><img src="../../../c++boost.gif" alt="C++ Boost" width=
|
||||
"277" height="86"></h3>
|
||||
</td>
|
||||
|
||||
<td valign="top">
|
||||
<h1 align="center">Boost.Threads</h1>
|
||||
|
||||
<h2 align="center">call_once</h2>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<hr>
|
||||
|
||||
<p><a href="#Introduction">Introduction</a><br>
|
||||
<a href="#Header">Header</a><br>
|
||||
<a href="#Synopsis">Synopsis</a><br>
|
||||
<a href="#Members">Members</a><br>
|
||||
<a href="#Example">Example</a></p>
|
||||
|
||||
<h2><a name="Introduction">Introduction</a></h2>
|
||||
|
||||
<p>The <code>call_once</code> routine and <code>once_flag</code> type
|
||||
can be used to run a routine exactly once. This can be used to
|
||||
initialize data in a <a href="definitions.html#Thread-safe">
|
||||
thread-safe</a> manner.</p>
|
||||
|
||||
<h2><a name="Header">Header</a></h2>
|
||||
<pre>
|
||||
#include <a href=
|
||||
"../../../boost/thread/once.hpp"><boost/thread/once.hpp></a>
|
||||
</pre>
|
||||
|
||||
<h2><a name="Synopsis">Synopsis</a></h2>
|
||||
<pre>
|
||||
namespace boost {
|
||||
|
||||
typedef <i>[implementation defined]</i> once_flag;
|
||||
const once_flag once_init = <i>[implementation defined]</i>;
|
||||
void call_once(void (*func)(), once_flag& flag);
|
||||
|
||||
} // namespace boost
|
||||
</pre>
|
||||
|
||||
<h2><a name="Reference">Reference</a></h2>
|
||||
<hr>
|
||||
|
||||
<h3>once_flag</h3>
|
||||
|
||||
<p>This implementation defined type is used as a flag to insure a
|
||||
routine is called only once. Instances of this type should be
|
||||
statically initialized to <code>once_init</code>.</p>
|
||||
<hr>
|
||||
|
||||
<h3>once_init</h3>
|
||||
|
||||
<p>This is a constant value used to initialize <code>once_flag</code>
|
||||
instances to indicate that the logically associated routine has not
|
||||
been run yet.</p>
|
||||
<hr>
|
||||
|
||||
<h3>call_once</h3>
|
||||
<pre>
|
||||
void call_once(void (*func)(), once_flag& flag);
|
||||
</pre>
|
||||
|
||||
<p><b>Requires:</b> The function <code>func</code> shall not throw
|
||||
exceptions.</p>
|
||||
|
||||
<p><b>Effects:</b> As if (in an atomic fashion)</p>
|
||||
<code> if (flag == once_init)<br>
|
||||
func();</code>
|
||||
|
||||
<p><b>Postcondition:</b> <code>flag</code> != <code>
|
||||
once_init</code></p>
|
||||
<hr>
|
||||
|
||||
<h2><a name="Example">Example Usage</a></h2>
|
||||
<pre>
|
||||
#include <a href=
|
||||
"../../../boost/thread/thread.hpp"><boost/thread/thread.hpp></a>
|
||||
#include <a href=
|
||||
"../../../boost/thread/tss.hpp"><boost/thread/once.hpp></a>
|
||||
#include <cassert>
|
||||
|
||||
int value=0;
|
||||
boost::once_flag once = boost::once_init;
|
||||
|
||||
void init()
|
||||
{
|
||||
++value;
|
||||
}
|
||||
|
||||
void thread_proc()
|
||||
{
|
||||
boost::call_once(&init, once);
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
boost::thread_group threads;
|
||||
for (int i=0; i<5; ++i)
|
||||
threads.create_thread(&thread_proc);
|
||||
threads.join_all();
|
||||
assert(value == 1);
|
||||
}
|
||||
</pre>
|
||||
<hr>
|
||||
|
||||
<p>Revised
|
||||
<!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan -->05 November, 2001<!--webbot bot="Timestamp" endspan i-checksum="39359" --></p>
|
||||
|
||||
<p><i>© Copyright <a href="mailto:williamkempf@hotmail.com">
|
||||
William E. Kempf</a> 2001 all rights reserved.</i></p>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
362
doc/changes.qbk
Normal file
362
doc/changes.qbk
Normal file
@@ -0,0 +1,362 @@
|
||||
[/
|
||||
(C) Copyright 2007-11 Anthony Williams.
|
||||
(C) Copyright 2011-12 Vicente J. Botet Escriba.
|
||||
Distributed under the Boost Software License, Version 1.0.
|
||||
(See accompanying file LICENSE_1_0.txt or copy at
|
||||
http://www.boost.org/LICENSE_1_0.txt).
|
||||
]
|
||||
|
||||
[section:changes History]
|
||||
|
||||
[heading Version 4.0.0 - boost 1.53]
|
||||
|
||||
[/
|
||||
[*Breaking changes:]
|
||||
|
||||
[warning
|
||||
BOOST_THREAD_VERSION==3 by default since Boost 1.53. So that all the deprecated features since 1.50 are not included by default. You can change this by setting the appropriated define (see Configuration section).
|
||||
]
|
||||
]
|
||||
|
||||
[*Deprecated features:]
|
||||
|
||||
[warning Deprecated features since boost 1.53 will be available only until boost 1.58.]
|
||||
|
||||
* C++11 compliance: packaged_task<R> is deprecated, use instead packaged_task<R()>.
|
||||
See BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK and BOOST_THREAD_DONT_PROVIDE_SIGNATURE_PACKAGED_TASK
|
||||
|
||||
* [@http://svn.boost.org/trac/boost/ticket/7537 #7537] deprecate Mutex::scoped_lock and scoped_try_lock and boost::condition
|
||||
|
||||
[*New Features:]
|
||||
|
||||
* [@http://svn.boost.org/trac/boost/ticket/6270 #6270] c++11 compliance: Add thread constructor from movable callable and movable arguments
|
||||
Provided when BOOST_THREAD_PROVIDES_VARIADIC_THREAD is defined (Default value from Boost 1.55):
|
||||
See BOOST_THREAD_PROVIDES_VARIADIC_THREAD and BOOST_THREAD_DONT_PROVIDE_VARIADIC_THREAD.
|
||||
|
||||
* [@http://svn.boost.org/trac/boost/ticket/7279 #7279] C++11 compliance: Add noexcept in system related functions
|
||||
* [@http://svn.boost.org/trac/boost/ticket/7280 #7280] C++11 compliance: Add promise::...at_thread_exit functions
|
||||
|
||||
* [@http://svn.boost.org/trac/boost/ticket/7281 #7281] C++11 compliance: Add ArgTypes to packaged_task template.
|
||||
Provided when BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK is defined (Default value from Boost 1.55).
|
||||
See BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK and BOOST_THREAD_DONT_PROVIDE_SIGNATURE_PACKAGED_TASK.
|
||||
|
||||
* [@http://svn.boost.org/trac/boost/ticket/7282 #7282] C++11 compliance: Add packaged_task::make_ready_at_thread_exit function
|
||||
|
||||
* [@http://svn.boost.org/trac/boost/ticket/7412 #7412] C++11 compliance: Add async from movable callable and movable arguments
|
||||
Provided when BOOST_THREAD_PROVIDES_VARIADIC_THREAD and BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK are defined (Default value from Boost 1.55):
|
||||
See BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK and BOOST_THREAD_DONT_PROVIDE_SIGNATURE_PACKAGED_TASK, BOOST_THREAD_PROVIDES_VARIADIC_THREAD and BOOST_THREAD_DONT_PROVIDE_VARIADIC_THREAD.
|
||||
|
||||
* [@http://svn.boost.org/trac/boost/ticket/7413 #7413] C++11 compliance: Add async when the launch policy is deferred.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/7414 #7414] C++11 compliance: future::get post-condition should be valid()==false.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/7422 #7422] Provide a condition variable with zero-overhead performance penality.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/7414 #7444] Async: Add make_future/make_shared_future.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/7540 #7540] Threads: Add a helper class that join a thread on destruction.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/7541 #7541] Threads: Add a thread wrapper class that joins on destruction.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/7575 #7575] C++11 compliance: A future created by async should "join" in the destructor.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/7587 #7587] Synchro: Add strict_lock and nested_strict_lock.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/7588 #7588] Synchro: Split the locks.hpp in several files to limit dependencies.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/7590 #7590] Synchro: Add lockable concept checkers based on Boost.ConceptCheck.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/7591 #7591] Add lockable traits that can be used with enable_if.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/7592 #7592] Synchro: Add a null_mutex that is a no-op and that is a model of UpgardeLockable.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/7593 #7593] Synchro: Add a externally_locked class.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/7590 #7594] Threads: Allow to disable thread interruptions.
|
||||
|
||||
[*Fixed Bugs:]
|
||||
|
||||
* [@http://svn.boost.org/trac/boost/ticket/7464 #7464] BOOST_TEST(n_alive == 1); fails due to race condition in a regression test tool.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/7657 #7657] Serious performance and memory consumption hit if condition_variable methods condition notify_one or notify_all is used repeatedly.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/7665 #7665] this_thread::sleep_for no longer uses steady_clock in thread.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/7668 #7668] thread_group::join_all() should check whether its threads are joinable.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/7669 #7669] thread_group::join_all() should catch resource_deadlock_would_occur.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/7672 #7672] lockable_traits.hpp syntax error: "defined" token misspelled.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/7798 #7798] boost::future set_wait_callback thread safety issues.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/7808 #7808] Incorrect description of effects for this_thread::sleep_for and this_thread::sleep_until.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/7812 #7812] Returns: cv_status::no_timeout if the call is returning because the time period specified by rel_time has elapsed, cv_status::timeout otherwise.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/7874 #7874] compile warning: thread.hpp:342: warning: type attributes are honored only at type definition.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/7875 #7875] BOOST_THREAD_THROW_IF_PRECONDITION_NOT_SATISFIED should not be enabled by default.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/7882 #7882] wrong exception text from condition_variable::wait(unique_lock<mutex>&).
|
||||
|
||||
|
||||
[heading Version 3.1.0 - boost 1.52]
|
||||
|
||||
Deprecated Features:
|
||||
|
||||
Deprecated features since boost 1.50 available only until boost 1.55:
|
||||
|
||||
These deprecated features will be provided by default up to boost 1.52. If you don't want to include the deprecated features you could define BOOST_THREAD_DONT_PROVIDE_DEPRECATED_FEATURES_SINCE_V3_0_0. Since 1.53 these features will not be included any more by default. Since this version, if you want to include the deprecated features yet you could define BOOST_THREAD_PROVIDE_DEPRECATED_FEATURES_SINCE_V3_0_0. These deprecated features will be only available until boost 1.55, that is you have yet 1 year to move to the new features.
|
||||
|
||||
* Time related functions don't using the Boost.Chrono library, use the chrono overloads instead.
|
||||
|
||||
Breaking changes when BOOST_THREAD_VERSION==3 (Default value since Boost 1.53):
|
||||
|
||||
There are some new features which share the same interface but with different behavior. These breaking features are provided by default when BOOST_THREAD_VERSION is 3, but the user can however choose the version 2 behavior by defining the corresponding macro. As for the deprecated features, these broken features will be only available until boost 1.55.
|
||||
|
||||
* [@http://svn.boost.org/trac/boost/ticket/6229 #6229] Rename the unique_future to future following the c++11.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/6266 #6266] Breaking change: thread destructor should call terminate if joinable.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/6269 #6269] Breaking change: thread move assignment should call terminate if joinable.
|
||||
|
||||
New Features:
|
||||
|
||||
* [@http://svn.boost.org/trac/boost/ticket/2361 #2361] thread_specific_ptr: document nature of the key, complexity and rationale.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/4710 #4710] C++11 compliance: Missing async().
|
||||
* [@http://svn.boost.org/trac/boost/ticket/7283 #7283] C++11 compliance: Add notify_all_at_thread_exit.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/7345 #7345] C++11 compliance: Add noexcept to recursive mutex try_lock.
|
||||
|
||||
Fixed Bugs:
|
||||
|
||||
* [@http://svn.boost.org/trac/boost/ticket/2797 #2797] Two problems with thread_specific_ptr.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/5274 #5274] failed to compile future.hpp with stlport 5.1.5 under msvc8.1, because of undefined class.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/5431 #5431] compile error in Windows CE 6.0(interlocked).
|
||||
[/* [@http://svn.boost.org/trac/boost/ticket/5752 #5752] boost::call_once() is unreliable on some platforms.]
|
||||
* [@http://svn.boost.org/trac/boost/ticket/5696 #5696] win32 detail::set_tss_data does nothing when tss_cleanup_function is NULL.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/6931 #6931] mutex waits forwever with Intel C++ Compiler XE 12.1.5.344 Build 20120612
|
||||
* [@http://svn.boost.org/trac/boost/ticket/7045 #7045] Thread library does not automatically compile date_time.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/7173 #7173] wrong function name interrupt_point().
|
||||
* [@http://svn.boost.org/trac/boost/ticket/7200 #7200] Unable to build boost.thread modularized.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/7220 #7220] gcc 4.6.2 warns about inline+dllimport functions.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/7238 #7238] this_thread::sleep_for() does not respond to interrupt().
|
||||
* [@http://svn.boost.org/trac/boost/ticket/7245 #7245] Minor typos on documentation related to version 3.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/7272 #7272] win32/thread_primitives.hpp: (Unneccessary) Warning.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/7284 #7284] Clarify that there is no access priority between lock and shared_lock on shared mutex.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/7329 #7329] boost/thread/future.hpp does not compile on HPUX.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/7336 #7336] BOOST_THREAD_DONT_USE_SYSTEM doesn't work.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/7329 #7349] packaged_task holds reference to temporary.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/7350 #7350] allocator_destructor does not destroy object
|
||||
* [@http://svn.boost.org/trac/boost/ticket/7360 #7360] Memory leak in pthread implementation of boost::thread_specific_ptr
|
||||
* [@http://svn.boost.org/trac/boost/ticket/7370 #7370] Boost.Thread documentation
|
||||
* [@http://svn.boost.org/trac/boost/ticket/7438 #7438] Segmentation fault in test_once regression test in group.join_all();
|
||||
* [@http://svn.boost.org/trac/boost/ticket/7461 #7461] detail::win32::ReleaseSemaphore may be called with count_to_release equal to 0
|
||||
* [@http://svn.boost.org/trac/boost/ticket/7499 #7499] call_once doesn't call even once
|
||||
|
||||
|
||||
[heading Version 3.0.1 - boost 1.51]
|
||||
|
||||
Deprecated Features:
|
||||
|
||||
Deprecated features since boost 1.50 available only until boost 1.55:
|
||||
|
||||
These deprecated features will be provided by default up to boost 1.52. If you don't want to include the deprecated features you could define BOOST_THREAD_DONT_PROVIDE_DEPRECATED_FEATURES_SINCE_V3_0_0. Since 1.53 these features will not be included any more by default. Since this version, if you want to include the deprecated features yet you could define BOOST_THREAD_PROVIDE_DEPRECATED_FEATURES_SINCE_V3_0_0. These deprecated features will be only available until boost 1.55, that is you have 1 year and a half to move to the new features.
|
||||
|
||||
* Time related functions don't using the Boost.Chrono library, use the chrono overloads instead.
|
||||
|
||||
Breaking changes when BOOST_THREAD_VERSION==3:
|
||||
|
||||
There are some new features which share the same interface but with different behavior. These breaking features are provided by default when BOOST_THREAD_VERSION is 3, but the user can however choose the version 2 behavior by defining the corresponding macro. As for the deprecated features, these broken features will be only available until boost 1.55.
|
||||
|
||||
* [@http://svn.boost.org/trac/boost/ticket/6229 #6229] Rename the unique_future to future following the c++11.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/6266 #6266] Breaking change: thread destructor should call terminate if joinable.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/6269 #6269] Breaking change: thread move assignment should call terminate if joinable.
|
||||
|
||||
|
||||
Fixed Bugs:
|
||||
|
||||
|
||||
* [@http://svn.boost.org/trac/boost/ticket/4258 #4258] Linking with boost thread does not work on mingw/gcc 4.5.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/4885 #4885] Access violation in set_tss_data at process exit due to invalid assumption about TlsAlloc.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/6931 #6931] mutex waits forwever with Intel Compiler and /debug:parallel
|
||||
* [@http://svn.boost.org/trac/boost/ticket/7044 #7044] boost 1.50.0 header missing.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/7052 #7052] Thread: BOOST_THREAD_PROVIDES_DEPRECATED_FEATURES_SINCE_V3_0_0 only masks thread::operator==, thread::operator!= forward declarations, not definitions.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/7066 #7066] An attempt to fix current_thread_tls_key static initialization order.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/7074 #7074] Multiply defined symbol boost::allocator_arg.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/7078 #7078] Trivial 64-bit warning fix on Windows for thread attribute stack size
|
||||
* [@http://svn.boost.org/trac/boost/ticket/7089 #7089] BOOST_THREAD_WAIT_BUG limits functionality without solving anything
|
||||
|
||||
[/
|
||||
#6787 boost::thread::sleep() hangs if system time is rolled back
|
||||
#7045 Thread library does not automatically compile date_time
|
||||
]
|
||||
|
||||
[heading Version 3.0.0 - boost 1.50]
|
||||
|
||||
Breaking changes when BOOST_THREAD_VERSION==3:
|
||||
|
||||
* [@http://svn.boost.org/trac/boost/ticket/6229 #6229] Breaking change: Rename the unique_future to future following the c++11.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/6266 #6266] Breaking change: thread destructor should call terminate if joinable.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/6269 #6269] Breaking change: thread move assignment should call terminate if joinable.
|
||||
|
||||
New Features:
|
||||
|
||||
* [@http://svn.boost.org/trac/boost/ticket/1850 #1850] Request for unlock_guard to compliment lock_guard.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/2637 #2637] Request for shared_mutex duration timed_lock and timed_lock_shared.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/2741 #2741] Proposal to manage portable and non portable thread attributes.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/3567 #3567] Request for shared_lock_guard.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/6194 #6194] Adapt to Boost.Move.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/6195 #6195] c++11 compliance: Provide the standard time related interface using Boost.Chrono.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/6217 #6217] Enhance Boost.Thread shared mutex interface following Howard Hinnant proposal.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/6224 #6224] c++11 compliance: Add the use of standard noexcept on compilers supporting them.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/6225 #6225] Add the use of standard =delete defaulted operations on compilers supporting them.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/6226 #6226] c++11 compliance: Add explicit bool conversion from locks.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/6228 #6228] Add promise constructor with allocator following the standard c++11.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/6230 #6230] c++11 compliance: Follows the exception reporting mechanism as defined in the c++11.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/6231 #6231] Add BasicLockable requirements in the documentation to follow c++11.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/6272 #6272] c++11 compliance: Add thread::id hash specialization.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/6273 #6273] c++11 compliance: Add cv_status enum class and use it on the conditions wait functions.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/6342 #6342] c++11 compliance: Adapt the one_flag to the c++11 interface.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/6671 #6671] upgrade_lock: missing mutex and release functions.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/6672 #6672] upgrade_lock:: missing constructors from time related types.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/6675 #6675] upgrade_lock:: missing non-member swap.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/6676 #6676] lock conversion should be explicit.
|
||||
* Added missing packaged_task::result_type and packaged_task:: constructor with allocator.
|
||||
* Added packaged_task::reset()
|
||||
|
||||
|
||||
Fixed Bugs:
|
||||
|
||||
* [@http://svn.boost.org/trac/boost/ticket/2380 #2380] boost::move from lvalue does not work with gcc.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/2430 #2430] shared_mutex for win32 doesn't have timed_lock_upgrade.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/2575 #2575] Bug- Boost 1.36.0 on Itanium platform.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/3160 #3160] Duplicate tutorial code in boost::thread.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/4345 #4345] thread::id and joining problem with cascade of threads.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/4521 #4521] Error using boost::move on packaged_task (MSVC 10).
|
||||
* [@http://svn.boost.org/trac/boost/ticket/4711 #4711] Must use implementation details to return move-only types.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/4921 #4921] BOOST_THREAD_USE_DLL and BOOST_THREAD_USE_LIB are crucial and need to be documented.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/5013 #5013] documentation: boost::thread: pthreas_exit causes terminate().
|
||||
* [@http://svn.boost.org/trac/boost/ticket/5173 #5173] boost::this_thread::get_id is very slow.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/5351 #5351] interrupt a future get boost::unknown_exception.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/5516 #5516] Upgrade lock is not acquired when previous upgrade lock releases if another read lock is present.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/5990 #5990] shared_future<T>::get() has wrong return type.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/6174 #6174] packaged_task doesn't correctly handle moving results.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/6222 #6222] Compile error with SunStudio: unique_future move.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/6354 #6354] PGI: Compiler threading support is not turned on.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/6673 #6673] shared_lock: move assign doesn't works with c++11.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/6674 #6674] shared_mutex: try_lock_upgrade_until doesn't works.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/6908 #6908] Compile error due to unprotected definitions of _WIN32_WINNT and WINVER.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/6940 #6940] TIME_UTC is a macro in C11.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/6959 #6959] call of abs is ambiguous.
|
||||
* Fix issue signaled on the ML with task_object(task_object const&) in presence of task_object(task_object &&)
|
||||
|
||||
|
||||
|
||||
[heading Version 2.1.1 - boost 1.49]
|
||||
|
||||
Fixed Bugs:
|
||||
|
||||
* [@http://svn.boost.org/trac/boost/ticket/2309 #2309] Lack of g++ symbol visibility support in Boost.Thread.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/2639 #2639] documentation should be extended(defer_lock, try_to_lock, ...).
|
||||
|
||||
* [@http://svn.boost.org/trac/boost/ticket/3639 #3639] Boost.Thread doesn't build with Sun-5.9 on Linux.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/3762 #3762] Thread can't be compiled with winscw (Codewarrior by Nokia).
|
||||
* [@http://svn.boost.org/trac/boost/ticket/3885 #3885] document about mix usage of boost.thread and native thread api.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/3975 #3975] Incorrect precondition for promise::set_wait_callback().
|
||||
|
||||
* [@http://svn.boost.org/trac/boost/ticket/4048 #4048] thread::id formatting involves locale
|
||||
* [@http://svn.boost.org/trac/boost/ticket/4315 #4315] gcc 4.4 Warning: inline ... declared as dllimport: attribute ignored.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/4480 #4480] OpenVMS patches for compiler issues workarounds.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/4819 #4819] boost.thread's documentation misprints.
|
||||
|
||||
* [@http://svn.boost.org/trac/boost/ticket/5423 #5423] thread issues with C++0x.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/5617 #5617] boost::thread::id copy ctor.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/5739 #5739] set-but-not-used warnings with gcc-4.6.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/5826 #5826] threads.cpp: resource leak on threads creation failure.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/5839 #5839] thread.cpp: ThreadProxy leaks on exceptions.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/5859 #5859] win32 shared_mutex constructor leaks on exceptions.
|
||||
|
||||
* [@http://svn.boost.org/trac/boost/ticket/6100 #6100] Compute hardware_concurrency() using get_nprocs() on GLIBC systems.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/6168 #6168] recursive_mutex is using wrong config symbol (possible typo).
|
||||
* [@http://svn.boost.org/trac/boost/ticket/6175 #6175] Compile error with SunStudio.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/6200 #6200] patch to have condition_variable and mutex error better handle EINTR.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/6207 #6207] shared_lock swap compiler error on clang 3.0 c++11.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/6208 #6208] try_lock_wrapper swap compiler error on clang 3.0 c++11.
|
||||
|
||||
[heading Version 2.1.0 - Changes since boost 1.40]
|
||||
|
||||
The 1.41.0 release of Boost adds futures to the thread library. There are also a few minor changes.
|
||||
|
||||
[heading Changes since boost 1.35]
|
||||
|
||||
The 1.36.0 release of Boost includes a few new features in the thread library:
|
||||
|
||||
* New generic __lock_multiple_ref__ and __try_lock_multiple_ref__ functions for locking multiple mutexes at once.
|
||||
|
||||
* Rvalue reference support for move semantics where the compilers supports it.
|
||||
|
||||
* A few bugs fixed and missing functions added (including the serious win32 condition variable bug).
|
||||
|
||||
* `scoped_try_lock` types are now backwards-compatible with Boost 1.34.0 and previous releases.
|
||||
|
||||
* Support for passing function arguments to the thread function by supplying additional arguments to the __thread__ constructor.
|
||||
|
||||
* Backwards-compatibility overloads added for `timed_lock` and `timed_wait` functions to allow use of `xtime` for timeouts.
|
||||
|
||||
[heading Version 2.0.0 - Changes since boost 1.34]
|
||||
|
||||
Almost every line of code in __boost_thread__ has been changed since the 1.34 release of boost. However, most of the interface
|
||||
changes have been extensions, so the new code is largely backwards-compatible with the old code. The new features and breaking
|
||||
changes are described below.
|
||||
|
||||
[heading New Features]
|
||||
|
||||
* Instances of __thread__ and of the various lock types are now movable.
|
||||
|
||||
* Threads can be interrupted at __interruption_points__.
|
||||
|
||||
* Condition variables can now be used with any type that implements the __lockable_concept__, through the use of
|
||||
`boost::condition_variable_any` (`boost::condition` is a `typedef` to `boost::condition_variable_any`, provided for backwards
|
||||
compatibility). `boost::condition_variable` is provided as an optimization, and will only work with
|
||||
`boost::unique_lock<boost::mutex>` (`boost::mutex::scoped_lock`).
|
||||
|
||||
* Thread IDs are separated from __thread__, so a thread can obtain it's own ID (using `boost::this_thread::get_id()`), and IDs can
|
||||
be used as keys in associative containers, as they have the full set of comparison operators.
|
||||
|
||||
* Timeouts are now implemented using the Boost DateTime library, through a typedef `boost::system_time` for absolute timeouts, and
|
||||
with support for relative timeouts in many cases. `boost::xtime` is supported for backwards compatibility only.
|
||||
|
||||
* Locks are implemented as publicly accessible templates `boost::lock_guard`, `boost::unique_lock`, `boost::shared_lock`, and
|
||||
`boost::upgrade_lock`, which are templated on the type of the mutex. The __lockable_concept__ has been extended to include publicly
|
||||
available __lock_ref__ and __unlock_ref__ member functions, which are used by the lock types.
|
||||
|
||||
[heading Breaking Changes]
|
||||
|
||||
The list below should cover all changes to the public interface which break backwards compatibility.
|
||||
|
||||
* __try_mutex__ has been removed, and the functionality subsumed into __mutex__. __try_mutex__ is left as a `typedef`,
|
||||
but is no longer a separate class.
|
||||
|
||||
* __recursive_try_mutex__ has been removed, and the functionality subsumed into
|
||||
__recursive_mutex__. __recursive_try_mutex__ is left as a `typedef`, but is no longer a separate class.
|
||||
|
||||
* `boost::detail::thread::lock_ops` has been removed. Code that relies on the `lock_ops` implementation detail will no longer work,
|
||||
as this has been removed, as it is no longer necessary now that mutex types now have public __lock_ref__ and __unlock_ref__ member
|
||||
functions.
|
||||
|
||||
* `scoped_lock` constructors with a second parameter of type `bool` are no longer provided. With previous boost releases,
|
||||
``boost::mutex::scoped_lock some_lock(some_mutex,false);`` could be used to create a lock object that was associated with a mutex,
|
||||
but did not lock it on construction. This facility has now been replaced with the constructor that takes a
|
||||
`boost::defer_lock_type` as the second parameter: ``boost::mutex::scoped_lock some_lock(some_mutex,boost::defer_lock);``
|
||||
|
||||
* The `locked()` member function of the `scoped_lock` types has been renamed to __owns_lock_ref__.
|
||||
|
||||
* You can no longer obtain a __thread__ instance representing the current thread: a default-constructed __thread__ object is not
|
||||
associated with any thread. The only use for such a thread object was to support the comparison operators: this functionality has
|
||||
been moved to __thread_id__.
|
||||
|
||||
* The broken `boost::read_write_mutex` has been replaced with __shared_mutex__.
|
||||
|
||||
* __mutex__ is now never recursive. For Boost releases prior to 1.35 __mutex__ was recursive on Windows and not on POSIX platforms.
|
||||
|
||||
* When using a __recursive_mutex__ with a call to [cond_any_wait_link `boost::condition_variable_any::wait()`], the mutex is only
|
||||
unlocked one level, and not completely. This prior behaviour was not guaranteed and did not feature in the tests.
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:future Future]
|
||||
|
||||
The following features will be included in next releases.
|
||||
|
||||
# Complete the C++11 missing features, in particular
|
||||
* [@http://svn.boost.org/trac/boost/ticket/7285 #7285] C++11 compliance: Allow to pass movable arguments for call_once.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/6227 #6227] C++11 compliance: Use of variadic templates on Generic Locking Algorithms on compilers providing them.
|
||||
|
||||
|
||||
# Add some of the extension proposed in [@http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3428.pdf A Standardized Representation of Asynchronous Operations], in particular
|
||||
|
||||
* [@http://svn.boost.org/trac/boost/ticket/7589 #7589] Synchro: Add polymorphic lockables.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/7449 #7449] Synchro: Add a synchronized value class.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/7445 #7445] Async: Add future<>.then.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/7446 #7446] Async: Add when_any.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/7447 #7447] Async: Add when_all.
|
||||
* [@http://svn.boost.org/trac/boost/ticket/7448 #7448] Async: Add async taking a scheduler parameter.
|
||||
|
||||
|
||||
[endsect]
|
||||
|
||||
109
doc/compliance.qbk
Normal file
109
doc/compliance.qbk
Normal file
@@ -0,0 +1,109 @@
|
||||
[/
|
||||
(C) Copyright 2011-12 Vicente J. Botet Escriba.
|
||||
Distributed under the Boost Software License, Version 1.0.
|
||||
(See accompanying file LICENSE_1_0.txt or copy at
|
||||
http://www.boost.org/LICENSE_1_0.txt).
|
||||
]
|
||||
|
||||
[section:compliance Conformance and Extension]
|
||||
|
||||
[section:cpp11 C++11 standard Thread library]
|
||||
|
||||
|
||||
[table C++11 standard Conformance
|
||||
[[Section] [Description] [Status] [Comments] [Ticket]]
|
||||
[[30] [Thread support library] [Partial] [-] [-]]
|
||||
[[30.1] [General] [-] [-] [-]]
|
||||
[[30.2] [Requirements] [-] [-] [-]]
|
||||
[[30.2.1] [Template parameter names] [-] [-] [-]]
|
||||
[[30.2.2] [Exceptions] [Yes] [-] [-]]
|
||||
[[30.2.3] [Native handles] [Yes] [-] [-]]
|
||||
[[30.2.4] [Timing specifications] [Yes] [-] [-]]
|
||||
[[30.2.5] [Requirements for Lockable types] [Yes] [-] [-]]
|
||||
[[30.2.5.1] [In general] [-] [-] [-]]
|
||||
[[30.2.5.2] [BasicLockable requirements] [Yes] [-] [-]]
|
||||
[[30.2.5.3] [Lockable requirements] [yes] [-] [-]]
|
||||
[[30.2.5.4] [TimedLockable requirements] [Yes] [-] [-]]
|
||||
[[30.2.6] [decay_copy] [-] [-] [-]]
|
||||
[[30.3] [Threads] [Yes] [-] [-]]
|
||||
[[30.3.1] [Class thread] [Yes] [-] [-]]
|
||||
[[30.3.1.1] [Class thread::id] [Yes] [-] [-]]
|
||||
[[30.3.1.2] [thread constructors] [Partial] [-] [-]]
|
||||
[[30.3.1.3] [thread destructor] [Yes] [-] [-]]
|
||||
[[30.3.1.4] [thread assignment] [Yes] [-] [-]]
|
||||
[[30.3.1.5] [thread members] [Yes] [-] [-]]
|
||||
[[30.3.1.6] [thread static members] [Yes] [-] [-]]
|
||||
[[30.3.1.7] [thread specialized algorithms] [Yes] [-] [-]]
|
||||
|
||||
[[30.3.2] [Namespace this_thread] [Yes] [-] [-]]
|
||||
[[30.4] [Mutual exclusion] [Partial] [-] [-]]
|
||||
[[30.4.1] [Mutex requirements] [Yes] [-] [-]]
|
||||
[[30.4.1.1] [In general] [Yes] [-] [-]]
|
||||
[[30.4.1.2] [Mutex types] [Yes] [-] [-]]
|
||||
[[30.4.1.2.1] [Class mutex] [Yes] [-] [-]]
|
||||
[[30.4.1.2.2] [Class recursive_mutex] [Yes] [-] [-]]
|
||||
[[30.4.1.3] [Timed mutex types] [Yes] [-] [-]]
|
||||
[[30.4.1.3.1] [Class timed_mutex] [Yes] [-] [-]]
|
||||
[[30.4.1.3.1] [Class recursive_timed_mutex] [Yes] [-] [-]]
|
||||
[[30.4.2] [Locks] [Yes] [-] [-]]
|
||||
[[30.4.2.1] [Class template lock_guard] [Yes] [-] [-]]
|
||||
[[30.4.2.2] [Class template unique_lock] [Yes] [-] [-]]
|
||||
[[30.4.2.2.1] [unique_lock constructors, destructor, and assignment] [Yes] [-] [-]]
|
||||
[[30.4.2.2.2] [unique_lock locking] [Yes] [-] [-]]
|
||||
[[30.4.2.2.3] [unique_lock modifiers] [Yes] [-] [-]]
|
||||
[[30.4.2.2.4] [unique_lock observers] [Yes] [] [-]]
|
||||
[[30.4.3] [Generic locking algorithms] [Partial] [variadic] [#6227]]
|
||||
[[30.4.4] [Call once] [Partial] [call_once] [#7285]]
|
||||
[[30.4.4.1] [Struct once_flag] [Yes] [-] [-]]
|
||||
[[30.4.4.2] [Function call_once] [Partial] [interface] [#7285]]
|
||||
[[30.5] [Condition variables] [Yes] [-] [-]]
|
||||
[[30.5.1] [Class condition_variable] [Yes] [-] [-]]
|
||||
[[30.5.2] [Class condition_variable_any] [Yes] [-] [-]]
|
||||
[[30.6] [Futures] [Partial] [noexcept] [#7279]]
|
||||
[[30.6.1] [Overview] [Partial] [-] [-]]
|
||||
[[30.6.2] [Error handling] [Yes] [-] [-]]
|
||||
[[30.6.3] [Class future_error] [Partial] [noexcept] [#7279]]
|
||||
[[30.6.4] [Shared state] [-] [-] [-]]
|
||||
[[30.6.5] [Class template promise] [Yes] [-] [-]]
|
||||
[[30.6.6] [Class template future] [Yes] [-] [-]]
|
||||
[[30.6.7] [Class template shared_future] [Yes] [-] [-]]
|
||||
[[30.6.8] [Function template async] [Yes] [-] [-]]
|
||||
[[30.6.9] [Class template packaged_task] [Yes] [-] [-]]
|
||||
]
|
||||
|
||||
[/
|
||||
[table Extension
|
||||
[[Section] [Description] [Comments]]
|
||||
[[30.3.1.5.x] [interrupt] [-]]
|
||||
[[30.3.2.x] [Interruption] [-]]
|
||||
[[30.3.2.y] [at_thread_exit] [-]]
|
||||
[[30.4.3.x] [Generic locking algorithms begin/end] [-]]
|
||||
[[30.x] [Barriers] [-]]
|
||||
[[30.y] [Thread Local Storage] [-]]
|
||||
[[30.z] [Class thread_group] [-]]
|
||||
]
|
||||
]
|
||||
[endsect]
|
||||
|
||||
|
||||
[section:shared Shared Locking extensions]
|
||||
|
||||
[table Howard's Shared Locking Proposal Conformance
|
||||
[[Section] [Description] [Status] [Comments]]
|
||||
[[X] [Shared Locking] [Yes] [Needs `BOOST_THREAD_PROVIDES_SHARED_MUTEX_UPWARDS_CONVERSION]]
|
||||
[[X.1] [Shared Lockables Concepts] [Yes] [ - ]]
|
||||
[[X.1.1] [SharedLockable concept] [Yes] [ - ]]
|
||||
[[X.1.2] [UpgradeLockable concept] [Yes] [ - ]]
|
||||
[[X.2] [Shared Mutex Types] [Yes] [ - ]]
|
||||
[[X.2.1] [shared_mutex class] [Yes] [ - ]]
|
||||
[[X.2.2] [upgrade_mutex class] [Yes] [ - ]]
|
||||
[[X.3] [Locks] [Yes] [-]]
|
||||
[[X.3.1] [unique_lock class adaptations] [Yes] [-]]
|
||||
[[X.3.2] [shared_lock class] [Yes] [ - ]]
|
||||
[[X.3.3] [upgrade_lock class] [Yes] [-]]
|
||||
]
|
||||
|
||||
[endsect]
|
||||
|
||||
|
||||
[endsect]
|
||||
@@ -1,338 +0,0 @@
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content=
|
||||
"text/html; charset=iso-8859-1">
|
||||
<meta name="keywords" content="threads, BTL, thread library, C++">
|
||||
<link rel="stylesheet" type="text/css" href="styles.css">
|
||||
|
||||
<title>Boost.Threads, condition</title>
|
||||
</head>
|
||||
|
||||
<body bgcolor="#ffffff" link="#0000ff" vlink="#800080" text="#000000">
|
||||
<table summary="header" border="0" cellpadding="7" cellspacing="0"
|
||||
width="100%">
|
||||
<tr>
|
||||
<td valign="top" width="300">
|
||||
<h3><img height="86" alt="C++ Boost" src=
|
||||
"../../../c++boost.gif" width="277"></h3>
|
||||
</td>
|
||||
|
||||
<td valign="top">
|
||||
<h1 align="center">Boost.Threads</h1>
|
||||
|
||||
<h2 align="center">condition</h2>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<hr>
|
||||
|
||||
<p><a href="#Introduction">Introduction</a><br>
|
||||
<a href="#Header">Header</a><br>
|
||||
<a href="#Synopsis">Synopsis</a><br>
|
||||
<a href="#Members">Members</a><br>
|
||||
<a href="#Example">Example</a></p>
|
||||
|
||||
<h2><a name="Introduction">Introduction</a></h2>
|
||||
|
||||
<p>An object of class <code>condition</code> is a synchronization
|
||||
primitive used to cause a thread to wait until a particular shared-data
|
||||
condition (or time) is met. A <code>condition</code> object is always
|
||||
used in conjunction with a mutex object modeling a <a href=
|
||||
"mutex_concept.html">Mutex Concept</a>. The mutex must be locked prior
|
||||
to waiting on the <code>condition</code>, which is ensured by passing a
|
||||
lock object modeling a <a href="lock_concept.html">Lock Concept</a> to
|
||||
the <code>condition</code> object's <code>wait</code> functions.
|
||||
While the thread is waiting on the <code>condition</code> object, the
|
||||
mutex associated with the lock is unlocked. When the thread returns
|
||||
from a call to one of the <code>condition</code> object's <code>
|
||||
wait</code> functions, the mutex is again locked. The tricky
|
||||
lock/unlock/lock sequence is performed automatically by the <code>
|
||||
condition</code> object's <code>wait</code> functions.</p>
|
||||
|
||||
<p>The <code>condition</code> type is often used to implement the <i>
|
||||
Monitor Object</i> and other important patterns. See <a href=
|
||||
"bibliography.html#Schmidt-00">[Schmidt-00]</a> and <a href=
|
||||
"bibliography.html#Hoare-74">[Hoare 74]</a>. Monitors are one of the
|
||||
most important patterns for creating reliable multithreaded
|
||||
programs.</p>
|
||||
|
||||
<p>See <a href="definitions.html">Formal Definitions</a> for
|
||||
definitions of thread states <a href="definitions.html#state">
|
||||
blocked</a> and <a href="definitions.html#state">ready</a>. Note that
|
||||
"waiting" is a synonym for blocked.</p>
|
||||
|
||||
<h2><a name="Header">Header</a></h2>
|
||||
<pre>
|
||||
#include <a href=
|
||||
"../../../boost/thread/condition.hpp"><boost/thread/condition.hpp></a>
|
||||
</pre>
|
||||
|
||||
<h2><a name="Synopsis">Synopsis</a></h2>
|
||||
<pre>
|
||||
namespace boost {
|
||||
|
||||
class condition : private <a href=
|
||||
"../../utility/utility.htm#Class noncopyable">boost::noncopyable</a> // Exposition only.
|
||||
// Class condition meets the <a href=
|
||||
"overview.html#NonCopyable">NonCopyable</a> requirement.
|
||||
{
|
||||
public:
|
||||
condition();
|
||||
~condition();
|
||||
|
||||
void notify_one();
|
||||
void notify_all();
|
||||
template <typename <a href="scoped_lock.html">ScopedLock</a>>
|
||||
void wait(<a href="scoped_lock.html">ScopedLock</a>& lock);
|
||||
template <typename <a href="scoped_lock.html">ScopedLock</a>, typename <a
|
||||
href="http://www.sgi.com/tech/stl/Predicate.html">Predicate</a>>
|
||||
void wait(<a href="scoped_lock.html">ScopedLock</a>& lock, <a href=
|
||||
"http://www.sgi.com/tech/stl/Predicate.html">Predicate</a> pred);
|
||||
template <typename <a href="scoped_lock.html">ScopedLock</a>>
|
||||
bool timed_wait(<a href=
|
||||
"scoped_lock.html">ScopedLock</a>& lock, const xtime& xt);
|
||||
template <typename <a href="scoped_lock.html">ScopedLock</a>, typename <a
|
||||
href="http://www.sgi.com/tech/stl/Predicate.html">Predicate</a>>
|
||||
bool timed_wait(<a href=
|
||||
"scoped_lock.html">ScopedLock</a>& lock, const xtime& xt, <a href=
|
||||
"http://www.sgi.com/tech/stl/Predicate.html">Predicate</a> pred);
|
||||
};
|
||||
|
||||
} // namespace boost
|
||||
</pre>
|
||||
|
||||
<h2><a name="Members">Members</a></h2>
|
||||
<hr>
|
||||
|
||||
<h3>Constructor</h3>
|
||||
<pre>
|
||||
condition();
|
||||
</pre>
|
||||
|
||||
<p><b>Effects:</b> Constructs a <code>condition</code>.</p>
|
||||
<hr>
|
||||
|
||||
<h3>Destructor</h3>
|
||||
<pre>
|
||||
~condition();
|
||||
</pre>
|
||||
|
||||
<p><b>Effects:</b> Destroys <code>*this</code>.</p>
|
||||
<hr>
|
||||
|
||||
<h3>notify_one</h3>
|
||||
<pre>
|
||||
void notify_one();
|
||||
</pre>
|
||||
|
||||
<p><b>Effects:</b> If there is a thread waiting on <code>*this</code>,
|
||||
change that thread's state to ready. Otherwise there is no
|
||||
effect.</p>
|
||||
|
||||
<p><b>Notes:</b> If more that one thread is waiting on the condition,
|
||||
it is unspecified which is made ready.</p>
|
||||
<hr>
|
||||
|
||||
<h3>notify_all</h3>
|
||||
<pre>
|
||||
void notify_all();
|
||||
</pre>
|
||||
|
||||
<p><b>Effects:</b> Change the state of all threads waiting on <code>
|
||||
*this</code> to ready. If there are no waiting threads, <code>
|
||||
notify_all()</code> has no effect.</p>
|
||||
<hr>
|
||||
|
||||
<h3>wait</h3>
|
||||
<pre>
|
||||
template <typename ScopedLock>
|
||||
void wait(ScopedLock& lock);
|
||||
</pre>
|
||||
|
||||
<p><b>Requires:</b> ScopedLock meets the <a href=
|
||||
"lock_concept.html#ScopedLock">ScopedLock</a> requirements.</p>
|
||||
|
||||
<p><b>Effects:</b> Releases the lock on the <a href=
|
||||
"mutex_concept.html">mutex model</a> associated with <code>lock</code>,
|
||||
blocks the current thread of execution until readied by a call to
|
||||
<code>this->notify_one()</code> or <code>
|
||||
this->notify_all()</code>, and then reacquires the lock. All effects
|
||||
occur in an atomic fashion.</p>
|
||||
|
||||
<p><b>Throws:</b> <code><a href="lock_error.html">lock_error</a></code>
|
||||
if <code>!lock.locked()</code></p>
|
||||
|
||||
<p><b>Danger:</b> This version should always be used within a loop
|
||||
checking that the state logically associated with the <code>
|
||||
condition</code> has become true. Without the loop, race conditions can
|
||||
ensue due to possible "spurious wake ups". The second version
|
||||
encapsulates this loop idiom internally and is generally the preferred
|
||||
method.</p>
|
||||
<pre>
|
||||
template <typename ScopedLock, typename Pr>
|
||||
void wait(ScopedLock& lock, Pr pred);
|
||||
</pre>
|
||||
|
||||
<p><b>Requires:</b> ScopedLock meets the <a href=
|
||||
"lock_concept.html#ScopedLock">ScopedLock</a> requirements, return from
|
||||
<code>pred()</code> convertible to bool.</p>
|
||||
|
||||
<p><b>Effects:</b> As if:</p>
|
||||
<code> while (!pred()) wait(lock)</code>
|
||||
|
||||
<p><b>Throws:</b> <code><a href="lock_error.html">lock_error</a></code>
|
||||
if <code>!lock.locked()</code></p>
|
||||
<hr>
|
||||
|
||||
<h3>timed_wait</h3>
|
||||
<pre>
|
||||
template <typename ScopedLock>
|
||||
bool timed_wait(ScopedLock& lock, const <a href=
|
||||
"xtime.html">xtime</a>& xt);
|
||||
</pre>
|
||||
|
||||
<p><b>Requires:</b> ScopedLock meets the <a href=
|
||||
"lock_concept.html#ScopedLock">ScopedLock</a>
|
||||
requirements.</p>
|
||||
|
||||
<p><b>Effects:</b> Releases the lock on the <a href=
|
||||
"mutex_concept.html">mutex model</a> associated with the <code>
|
||||
lock</code>, blocks the current thread of execution until readied by a
|
||||
call to <code>this->notify_one()</code> or <code>
|
||||
this->notify_all()</code>, or until <code>xt</code>, and then
|
||||
reacquires the lock. All effects occur in an atomic fashion.</p>
|
||||
|
||||
<p><b>Throws:</b> <code><a href="lock_error.html">lock_error</a></code>
|
||||
if <code>!lock.locked()</code></p>
|
||||
|
||||
<p><b>Danger:</b> This version should always be used within a loop
|
||||
checking that the state logically associated with the <code>
|
||||
condition</code> has become true. Without the loop, race conditions can
|
||||
ensue due to "spurious wake ups". The second version
|
||||
encapsulates this loop idiom internally and is generally the preferred
|
||||
method.</p>
|
||||
|
||||
<p><b>Returns:</b> <code>false</code> if <code>xt</code> is reached,
|
||||
otherwise <code>true</code>.</p>
|
||||
<pre>
|
||||
template <typename ScopedLock, typename Pr>
|
||||
bool timed_wait(ScopedLock& lock, const <a href=
|
||||
"xtime.html">xtime</a>& xt, Pr pred);
|
||||
</pre>
|
||||
|
||||
<p><b>Requires:</b> ScopedLock meets the <a href=
|
||||
"lock_concept.html#ScopedLock">ScopedLock</a> requirements,
|
||||
return from <code>pred()</code> convertible to bool.</p>
|
||||
|
||||
<p><b>Effects:</b> As if:</p>
|
||||
<code> while (!pred())<br>
|
||||
{<br>
|
||||
if (!timed_wait(lock, xt))<br>
|
||||
return
|
||||
false;<br>
|
||||
}</code>
|
||||
|
||||
<p><b>Throws:</b> <code><a href="lock_error.html">lock_error</a></code>
|
||||
if <code>!lock.locked()</code></p>
|
||||
|
||||
<p><b>Returns:</b> <code>false</code> if <code>xt</code> is reached,
|
||||
otherwise <code>true</code>.</p>
|
||||
<hr>
|
||||
|
||||
<h2><a name="Example">Example Usage</a></h2>
|
||||
<pre>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <a href="../../../boost/utility.hpp"><boost/utility.hpp></a>
|
||||
#include <a href=
|
||||
"../../../boost/thread/condition.hpp"><boost/thread/condition.hpp></a>
|
||||
#include <a href=
|
||||
"../../../boost/thread/thread.hpp"><boost/thread/thread.hpp></a>
|
||||
|
||||
class bounded_buffer : private boost::noncopyable
|
||||
{
|
||||
public:
|
||||
typedef boost::mutex::scoped_lock lock;
|
||||
|
||||
bounded_buffer(int n) : begin(0), end(0), buffered(0), circular_buf(n) { }
|
||||
|
||||
void send (int m) {
|
||||
lock lk(monitor);
|
||||
while (buffered == circular_buf.size())
|
||||
buffer_not_full.wait(lk);
|
||||
circular_buf[end] = m;
|
||||
end = (end+1) % circular_buf.size();
|
||||
++buffered;
|
||||
buffer_not_empty.notify_one();
|
||||
}
|
||||
int receive() {
|
||||
lock lk(monitor);
|
||||
while (buffered == 0)
|
||||
buffer_not_empty.wait(lk);
|
||||
int i = circular_buf[begin];
|
||||
begin = (begin+1) % circular_buf.size();
|
||||
--buffered;
|
||||
buffer_not_full.notify_one();
|
||||
return i;
|
||||
}
|
||||
|
||||
private:
|
||||
int begin, end, buffered;
|
||||
std::vector<int> circular_buf;
|
||||
boost::condition buffer_not_full, buffer_not_empty;
|
||||
boost::mutex monitor;
|
||||
};
|
||||
|
||||
bounded_buffer buf(2);
|
||||
|
||||
void sender() {
|
||||
int n = 0;
|
||||
while (n < 100) {
|
||||
buf.send(n);
|
||||
std::cout << "sent: " << n << std::endl;
|
||||
++n;
|
||||
}
|
||||
buf.send(-1);
|
||||
}
|
||||
|
||||
void receiver() {
|
||||
int n;
|
||||
do {
|
||||
n = buf.receive();
|
||||
std::cout << "received: " << n << std::endl;
|
||||
} while (n != -1); // -1 indicates end of buffer
|
||||
}
|
||||
|
||||
int main(int, char*[])
|
||||
{
|
||||
boost::thread thrd1(&sender);
|
||||
boost::thread thrd2(&receiver);
|
||||
thrd1.join();
|
||||
thrd2.join();
|
||||
return 0;
|
||||
}
|
||||
</pre>
|
||||
|
||||
<p>Typical output (dependent on scheduling policies) is:</p>
|
||||
<pre>
|
||||
sent: 0
|
||||
sent: 1
|
||||
received: 0
|
||||
received: 1
|
||||
sent: 2
|
||||
sent: 3
|
||||
received: 2
|
||||
received: 3
|
||||
sent: 4
|
||||
received: 4
|
||||
</pre>
|
||||
<hr>
|
||||
|
||||
<p>Revised
|
||||
<!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan -->05 November, 2001<!--webbot bot="Timestamp" endspan i-checksum="39359" --></p>
|
||||
|
||||
<p><i>© Copyright <a href="mailto:williamkempf@hotmail.com">
|
||||
William E. Kempf</a> 2001 all rights reserved.</i></p>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
790
doc/condition_variables.qbk
Normal file
790
doc/condition_variables.qbk
Normal file
@@ -0,0 +1,790 @@
|
||||
[/
|
||||
(C) Copyright 2007-11 Anthony Williams.
|
||||
(C) Copyright 2011-12 Vicente J. Botet Escriba.
|
||||
Distributed under the Boost Software License, Version 1.0.
|
||||
(See accompanying file LICENSE_1_0.txt or copy at
|
||||
http://www.boost.org/LICENSE_1_0.txt).
|
||||
]
|
||||
|
||||
[section:condvar_ref Condition Variables]
|
||||
|
||||
[heading Synopsis]
|
||||
|
||||
namespace boost
|
||||
{
|
||||
enum class cv_status;
|
||||
{
|
||||
no_timeout,
|
||||
timeout
|
||||
};
|
||||
class condition_variable;
|
||||
class condition_variable_any;
|
||||
void notify_all_at_thread_exit(condition_variable& cond, unique_lock<mutex> lk);
|
||||
}
|
||||
|
||||
The classes `condition_variable` and `condition_variable_any` provide a
|
||||
mechanism for one thread to wait for notification from another thread that a
|
||||
particular condition has become true. The general usage pattern is that one
|
||||
thread locks a mutex and then calls `wait` on an instance of
|
||||
`condition_variable` or `condition_variable_any`. When the thread is woken from
|
||||
the wait, then it checks to see if the appropriate condition is now true, and
|
||||
continues if so. If the condition is not true, then the thread then calls `wait`
|
||||
again to resume waiting. In the simplest case, this condition is just a boolean
|
||||
variable:
|
||||
|
||||
boost::condition_variable cond;
|
||||
boost::mutex mut;
|
||||
bool data_ready;
|
||||
|
||||
void process_data();
|
||||
|
||||
void wait_for_data_to_process()
|
||||
{
|
||||
boost::unique_lock<boost::mutex> lock(mut);
|
||||
while(!data_ready)
|
||||
{
|
||||
cond.wait(lock);
|
||||
}
|
||||
process_data();
|
||||
}
|
||||
|
||||
Notice that the `lock` is passed to `wait`: `wait` will atomically add the
|
||||
thread to the set of threads waiting on the condition variable, and unlock the
|
||||
mutex. When the thread is woken, the mutex will be locked again before the call
|
||||
to `wait` returns. This allows other threads to acquire the mutex in order to
|
||||
update the shared data, and ensures that the data associated with the condition
|
||||
is correctly synchronized.
|
||||
|
||||
In the mean time, another thread sets the condition to `true`, and then calls
|
||||
either `notify_one` or `notify_all` on the condition variable to wake one
|
||||
waiting thread or all the waiting threads respectively.
|
||||
|
||||
void retrieve_data();
|
||||
void prepare_data();
|
||||
|
||||
void prepare_data_for_processing()
|
||||
{
|
||||
retrieve_data();
|
||||
prepare_data();
|
||||
{
|
||||
boost::lock_guard<boost::mutex> lock(mut);
|
||||
data_ready=true;
|
||||
}
|
||||
cond.notify_one();
|
||||
}
|
||||
|
||||
Note that the same mutex is locked before the shared data is updated, but that
|
||||
the mutex does not have to be locked across the call to `notify_one`.
|
||||
|
||||
This example uses an object of type `condition_variable`, but would work just as
|
||||
well with an object of type `condition_variable_any`: `condition_variable_any`
|
||||
is more general, and will work with any kind of lock or mutex, whereas
|
||||
`condition_variable` requires that the lock passed to `wait` is an instance of
|
||||
`boost::unique_lock<boost::mutex>`. This enables `condition_variable` to make
|
||||
optimizations in some cases, based on the knowledge of the mutex type;
|
||||
`condition_variable_any` typically has a more complex implementation than
|
||||
`condition_variable`.
|
||||
|
||||
[section:condition_variable Class `condition_variable`]
|
||||
|
||||
//#include <boost/thread/condition_variable.hpp>
|
||||
|
||||
namespace boost
|
||||
{
|
||||
class condition_variable
|
||||
{
|
||||
public:
|
||||
condition_variable();
|
||||
~condition_variable();
|
||||
|
||||
void notify_one() noexcept;
|
||||
void notify_all() noexcept;
|
||||
|
||||
void wait(boost::unique_lock<boost::mutex>& lock);
|
||||
|
||||
template<typename predicate_type>
|
||||
void wait(boost::unique_lock<boost::mutex>& lock,predicate_type predicate);
|
||||
|
||||
template <class Clock, class Duration>
|
||||
typename cv_status::type
|
||||
wait_until(
|
||||
unique_lock<mutex>& lock,
|
||||
const chrono::time_point<Clock, Duration>& t);
|
||||
|
||||
template <class Clock, class Duration, class Predicate>
|
||||
bool
|
||||
wait_until(
|
||||
unique_lock<mutex>& lock,
|
||||
const chrono::time_point<Clock, Duration>& t,
|
||||
Predicate pred);
|
||||
|
||||
template <class Rep, class Period>
|
||||
typename cv_status::type
|
||||
wait_for(
|
||||
unique_lock<mutex>& lock,
|
||||
const chrono::duration<Rep, Period>& d);
|
||||
|
||||
template <class Rep, class Period, class Predicate>
|
||||
bool
|
||||
wait_for(
|
||||
unique_lock<mutex>& lock,
|
||||
const chrono::duration<Rep, Period>& d,
|
||||
Predicate pred);
|
||||
|
||||
#if defined BOOST_THREAD_USES_DATETIME
|
||||
bool timed_wait(boost::unique_lock<boost::mutex>& lock,boost::system_time const& abs_time);
|
||||
template<typename duration_type>
|
||||
bool timed_wait(boost::unique_lock<boost::mutex>& lock,duration_type const& rel_time);
|
||||
template<typename predicate_type>
|
||||
bool timed_wait(boost::unique_lock<boost::mutex>& lock,boost::system_time const& abs_time,predicate_type predicate);
|
||||
template<typename duration_type,typename predicate_type>
|
||||
bool timed_wait(boost::unique_lock<boost::mutex>& lock,duration_type const& rel_time,predicate_type predicate);
|
||||
bool timed_wait(boost::unique_lock<boost::mutex>& lock,boost::xtime const& abs_time);
|
||||
|
||||
template<typename predicate_type>
|
||||
bool timed_wait(boost::unique_lock<boost::mutex>& lock,boost::xtime const& abs_time,predicate_type predicate);
|
||||
#endif
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
[section:constructor `condition_variable()`]
|
||||
|
||||
[variablelist
|
||||
|
||||
[[Effects:] [Constructs an object of class `condition_variable`.]]
|
||||
|
||||
[[Throws:] [__thread_resource_error__ if an error occurs.]]
|
||||
|
||||
]
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:destructor `~condition_variable()`]
|
||||
|
||||
[variablelist
|
||||
|
||||
[[Precondition:] [All threads waiting on `*this` have been notified by a call to
|
||||
`notify_one` or `notify_all` (though the respective calls to `wait` or
|
||||
`timed_wait` need not have returned).]]
|
||||
|
||||
[[Effects:] [Destroys the object.]]
|
||||
|
||||
[[Throws:] [Nothing.]]
|
||||
|
||||
]
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:notify_one `void notify_one()`]
|
||||
|
||||
[variablelist
|
||||
|
||||
[[Effects:] [If any threads are currently __blocked__ waiting on `*this` in a call
|
||||
to `wait` or `timed_wait`, unblocks one of those threads.]]
|
||||
|
||||
[[Throws:] [Nothing.]]
|
||||
|
||||
]
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:notify_all `void notify_all()`]
|
||||
|
||||
[variablelist
|
||||
|
||||
[[Effects:] [If any threads are currently __blocked__ waiting on `*this` in a call
|
||||
to `wait` or `timed_wait`, unblocks all of those threads.]]
|
||||
|
||||
[[Throws:] [Nothing.]]
|
||||
|
||||
]
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:wait `void wait(boost::unique_lock<boost::mutex>& lock)`]
|
||||
|
||||
[variablelist
|
||||
|
||||
[[Precondition:] [`lock` is locked by the current thread, and either no other
|
||||
thread is currently waiting on `*this`, or the execution of the `mutex()` member
|
||||
function on the `lock` objects supplied in the calls to `wait` or `timed_wait`
|
||||
in all the threads currently waiting on `*this` would return the same value as
|
||||
`lock->mutex()` for this call to `wait`.]]
|
||||
|
||||
[[Effects:] [Atomically call `lock.unlock()` and blocks the current thread. The
|
||||
thread will unblock when notified by a call to `this->notify_one()` or
|
||||
`this->notify_all()`, or spuriously. When the thread is unblocked (for whatever
|
||||
reason), the lock is reacquired by invoking `lock.lock()` before the call to
|
||||
`wait` returns. The lock is also reacquired by invoking `lock.lock()` if the
|
||||
function exits with an exception.]]
|
||||
|
||||
[[Postcondition:] [`lock` is locked by the current thread.]]
|
||||
|
||||
[[Throws:] [__thread_resource_error__ if an error
|
||||
occurs. __thread_interrupted__ if the wait was interrupted by a call to
|
||||
__interrupt__ on the __thread__ object associated with the current thread of execution.]]
|
||||
|
||||
]
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:wait_predicate `template<typename predicate_type> void wait(boost::unique_lock<boost::mutex>& lock, predicate_type pred)`]
|
||||
|
||||
[variablelist
|
||||
|
||||
[[Effects:] [As-if ``
|
||||
while(!pred())
|
||||
{
|
||||
wait(lock);
|
||||
}
|
||||
``]]
|
||||
|
||||
]
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:timed_wait `bool timed_wait(boost::unique_lock<boost::mutex>& lock,boost::system_time const& abs_time)`]
|
||||
|
||||
[variablelist
|
||||
|
||||
[[Precondition:] [`lock` is locked by the current thread, and either no other
|
||||
thread is currently waiting on `*this`, or the execution of the `mutex()` member
|
||||
function on the `lock` objects supplied in the calls to `wait` or `timed_wait`
|
||||
in all the threads currently waiting on `*this` would return the same value as
|
||||
`lock->mutex()` for this call to `wait`.]]
|
||||
|
||||
[[Effects:] [Atomically call `lock.unlock()` and blocks the current thread. The
|
||||
thread will unblock when notified by a call to `this->notify_one()` or
|
||||
`this->notify_all()`, when the time as reported by `boost::get_system_time()`
|
||||
would be equal to or later than the specified `abs_time`, or spuriously. When
|
||||
the thread is unblocked (for whatever reason), the lock is reacquired by
|
||||
invoking `lock.lock()` before the call to `wait` returns. The lock is also
|
||||
reacquired by invoking `lock.lock()` if the function exits with an exception.]]
|
||||
|
||||
[[Returns:] [`false` if the call is returning because the time specified by
|
||||
`abs_time` was reached, `true` otherwise.]]
|
||||
|
||||
[[Postcondition:] [`lock` is locked by the current thread.]]
|
||||
|
||||
[[Throws:] [__thread_resource_error__ if an error
|
||||
occurs. __thread_interrupted__ if the wait was interrupted by a call to
|
||||
__interrupt__ on the __thread__ object associated with the current thread of execution.]]
|
||||
|
||||
]
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:timed_wait_rel `template<typename duration_type> bool timed_wait(boost::unique_lock<boost::mutex>& lock,duration_type const& rel_time)`]
|
||||
|
||||
[variablelist
|
||||
|
||||
[[Precondition:] [`lock` is locked by the current thread, and either no other
|
||||
thread is currently waiting on `*this`, or the execution of the `mutex()` member
|
||||
function on the `lock` objects supplied in the calls to `wait` or `timed_wait`
|
||||
in all the threads currently waiting on `*this` would return the same value as
|
||||
`lock->mutex()` for this call to `wait`.]]
|
||||
|
||||
[[Effects:] [Atomically call `lock.unlock()` and blocks the current thread. The
|
||||
thread will unblock when notified by a call to `this->notify_one()` or
|
||||
`this->notify_all()`, after the period of time indicated by the `rel_time`
|
||||
argument has elapsed, or spuriously. When the thread is unblocked (for whatever
|
||||
reason), the lock is reacquired by invoking `lock.lock()` before the call to
|
||||
`wait` returns. The lock is also reacquired by invoking `lock.lock()` if the
|
||||
function exits with an exception.]]
|
||||
|
||||
[[Returns:] [`false` if the call is returning because the time period specified
|
||||
by `rel_time` has elapsed, `true` otherwise.]]
|
||||
|
||||
[[Postcondition:] [`lock` is locked by the current thread.]]
|
||||
|
||||
[[Throws:] [__thread_resource_error__ if an error
|
||||
occurs. __thread_interrupted__ if the wait was interrupted by a call to
|
||||
__interrupt__ on the __thread__ object associated with the current thread of execution.]]
|
||||
|
||||
]
|
||||
|
||||
[note The duration overload of timed_wait is difficult to use correctly. The overload taking a predicate should be preferred in most cases.]
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:timed_wait_predicate `template<typename predicate_type> bool timed_wait(boost::unique_lock<boost::mutex>& lock, boost::system_time const& abs_time, predicate_type pred)`]
|
||||
|
||||
[variablelist
|
||||
|
||||
[[Effects:] [As-if ``
|
||||
while(!pred())
|
||||
{
|
||||
if(!timed_wait(lock,abs_time))
|
||||
{
|
||||
return pred();
|
||||
}
|
||||
}
|
||||
return true;
|
||||
``]]
|
||||
|
||||
]
|
||||
|
||||
[endsect]
|
||||
|
||||
|
||||
[section:wait_until `template <class Clock, class Duration> cv_status wait_until(boost::unique_lock<boost::mutex>& lock, const chrono::time_point<Clock, Duration>& abs_time)`]
|
||||
|
||||
[variablelist
|
||||
|
||||
[[Precondition:] [`lock` is locked by the current thread, and either no other
|
||||
thread is currently waiting on `*this`, or the execution of the `mutex()` member
|
||||
function on the `lock` objects supplied in the calls to `wait` or `wait_for` or `wait_until`
|
||||
in all the threads currently waiting on `*this` would return the same value as
|
||||
`lock->mutex()` for this call to `wait`.]]
|
||||
|
||||
[[Effects:] [Atomically call `lock.unlock()` and blocks the current thread. The
|
||||
thread will unblock when notified by a call to `this->notify_one()` or
|
||||
`this->notify_all()`, when the time as reported by `Clock::now()`
|
||||
would be equal to or later than the specified `abs_time`, or spuriously. When
|
||||
the thread is unblocked (for whatever reason), the lock is reacquired by
|
||||
invoking `lock.lock()` before the call to `wait` returns. The lock is also
|
||||
reacquired by invoking `lock.lock()` if the function exits with an exception.]]
|
||||
|
||||
[[Returns:] [`cv_status::timeout` if the call is returning because the time specified by
|
||||
`abs_time` was reached, `cv_status::no_timeout` otherwise.]]
|
||||
|
||||
[[Postcondition:] [`lock` is locked by the current thread.]]
|
||||
|
||||
[[Throws:] [__thread_resource_error__ if an error
|
||||
occurs. __thread_interrupted__ if the wait was interrupted by a call to
|
||||
__interrupt__ on the __thread__ object associated with the current thread of execution.]]
|
||||
|
||||
]
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:wait_for `template <class Rep, class Period> cv_status wait_for(boost::unique_lock<boost::mutex>& lock, const chrono::duration<Rep, Period>& rel_time)`]
|
||||
|
||||
|
||||
[variablelist
|
||||
|
||||
[[Precondition:] [`lock` is locked by the current thread, and either no other
|
||||
thread is currently waiting on `*this`, or the execution of the `mutex()` member
|
||||
function on the `lock` objects supplied in the calls to `wait` or `wait_until` or `wait_for`
|
||||
in all the threads currently waiting on `*this` would return the same value as
|
||||
`lock->mutex()` for this call to `wait`.]]
|
||||
|
||||
[[Effects:] [Atomically call `lock.unlock()` and blocks the current thread. The
|
||||
thread will unblock when notified by a call to `this->notify_one()` or
|
||||
`this->notify_all()`, after the period of time indicated by the `rel_time`
|
||||
argument has elapsed, or spuriously. When the thread is unblocked (for whatever
|
||||
reason), the lock is reacquired by invoking `lock.lock()` before the call to
|
||||
`wait` returns. The lock is also reacquired by invoking `lock.lock()` if the
|
||||
function exits with an exception.]]
|
||||
|
||||
[[Returns:] [`cv_status::timeout ` if the call is returning because the time period specified
|
||||
by `rel_time` has elapsed, `cv_status::no_timeout ` otherwise.]]
|
||||
|
||||
[[Postcondition:] [`lock` is locked by the current thread.]]
|
||||
|
||||
[[Throws:] [__thread_resource_error__ if an error
|
||||
occurs. __thread_interrupted__ if the wait was interrupted by a call to
|
||||
__interrupt__ on the __thread__ object associated with the current thread of execution.]]
|
||||
|
||||
]
|
||||
|
||||
[note The duration overload of timed_wait is difficult to use correctly. The overload taking a predicate should be preferred in most cases.]
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:wait_until_predicate `template <class Clock, class Duration, class Predicate> bool wait_until(boost::unique_lock<boost::mutex>& lock, const chrono::time_point<Clock, Duration>& abs_time, Predicate pred)`]
|
||||
|
||||
|
||||
[variablelist
|
||||
|
||||
[[Effects:] [As-if ``
|
||||
while(!pred())
|
||||
{
|
||||
if(!wait_until(lock,abs_time))
|
||||
{
|
||||
return pred();
|
||||
}
|
||||
}
|
||||
return true;
|
||||
``]]
|
||||
|
||||
]
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:wait_for_predicate `template <class Rep, class Period, class Predicate> bool wait_for(boost::unique_lock<boost::mutex>& lock, const chrono::duration<Rep, Period>& rel_time, Predicate pred)`]
|
||||
|
||||
|
||||
[variablelist
|
||||
|
||||
[[Effects:] [As-if ``
|
||||
return wait_until(lock, chrono::steady_clock::now() + d, boost::move(pred));
|
||||
``]]
|
||||
|
||||
]
|
||||
|
||||
[endsect]
|
||||
|
||||
|
||||
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:condition_variable_any Class `condition_variable_any`]
|
||||
|
||||
//#include <boost/thread/condition_variable.hpp>
|
||||
|
||||
namespace boost
|
||||
{
|
||||
class condition_variable_any
|
||||
{
|
||||
public:
|
||||
condition_variable_any();
|
||||
~condition_variable_any();
|
||||
|
||||
void notify_one();
|
||||
void notify_all();
|
||||
|
||||
template<typename lock_type>
|
||||
void wait(lock_type& lock);
|
||||
|
||||
template<typename lock_type,typename predicate_type>
|
||||
void wait(lock_type& lock,predicate_type predicate);
|
||||
|
||||
template <class lock_type, class Clock, class Duration>
|
||||
cv_status wait_until(
|
||||
lock_type& lock,
|
||||
const chrono::time_point<Clock, Duration>& t);
|
||||
|
||||
template <class lock_type, class Clock, class Duration, class Predicate>
|
||||
bool wait_until(
|
||||
lock_type& lock,
|
||||
const chrono::time_point<Clock, Duration>& t,
|
||||
Predicate pred);
|
||||
|
||||
|
||||
template <class lock_type, class Rep, class Period>
|
||||
cv_status wait_for(
|
||||
lock_type& lock,
|
||||
const chrono::duration<Rep, Period>& d);
|
||||
|
||||
template <class lock_type, class Rep, class Period, class Predicate>
|
||||
bool wait_for(
|
||||
lock_type& lock,
|
||||
const chrono::duration<Rep, Period>& d,
|
||||
Predicate pred);
|
||||
|
||||
#if defined BOOST_THREAD_USES_DATETIME
|
||||
template<typename lock_type>
|
||||
bool timed_wait(lock_type& lock,boost::system_time const& abs_time);
|
||||
template<typename lock_type,typename duration_type>
|
||||
bool timed_wait(lock_type& lock,duration_type const& rel_time);
|
||||
template<typename lock_type,typename predicate_type>
|
||||
bool timed_wait(lock_type& lock,boost::system_time const& abs_time,predicate_type predicate);
|
||||
template<typename lock_type,typename duration_type,typename predicate_type>
|
||||
bool timed_wait(lock_type& lock,duration_type const& rel_time,predicate_type predicate);
|
||||
template<typename lock_type>
|
||||
bool timed_wait(lock_type>& lock,boost::xtime const& abs_time);
|
||||
template<typename lock_type,typename predicate_type>
|
||||
bool timed_wait(lock_type& lock,boost::xtime const& abs_time,predicate_type predicate);
|
||||
#endif
|
||||
};
|
||||
}
|
||||
|
||||
[section:constructor `condition_variable_any()`]
|
||||
|
||||
[variablelist
|
||||
|
||||
[[Effects:] [Constructs an object of class `condition_variable_any`.]]
|
||||
|
||||
[[Throws:] [__thread_resource_error__ if an error occurs.]]
|
||||
|
||||
]
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:destructor `~condition_variable_any()`]
|
||||
|
||||
[variablelist
|
||||
|
||||
[[Precondition:] [All threads waiting on `*this` have been notified by a call to
|
||||
`notify_one` or `notify_all` (though the respective calls to `wait` or
|
||||
`timed_wait` need not have returned).]]
|
||||
|
||||
[[Effects:] [Destroys the object.]]
|
||||
|
||||
[[Throws:] [Nothing.]]
|
||||
|
||||
]
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:notify_one `void notify_one()`]
|
||||
|
||||
[variablelist
|
||||
|
||||
[[Effects:] [If any threads are currently __blocked__ waiting on `*this` in a call
|
||||
to `wait` or `timed_wait`, unblocks one of those threads.]]
|
||||
|
||||
[[Throws:] [Nothing.]]
|
||||
|
||||
]
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:notify_all `void notify_all()`]
|
||||
|
||||
[variablelist
|
||||
|
||||
[[Effects:] [If any threads are currently __blocked__ waiting on `*this` in a call
|
||||
to `wait` or `timed_wait`, unblocks all of those threads.]]
|
||||
|
||||
[[Throws:] [Nothing.]]
|
||||
|
||||
]
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:wait `template<typename lock_type> void wait(lock_type& lock)`]
|
||||
|
||||
[variablelist
|
||||
|
||||
[[Effects:] [Atomically call `lock.unlock()` and blocks the current thread. The
|
||||
thread will unblock when notified by a call to `this->notify_one()` or
|
||||
`this->notify_all()`, or spuriously. When the thread is unblocked (for whatever
|
||||
reason), the lock is reacquired by invoking `lock.lock()` before the call to
|
||||
`wait` returns. The lock is also reacquired by invoking `lock.lock()` if the
|
||||
function exits with an exception.]]
|
||||
|
||||
[[Postcondition:] [`lock` is locked by the current thread.]]
|
||||
|
||||
[[Throws:] [__thread_resource_error__ if an error
|
||||
occurs. __thread_interrupted__ if the wait was interrupted by a call to
|
||||
__interrupt__ on the __thread__ object associated with the current thread of execution.]]
|
||||
|
||||
]
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:wait_predicate `template<typename lock_type,typename predicate_type> void wait(lock_type& lock, predicate_type pred)`]
|
||||
|
||||
[variablelist
|
||||
|
||||
[[Effects:] [As-if ``
|
||||
while(!pred())
|
||||
{
|
||||
wait(lock);
|
||||
}
|
||||
``]]
|
||||
|
||||
]
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:timed_wait `template<typename lock_type> bool timed_wait(lock_type& lock,boost::system_time const& abs_time)`]
|
||||
|
||||
[variablelist
|
||||
|
||||
[[Effects:] [Atomically call `lock.unlock()` and blocks the current thread. The
|
||||
thread will unblock when notified by a call to `this->notify_one()` or
|
||||
`this->notify_all()`, when the time as reported by `boost::get_system_time()`
|
||||
would be equal to or later than the specified `abs_time`, or spuriously. When
|
||||
the thread is unblocked (for whatever reason), the lock is reacquired by
|
||||
invoking `lock.lock()` before the call to `wait` returns. The lock is also
|
||||
reacquired by invoking `lock.lock()` if the function exits with an exception.]]
|
||||
|
||||
[[Returns:] [`false` if the call is returning because the time specified by
|
||||
`abs_time` was reached, `true` otherwise.]]
|
||||
|
||||
[[Postcondition:] [`lock` is locked by the current thread.]]
|
||||
|
||||
[[Throws:] [__thread_resource_error__ if an error
|
||||
occurs. __thread_interrupted__ if the wait was interrupted by a call to
|
||||
__interrupt__ on the __thread__ object associated with the current thread of execution.]]
|
||||
|
||||
]
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:timed_wait_rel `template<typename lock_type,typename duration_type> bool timed_wait(lock_type& lock,duration_type const& rel_time)`]
|
||||
|
||||
[variablelist
|
||||
|
||||
[[Effects:] [Atomically call `lock.unlock()` and blocks the current thread. The
|
||||
thread will unblock when notified by a call to `this->notify_one()` or
|
||||
`this->notify_all()`, after the period of time indicated by the `rel_time`
|
||||
argument has elapsed, or spuriously. When the thread is unblocked (for whatever
|
||||
reason), the lock is reacquired by invoking `lock.lock()` before the call to
|
||||
`wait` returns. The lock is also reacquired by invoking `lock.lock()` if the
|
||||
function exits with an exception.]]
|
||||
|
||||
[[Returns:] [`false` if the call is returning because the time period specified
|
||||
by `rel_time` has elapsed, `true` otherwise.]]
|
||||
|
||||
[[Postcondition:] [`lock` is locked by the current thread.]]
|
||||
|
||||
[[Throws:] [__thread_resource_error__ if an error
|
||||
occurs. __thread_interrupted__ if the wait was interrupted by a call to
|
||||
__interrupt__ on the __thread__ object associated with the current thread of execution.]]
|
||||
|
||||
]
|
||||
|
||||
[note The duration overload of timed_wait is difficult to use correctly. The overload taking a predicate should be preferred in most cases.]
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:timed_wait_predicate `template<typename lock_type,typename predicate_type> bool timed_wait(lock_type& lock, boost::system_time const& abs_time, predicate_type pred)`]
|
||||
|
||||
[variablelist
|
||||
|
||||
[[Effects:] [As-if ``
|
||||
while(!pred())
|
||||
{
|
||||
if(!timed_wait(lock,abs_time))
|
||||
{
|
||||
return pred();
|
||||
}
|
||||
}
|
||||
return true;
|
||||
``]]
|
||||
|
||||
]
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:wait_until `template <class lock_type, class Clock, class Duration> cv_status wait_until(lock_type& lock, const chrono::time_point<Clock, Duration>& abs_time)`]
|
||||
|
||||
[variablelist
|
||||
|
||||
[[Effects:] [Atomically call `lock.unlock()` and blocks the current thread. The
|
||||
thread will unblock when notified by a call to `this->notify_one()` or
|
||||
`this->notify_all()`, when the time as reported by `Clock::now()`
|
||||
would be equal to or later than the specified `abs_time`, or spuriously. When
|
||||
the thread is unblocked (for whatever reason), the lock is reacquired by
|
||||
invoking `lock.lock()` before the call to `wait` returns. The lock is also
|
||||
reacquired by invoking `lock.lock()` if the function exits with an exception.]]
|
||||
|
||||
[[Returns:] [`cv_status::timeout` if the call is returning because the time specified by
|
||||
`abs_time` was reached, `cv_status::no_timeout` otherwise.]]
|
||||
|
||||
[[Postcondition:] [`lock` is locked by the current thread.]]
|
||||
|
||||
[[Throws:] [__thread_resource_error__ if an error
|
||||
occurs. __thread_interrupted__ if the wait was interrupted by a call to
|
||||
__interrupt__ on the __thread__ object associated with the current thread of execution.]]
|
||||
|
||||
]
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:wait_for `template <class lock_type, class Rep, class Period> cv_status wait_for(lock_type& lock, const chrono::duration<Rep, Period>& rel_time)`]
|
||||
|
||||
[variablelist
|
||||
|
||||
[[Effects:] [Atomically call `lock.unlock()` and blocks the current thread. The
|
||||
thread will unblock when notified by a call to `this->notify_one()` or
|
||||
`this->notify_all()`, after the period of time indicated by the `rel_time`
|
||||
argument has elapsed, or spuriously. When the thread is unblocked (for whatever
|
||||
reason), the lock is reacquired by invoking `lock.lock()` before the call to
|
||||
`wait` returns. The lock is also reacquired by invoking `lock.lock()` if the
|
||||
function exits with an exception.]]
|
||||
|
||||
[[Returns:] [`cv_status::timeout` if the call is returning because the time specified by
|
||||
`abs_time` was reached, `cv_status::no_timeout` otherwise.]]
|
||||
|
||||
[[Postcondition:] [`lock` is locked by the current thread.]]
|
||||
|
||||
[[Throws:] [__thread_resource_error__ if an error
|
||||
occurs. __thread_interrupted__ if the wait was interrupted by a call to
|
||||
__interrupt__ on the __thread__ object associated with the current thread of execution.]]
|
||||
|
||||
]
|
||||
|
||||
[note The duration overload of timed_wait is difficult to use correctly. The overload taking a predicate should be preferred in most cases.]
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:wait_until_predicate `template <class lock_type, class Clock, class Duration, class Predicate> bool wait_until(lock_type& lock, const chrono::time_point<Clock, Duration>& abs_time, Predicate pred)`]
|
||||
|
||||
[variablelist
|
||||
|
||||
[[Effects:] [As-if ``
|
||||
while(!pred())
|
||||
{
|
||||
if(!__cvany_wait_until(lock,abs_time))
|
||||
{
|
||||
return pred();
|
||||
}
|
||||
}
|
||||
return true;
|
||||
``]]
|
||||
|
||||
]
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:wait_for_predicate `template <class lock_type, class Rep, class Period, class Predicate> bool wait_for(lock_type& lock, const chrono::duration<Rep, Period>& rel_time, Predicate pred)`]
|
||||
|
||||
[variablelist
|
||||
|
||||
[[Effects:] [As-if ``
|
||||
return wait_until(lock, chrono::steady_clock::now() + d, boost::move(pred));
|
||||
``]]
|
||||
|
||||
]
|
||||
|
||||
[endsect]
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:condition Typedef `condition` DEPRECATED V3]
|
||||
|
||||
// #include <boost/thread/condition.hpp>
|
||||
namespace boost
|
||||
{
|
||||
|
||||
typedef condition_variable_any condition;
|
||||
|
||||
}
|
||||
|
||||
The typedef `condition` is provided for backwards compatibility with previous boost releases.
|
||||
|
||||
[endsect]
|
||||
|
||||
|
||||
[section:notify_all_at_thread_exit Non-member Function `notify_all_at_thread_exit`()]
|
||||
|
||||
// #include <boost/thread/condition_variable.hpp>
|
||||
|
||||
namespace boost
|
||||
{
|
||||
void notify_all_at_thread_exit(condition_variable& cond, unique_lock<mutex> lk);
|
||||
}
|
||||
|
||||
[variablelist
|
||||
|
||||
[[Requires:] [`lk` is locked by the calling thread and either no other thread is waiting on `cond`, or `lk.mutex()` returns the same value for each of the lock arguments supplied by all concurrently waiting (via `wait`, `wait_for`, or `wait_until`) threads.]]
|
||||
[[Effects:] [transfers ownership of the lock associated with `lk` into internal storage and schedules `cond` to be notified when the current thread exits, after all objects of thread storage duration associated with the current thread have been destroyed. This notification shall be as if
|
||||
|
||||
``
|
||||
lk.unlock();
|
||||
cond.notify_all();
|
||||
``
|
||||
|
||||
]]
|
||||
|
||||
]
|
||||
|
||||
[/
|
||||
[[Synchronization:] [The call to notify_all_at_thread_exit and the completion of the destructors for all the current threadÕs variables of thread storage duration synchronize with (1.10) calls to functions waiting on cond.
|
||||
]]
|
||||
[[Note:] [The supplied lock will be held until the thread exits, and care must be taken to ensure that this does not cause deadlock due to lock ordering issues. After calling notify_all_at_thread_exit it is recommended that the thread should be exited as soon as possible, and that no blocking or time-consuming tasks are run on that thread.
|
||||
]]
|
||||
[[Note:] [It is the userÕs responsibility to ensure that waiting threads do not erroneously assume that the thread has finished if they experience spurious wakeups. This typically requires that the condition being waited for is satisfied while holding the lock on lk, and that this lock is not released and reacquired prior to calling notify_all_at_thread_exit.
|
||||
]]
|
||||
]
|
||||
|
||||
[endsect]
|
||||
|
||||
[endsect]
|
||||
@@ -1,97 +0,0 @@
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content=
|
||||
"text/html; charset=iso-8859-1">
|
||||
<meta name="keywords" content="threads, BTL, thread library, C++">
|
||||
|
||||
<title>Boost.Threads, Configuration Information</title>
|
||||
</head>
|
||||
|
||||
<body bgcolor="#FFFFFF" link="#0000FF" vlink="#800080">
|
||||
<table summary="header" border="0" cellpadding="7" cellspacing="0"
|
||||
width="100%">
|
||||
<tr>
|
||||
<td valign="top" width="300">
|
||||
<h3><img src="../../../c++boost.gif" alt="C++ Boost" width=
|
||||
"277" height="86"></h3>
|
||||
</td>
|
||||
|
||||
<td valign="top">
|
||||
<h1 align="center">Boost.Threads</h1>
|
||||
|
||||
<h2 align="center">Configuration Information</h2>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<hr>
|
||||
|
||||
<p><b>Boost.Threads</b> uses several configuration macros in <a href=
|
||||
"../../config/config.htm"><boost/config.hpp></a>. These macros
|
||||
are documented here. Most of the macros are of interest only to
|
||||
developers attempting to provide new implementations of <b>
|
||||
Boost.Threads</b>. The one exception to this is BOOST_HAS_THREADS.</p>
|
||||
|
||||
<table summary="macros" cellspacing="10" width="100%">
|
||||
<tr>
|
||||
<td valign="top"><b>Macro</b> </td>
|
||||
|
||||
<td valign="top"><b>Meaning</b> </td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td valign="top">BOOST_HAS_THREADS</td>
|
||||
|
||||
<td valign="top">Indicates that threading support is available.
|
||||
This means both that there is a platform specific
|
||||
implementation for <b>Boost.Threads</b> and that threading
|
||||
support has been enabled in a platform specific manner. For
|
||||
instance, on the Win32 platform there's an implementation
|
||||
for <b>Boost.Threads</b> but unless the program is compiled
|
||||
against one of the multi-threading runtimes (often determined
|
||||
by the compiler predefining the macro _MT) the
|
||||
BOOST_HAS_THREADS macro remains undefined.</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td valign="top">BOOST_HAS_WINTHREADS</td>
|
||||
|
||||
<td valign="top">Indicates that the platform has the Microsoft
|
||||
Win32 threading libraries, and that they should be used to
|
||||
implement <b>Boost.Threads</b>.</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td valign="top">BOOST_HAS_PTHREADS</td>
|
||||
|
||||
<td valign="top">Indicates that the platform has the POSIX
|
||||
pthreads libraries, and that they should be used to implement
|
||||
<b>Boost.Threads</b>.</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td valign="top">BOOST_HAS_FTIME</td>
|
||||
|
||||
<td valign="top">Indicates that the implementation should use
|
||||
GetSystemTimeAsFileTime() and the FILETIME type to calculate
|
||||
the current time. This is an implementation detail used by
|
||||
boost::detail::getcurtime().</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td valign="top">BOOST_HAS_GETTTIMEOFDAY</td>
|
||||
|
||||
<td valign="top">Indicates that the implementation should use
|
||||
gettimeofday() to calculate the current time. This is an
|
||||
implementation detail used by boost::detail::getcurtime().</td>
|
||||
</tr>
|
||||
</table>
|
||||
<hr>
|
||||
|
||||
<p>Revised
|
||||
<!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan -->05 November, 2001<!--webbot bot="Timestamp" endspan i-checksum="39359" --></p>
|
||||
|
||||
<p><i>© Copyright <a href="mailto:williamkempf@hotmail.com">
|
||||
William E. Kempf</a> 2001 all rights reserved.</i></p>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
399
doc/configuration.qbk
Normal file
399
doc/configuration.qbk
Normal file
@@ -0,0 +1,399 @@
|
||||
[/
|
||||
(C) Copyright 20012 Vicente J. Botet Escriba.
|
||||
Distributed under the Boost Software License, Version 1.0.
|
||||
(See accompanying file LICENSE_1_0.txt or copy at
|
||||
http://www.boost.org/LICENSE_1_0.txt).
|
||||
]
|
||||
|
||||
|
||||
[section:configuration Configuration]
|
||||
|
||||
|
||||
[table Default Values for Configurable Features
|
||||
[[Feature] [Anti-Feature] [V2] [V3] [V4] ]
|
||||
[[USES_CHRONO] [DONT_USE_CHRONO] [YES] [YES] [YES] ]
|
||||
[[PROVIDES_INTERRUPTIONS] [DONT_PROVIDE_INTERRUPTIONS] [YES] [YES] [YES] ]
|
||||
[[THROW_IF_PRECONDITION_NOT_SATISFIED] [-] [NO] [NO] [NO] ]
|
||||
|
||||
|
||||
|
||||
[[PROVIDES_PROMISE_LAZY] [DONT_PROVIDE_PROMISE_LAZY] [YES] [NO] [NO] ]
|
||||
|
||||
[[PROVIDES_BASIC_THREAD_ID] [DONT_PROVIDE_BASIC_THREAD_ID] [NO] [YES] [YES] ]
|
||||
[[PROVIDES_GENERIC_SHARED_MUTEX_ON_WIN] [DONT_PROVIDE_GENERIC_SHARED_MUTEX_ON_WIN] [NO] [YES] [YES] ]
|
||||
|
||||
[[PROVIDES_SHARED_MUTEX_UPWARDS_CONVERSION] [DONT_PROVIDE_SHARED_MUTEX_UPWARDS_CONVERSION] [NO] [YES] [YES] ]
|
||||
[[PROVIDES_EXPLICIT_LOCK_CONVERSION] [DONT_PROVIDE_EXPLICIT_LOCK_CONVERSION] [NO] [YES] [YES] ]
|
||||
[[PROVIDES_FUTURE] [DONT_PROVIDE_FUTURE] [NO] [YES] [YES] ]
|
||||
[[PROVIDES_FUTURE_CTOR_ALLOCATORS] [DONT_PROVIDE_FUTURE_CTOR_ALLOCATORS] [NO] [YES] [YES] ]
|
||||
[[PROVIDES_THREAD_DESTRUCTOR_CALLS_TERMINATE_IF_JOINABLE] [DONT_PROVIDE_THREAD_DESTRUCTOR_CALLS_TERMINATE_IF_JOINABLE] [NO] [YES] [YES] ]
|
||||
[[PROVIDES_THREAD_MOVE_ASSIGN_CALLS_TERMINATE_IF_JOINABLE] [DONT_PROVIDE_THREAD_MOVE_ASSIGN_CALLS_TERMINATE_IF_JOINABLE] [NO] [YES] [YES] ]
|
||||
[[PROVIDES_ONCE_CXX11] [DONT_PROVIDE_ONCE_CXX11] [NO] [YES] [YES] ]
|
||||
[[USES_MOVE] [DONT_USE_MOVE] [NO] [YES] [YES] ]
|
||||
|
||||
[[USES_DATETIME] [DONT_USE_DATETIME] [YES] [YES] [NO] ]
|
||||
[[PROVIDES_THREAD_EQ] [DONT_PROVIDE_THREAD_EQ] [YES] [YES] [NO] ]
|
||||
[[PROVIDES_CONDITION] [DONT_PROVIDE_CONDITION] [YES] [YES] [NO] ]
|
||||
[[PROVIDES_NESTED_LOCKS] [DONT_PROVIDE_NESTED_LOCKS] [YES] [YES] [NO] ]
|
||||
[[PROVIDES_SIGNATURE_PACKAGED_TASK] [DONT_PROVIDE_SIGNATURE_PACKAGED_TASK] [NO] [NO] [YES] ]
|
||||
[[PROVIDES_FUTURE_INVALID_AFTER_GET] [DONT_PROVIDE_FUTURE_INVALID_AFTER_GET] [NO] [NO] [YES] ]
|
||||
[/ [[PROVIDES_FUTURE_CONTINUATION] [DONT_PROVIDE_FUTURE_CONTINUATION] [NO] [NO] [YES] ] ]
|
||||
|
||||
[[PROVIDES_VARIADIC_THREAD] [DONT_PROVIDE_VARIADIC_THREAD] [NO] [NO] [C++11] ]
|
||||
|
||||
]
|
||||
|
||||
[section:chrono Boost.Chrono]
|
||||
|
||||
Boost.Thread uses by default Boost.Chrono for the time related functions and define `BOOST_THREAD_USES_CHRONO` if `BOOST_THREAD_DONT_USE_CHRONO` is not defined. The user should define `BOOST_THREAD_DONT_USE_CHRONO` for compilers that don't work well with Boost.Chrono.
|
||||
|
||||
[endsect]
|
||||
|
||||
|
||||
[section:move Boost.Move]
|
||||
|
||||
Boost.Thread uses by default an internal move semantic implementation. Since version 3.0.0 you can use the move emulation emulation provided by Boost.Move.
|
||||
|
||||
When `BOOST_THREAD_VERSION==2` define `BOOST_THREAD_USES_MOVE ` if you want to use Boost.Move interface.
|
||||
When `BOOST_THREAD_VERSION>=3` define `BOOST_THREAD_DONT_USE_MOVE ` if you don't want to use Boost.Move interface.
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:date_time Boost.DateTime]
|
||||
|
||||
The Boost.DateTime time related functions introduced in Boost 1.35.0, using the [link date_time Boost.Date_Time] library are deprecated. These include (but are not limited to):
|
||||
|
||||
* __sleep__
|
||||
* __timed_join__
|
||||
* __cond_timed_wait__
|
||||
* __timed_lock_ref__
|
||||
|
||||
|
||||
When `BOOST_THREAD_VERSION<=3` define `BOOST_THREAD_DONT_USE_DATETIME ` if you don't want to use Boost.DateTime related interfaces.
|
||||
When `BOOST_THREAD_VERSION>3` define `BOOST_THREAD_USES_DATETIME ` if you want to use Boost.DateTime related interfaces.
|
||||
|
||||
[endsect]
|
||||
|
||||
|
||||
[section:thread_eq `boost::thread::oprator==` deprecated]
|
||||
|
||||
The following nested typedefs are deprecated:
|
||||
|
||||
* `boost::thread::oprator==`
|
||||
* `boost::thread::oprator!=`
|
||||
|
||||
When `BOOST_THREAD_PROVIDES_THREAD_EQ` is defined Boost.Thread provides these deprecated feature.
|
||||
|
||||
Use instead
|
||||
|
||||
* `boost::thread::id::oprator==`
|
||||
* `boost::thread::id::oprator!=`
|
||||
|
||||
[warning This is a breaking change respect to version 1.x.]
|
||||
|
||||
When `BOOST_THREAD_VERSION>=4` define `BOOST_THREAD_PROVIDES_THREAD_EQ ` if you want this feature.
|
||||
When `BOOST_THREAD_VERSION<4` define `BOOST_THREAD_DONT_PROVIDE_THREAD_EQ ` if you don't want this feature.
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:condition boost::condition deprecated]
|
||||
|
||||
`boost::condition` is deprecated. When `BOOST_THREAD_PROVIDES_CONDITION` is defined Boost.Thread provides this deprecated feature.
|
||||
|
||||
Use instead `boost::condition_variable_any`.
|
||||
|
||||
[warning This is a breaking change respect to version 1.x.]
|
||||
|
||||
When `BOOST_THREAD_VERSION>3` define `BOOST_THREAD_PROVIDES_CONDITION` if you want this feature.
|
||||
When `BOOST_THREAD_VERSION<=3` define `BOOST_THREAD_DONT_PROVIDE_CONDITION` if you don't want this feature.
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:nested_lock Mutex nested lock types deprecated]
|
||||
|
||||
|
||||
The following nested typedefs are deprecated:
|
||||
|
||||
* `boost::mutex::scoped_lock`,
|
||||
* `boost::mutex::scoped_try_lock`,
|
||||
* `boost::timed_mutex::scoped_lock`
|
||||
* `boost::timed_mutex::scoped_try_lock`
|
||||
* `boost::timed_mutex::timed_scoped_timed_lock`
|
||||
* `boost::recursive_mutex::scoped_lock`,
|
||||
* `boost::recursive_mutex::scoped_try_lock`,
|
||||
* `boost::recursive_timed_mutex::scoped_lock`
|
||||
* `boost::recursive_timed_mutex::scoped_try_lock`
|
||||
* `boost::recursive_timed_mutex::timed_scoped_timed_lock`
|
||||
|
||||
When `BOOST_THREAD_PROVIDES_NESTED_LOCKS` is defined Boost.Thread provides these deprecated feature.
|
||||
|
||||
|
||||
Use instead
|
||||
* `boost::unique_lock<boost::mutex>`,
|
||||
* `boost::unique_lock<boost::mutex>` with the `try_to_lock_t` constructor,
|
||||
* `boost::unique_lock<boost::timed_mutex>`
|
||||
* `boost::unique_lock<boost::timed_mutex>` with the `try_to_lock_t` constructor
|
||||
* `boost::unique_lock<boost::timed_mutex>`
|
||||
* `boost::unique_lock<boost::recursive_mutex>`,
|
||||
* `boost::unique_lock<boost::recursive_mutex>` with the `try_to_lock_t` constructor,
|
||||
* `boost::unique_lock<boost::recursive_timed_mutex>`
|
||||
* `boost::unique_lock<boost::recursive_timed_mutex>` with the `try_to_lock_t` constructor
|
||||
* `boost::unique_lock<boost::recursive_timed_mutex>`
|
||||
|
||||
[warning This is a breaking change respect to version 1.x.]
|
||||
|
||||
When `BOOST_THREAD_VERSION>=4` define `BOOST_THREAD_PROVIDES_NESTED_LOCKS` if you want these features.
|
||||
When `BOOST_THREAD_VERSION<4` define `BOOST_THREAD_DONT_PROVIDE_NESTED_LOCKS` if you don't want thes features.
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:id thread::id]
|
||||
|
||||
Boost.Thread uses by default a thread::id on Posix based on the pthread type (BOOST_THREAD_PROVIDES_BASIC_THREAD_ID). For backward compatibility and also for compilers that don't work well with this modification the user can define `BOOST_THREAD_DONT_PROVIDE_BASIC_THREAD_ID`.
|
||||
|
||||
Define `BOOST_THREAD_DONT_PROVIDE_BASIC_THREAD_ID ` if you don't want these features.
|
||||
|
||||
[endsect]
|
||||
[section:shared_gen Shared Locking Generic]
|
||||
|
||||
The shared mutex implementation on Windows platform provides currently less functionality than the generic one that is used for PTheads based platforms. In order to have access to these functions, the user needs to define `BOOST_THREAD_PROVIDES_GENERIC_SHARED_MUTEX_ON_WIN` to use the generic implementation, that while could be less efficient, provides all the functions.
|
||||
|
||||
When `BOOST_THREAD_VERSION==2` define `BOOST_THREAD_PROVIDES_GENERIC_SHARED_MUTEX_ON_WIN ` if you want these features.
|
||||
When `BOOST_THREAD_VERSION>=3` define `BOOST_THREAD_DONT_PROVIDE_GENERIC_SHARED_MUTEX_ON_WIN ` if you don't want these features.
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:shared_upwards Shared Locking Upwards Conversion]
|
||||
|
||||
Boost.Threads includes in version 3 the Shared Locking Upwards Conversion as defined in [@http://home.roadrunner.com/~hinnant/bloomington/shared_mutex.html Shared Locking].
|
||||
These conversions need to be used carefully to avoid deadlock or livelock. The user need to define explicitly `BOOST_THREAD_PROVIDES_SHARED_MUTEX_UPWARDS_CONVERSION` to get these upwards conversions.
|
||||
|
||||
When `BOOST_THREAD_VERSION==2` define `BOOST_THREAD_PROVIDES_SHARED_MUTEX_UPWARDS_CONVERSION ` if you want these features.
|
||||
When `BOOST_THREAD_VERSION>=3` define `BOOST_THREAD_DONT_PROVIDE_SHARED_MUTEX_UPWARDS_CONVERSION ` if you don't want these features.
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:explicit_cnv Explicit Lock Conversion]
|
||||
|
||||
In [@http://home.roadrunner.com/~hinnant/bloomington/shared_mutex.html Shared Locking] the lock conversions are explicit. As this explicit conversion breaks the lock interfaces, it is provided only if the `BOOST_THREAD_PROVIDES_EXPLICIT_LOCK_CONVERSION` is defined.
|
||||
|
||||
When `BOOST_THREAD_VERSION==2` define `BOOST_THREAD_PROVIDES_EXPLICIT_LOCK_CONVERSION ` if you want these features.
|
||||
When `BOOST_THREAD_VERSION==3` define `BOOST_THREAD_DONT_PROVIDE_EXPLICIT_LOCK_CONVERSION ` if you don't want these features.
|
||||
|
||||
[endsect]
|
||||
|
||||
|
||||
[section:future unique_future versus future]
|
||||
|
||||
C++11 uses `std::future`. Versions of Boost.Thread previous to version 3.0.0 uses `boost:unique_future`.
|
||||
Since version 3.0.0 `boost::future` replaces `boost::unique_future` when `BOOST_THREAD_PROVIDES_FUTURE` is defined. The documentation doesn't contains anymore however `boost::unique_future`.
|
||||
|
||||
When `BOOST_THREAD_VERSION==2` define `BOOST_THREAD_PROVIDES_FUTURE` if you want to use boost::future.
|
||||
When `BOOST_THREAD_VERSION>=3` define `BOOST_THREAD_DONT_PROVIDE_FUTURE` if you want to use boost::unique_future.
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:lazy promise lazy initialization]
|
||||
|
||||
C++11 promise initialize the associated state at construction time. Versions of Boost.Thread previous to version 3.0.0 initialize it lazily at any point in time in which this associated state is needed.
|
||||
|
||||
Since version 3.0.0 this difference in behavior can be configured. When `BOOST_THREAD_PROVIDES_PROMISE_LAZY` is defined the backward compatible behavior is provided.
|
||||
|
||||
When `BOOST_THREAD_VERSION==2` define `BOOST_THREAD_DONT_PROVIDE_PROMISE_LAZY ` if you want to use boost::future.
|
||||
When `BOOST_THREAD_VERSION>=3` define `BOOST_THREAD_PROVIDES_PROMISE_LAZY ` if you want to use boost::unique_future.
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:alloc promise Allocator constructor]
|
||||
|
||||
C++11 std::promise provides constructors with allocators.
|
||||
|
||||
template <typename R>
|
||||
class promise
|
||||
{
|
||||
public:
|
||||
template <class Allocator>
|
||||
explicit promise(allocator_arg_t, Allocator a);
|
||||
// ...
|
||||
};
|
||||
template <class R, class Alloc> struct uses_allocator<promise<R>,Alloc>: true_type {};
|
||||
|
||||
where
|
||||
|
||||
struct allocator_arg_t { };
|
||||
constexpr allocator_arg_t allocator_arg = allocator_arg_t();
|
||||
|
||||
template <class T, class Alloc> struct uses_allocator;
|
||||
|
||||
Since version 3.0.0 Boost.Thread implements this constructor using the following interface
|
||||
|
||||
namespace boost
|
||||
{
|
||||
typedef container::allocator_arg_t allocator_arg_t;
|
||||
constexpr allocator_arg_t allocator_arg = {};
|
||||
|
||||
namespace container
|
||||
{
|
||||
template <class R, class Alloc>
|
||||
struct uses_allocator<promise<R>,Alloc>: true_type {};
|
||||
}
|
||||
template <class T, class Alloc>
|
||||
struct uses_allocator : public container::uses_allocator<T, Alloc> {};
|
||||
}
|
||||
|
||||
which introduces a dependency on Boost.Container. This feature is provided only if `BOOST_THREAD_PROVIDES_FUTURE_CTOR_ALLOCATORS` is defined.
|
||||
|
||||
When `BOOST_THREAD_VERSION==2` define `BOOST_THREAD_PROVIDES_FUTURE_CTOR_ALLOCATORS ` if you want these features.
|
||||
When `BOOST_THREAD_VERSION>=3` define `BOOST_THREAD_DONT_PROVIDE_FUTURE_CTOR_ALLOCATORS ` if you don't want these features.
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:terminate Call to terminate if joinable]
|
||||
|
||||
C++11 has a different semantic for the thread destructor and the move assignment. Instead of detaching the thread, calls to terminate() if the thread was joinable. When `BOOST_THREAD_PROVIDES_THREAD_DESTRUCTOR_CALLS_TERMINATE_IF_JOINABLE` and `BOOST_THREAD_PROVIDES_THREAD_MOVE_ASSIGN_CALLS_TERMINATE_IF_JOINABLE` is defined Boost.Thread provides the C++ semantic.
|
||||
|
||||
When `BOOST_THREAD_VERSION==2` define `BOOST_THREAD_PROVIDES_THREAD_DESTRUCTOR_CALLS_TERMINATE_IF_JOINABLE ` if you want these features.
|
||||
When `BOOST_THREAD_VERSION>=3` define `BOOST_THREAD_DONT_PROVIDE_THREAD_DESTRUCTOR_CALLS_TERMINATE_IF_JOINABLE ` if you don't want these features.
|
||||
|
||||
When `BOOST_THREAD_VERSION==2` define `BOOST_THREAD_PROVIDES_THREAD_MOVE_ASSIGN_CALLS_TERMINATE_IF_JOINABLE ` if you want these features.
|
||||
When `BOOST_THREAD_VERSION>=3` define `BOOST_THREAD_DONT_PROVIDE_THREAD_MOVE_ASSIGN_CALLS_TERMINATE_IF_JOINABLE ` if you don't want these features.
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:once_flag once_flag]
|
||||
|
||||
C++11 defines a default constructor for once_flag. When `BOOST_THREAD_PROVIDES_ONCE_CXX11 ` is defined Boost.Thread provides this C++ semantics. In this case, the previous aggregate syntax is not supported.
|
||||
|
||||
boost::once_flag once = BOOST_ONCE_INIT;
|
||||
|
||||
You should now just do
|
||||
|
||||
boost::once_flag once;
|
||||
|
||||
When `BOOST_THREAD_VERSION==2` define `BOOST_THREAD_PROVIDES_ONCE_CXX11` if you want these features.
|
||||
When `BOOST_THREAD_VERSION>=3` define `BOOST_THREAD_DONT_PROVIDE_ONCE_CXX11` if you don't want these features.
|
||||
|
||||
[endsect]
|
||||
|
||||
|
||||
[section:deprecated Signature parameter for packaged_task]
|
||||
|
||||
C++11 packaged task class has a Signature template parameter. When `BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK ` is defined Boost.Thread provides this C++ feature.
|
||||
|
||||
[warning This is a breaking change respect to version 3.x.]
|
||||
|
||||
When `BOOST_THREAD_VERSION<4` define `BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK` if you want this feature.
|
||||
When `BOOST_THREAD_VERSION>=4` define `BOOST_THREAD_DONT_PROVIDE_SIGNATURE_PACKAGED_TASK` if you don't want this feature.
|
||||
|
||||
[endsect]
|
||||
|
||||
|
||||
[section:thread_const-var thread constructor with variadic rvalue parameters]
|
||||
|
||||
C++11 thread constructor accep a variable number of rvalue argumentshas. When `BOOST_THREAD_PROVIDES_VARIADIC_THREAD ` is defined Boost.Thread provides this C++ feature if the following are not defined
|
||||
|
||||
* BOOST_NO_CXX11_VARIADIC_TEMPLATES
|
||||
* BOOST_NO_CXX11_DECLTYPE
|
||||
* BOOST_NO_CXX11_RVALUE_REFERENCES
|
||||
* BOOST_NO_CXX11_HDR_TUPLE
|
||||
|
||||
When `BOOST_THREAD_VERSION>4` define `BOOST_THREAD_DONT_PROVIDE_VARIADIC_THREAD ` if you don't want this feature.
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:get_invalid future<>::get() invalidates the future]
|
||||
|
||||
C++11 future<>::get() invalidates the future once its value has been obtained. When `BOOST_THREAD_PROVIDES_FUTURE_INVALID_AFTER_GET ` is defined Boost.Thread provides this C++ feature.
|
||||
|
||||
[warning This is a breaking change respect to version 3.x.]
|
||||
|
||||
When `BOOST_THREAD_VERSION<4` define `BOOST_THREAD_PROVIDES_FUTURE_INVALID_AFTER_GET` if you want this feature.
|
||||
When `BOOST_THREAD_VERSION>=4` define `BOOST_THREAD_DONT_PROVIDE_FUTURE_INVALID_AFTER_GET` if you don't want this feature.
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:intr Interruptions]
|
||||
|
||||
Thread interruption, while useful, makes any interruption point less efficient than if the thread were not interruptible.
|
||||
|
||||
When `BOOST_THREAD_PROVIDES_INTERRUPTIONS` is defined Boost.Thread provides interruptions.
|
||||
When `BOOST_THREAD_DONT_PROVIDE_INTERRUPTIONS` is defined Boost.Thread don't provide interruption.
|
||||
|
||||
Boost.Thread defines BOOST_THREAD_PROVIDES_INTERRUPTIONS if neither BOOST_THREAD_PROVIDES_INTERRUPTIONS nor BOOST_THREAD_DONT_PROVIDE_INTERRUPTIONS are defined, so that there is no compatibility break.
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:version Version]
|
||||
|
||||
`BOOST_THREAD_VERSION` defines the Boost.Thread version.
|
||||
The default version is 2. In this case the following breaking or extending macros are defined if the opposite is not requested:
|
||||
|
||||
* `BOOST_THREAD_PROVIDES_PROMISE_LAZY`
|
||||
|
||||
The user can request the version 3 by defining `BOOST_THREAD_VERSION` to 3. In this case the following breaking or extending macros are defined if the opposite is not requested:
|
||||
|
||||
* Breaking change `BOOST_THREAD_PROVIDES_EXPLICIT_LOCK_CONVERSION `
|
||||
* Conformity & Breaking change `BOOST_THREAD_PROVIDES_FUTURE`
|
||||
* Uniformity `BOOST_THREAD_PROVIDES_GENERIC_SHARED_MUTEX_ON_WIN`
|
||||
* Extension `BOOST_THREAD_PROVIDES_SHARED_MUTEX_UPWARDS_CONVERSION`
|
||||
* Conformity `BOOST_THREAD_PROVIDES_FUTURE_CTOR_ALLOCATORS`
|
||||
* Conformity & Breaking change BOOST_THREAD_PROVIDES_THREAD_DESTRUCTOR_CALLS_TERMINATE_IF_JOINABLE
|
||||
* Conformity & Breaking change BOOST_THREAD_PROVIDES_THREAD_MOVE_ASSIGN_CALLS_TERMINATE_IF_JOINABLE
|
||||
* Conformity & Breaking change `BOOST_THREAD_PROVIDES_ONCE_CXX11`
|
||||
|
||||
* Breaking change `BOOST_THREAD_DONT_PROVIDE_PROMISE_LAZY`
|
||||
|
||||
The default value for `BOOST_THREAD_VERSION` will be changed to 3 since Boost 1.54.
|
||||
|
||||
The user can request the version 4 by defining `BOOST_THREAD_VERSION` to 4. In this case the following breaking or extending macros are defined if the opposite is not requested:
|
||||
|
||||
* Conformity & Breaking change `BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK `
|
||||
* Conformity & Breaking change `BOOST_THREAD_PROVIDES_FUTURE_INVALID_AFTER_GET `
|
||||
* Conformity `BOOST_THREAD_PROVIDES_VARIADIC_THREAD`
|
||||
* Breaking change `BOOST_THREAD_DONT_PROVIDE_THREAD_EQ`
|
||||
* Breaking change `BOOST_THREAD_DONT_USE_DATETIME`
|
||||
|
||||
|
||||
The default value for `BOOST_THREAD_VERSION` will be changed to 4 since Boost 1.56.
|
||||
|
||||
[endsect]
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:limitations Limitations]
|
||||
|
||||
Some compilers don't work correctly with some of the added features.
|
||||
|
||||
[section:sun SunPro]
|
||||
|
||||
If __SUNPRO_CC < 0x5100 the library defines
|
||||
|
||||
* `BOOST_THREAD_DONT_USE_MOVE`
|
||||
|
||||
If __SUNPRO_CC < 0x5100 the library defines
|
||||
|
||||
* `BOOST_THREAD_DONT_PROVIDE_FUTURE_CTOR_ALLOCATORS`
|
||||
|
||||
|
||||
[endsect]
|
||||
[section:vacpp VACPP]
|
||||
|
||||
If __IBMCPP__ < 1100 the library defines
|
||||
|
||||
* `BOOST_THREAD_DONT_USE_CHRONO`
|
||||
* `BOOST_THREAD_USES_DATE`
|
||||
|
||||
And Boost.Thread doesn't links with Boost.Chrono.
|
||||
|
||||
|
||||
[endsect]
|
||||
[section:ce WCE]
|
||||
|
||||
If _WIN32_WCE && _WIN32_WCE==0x501 the library defines
|
||||
|
||||
* `BOOST_THREAD_DONT_PROVIDE_FUTURE_CTOR_ALLOCATORS`
|
||||
|
||||
|
||||
[endsect]
|
||||
|
||||
[endsect]
|
||||
@@ -1,348 +0,0 @@
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Language" content="en-us">
|
||||
<meta http-equiv="Content-Type" content=
|
||||
"text/html; charset=windows-1252">
|
||||
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
|
||||
<meta name="ProgId" content="FrontPage.Editor.Document">
|
||||
|
||||
<title>Boost.Threads Definitions</title>
|
||||
</head>
|
||||
|
||||
<body bgcolor="#FFFFFF">
|
||||
<table summary="header" border="0" cellpadding="7" cellspacing="0"
|
||||
width="100%">
|
||||
<tr>
|
||||
<td valign="top" width="300">
|
||||
<h3><img src="../../../c++boost.gif" alt="C++ Boost" width=
|
||||
"277" height="86"></h3>
|
||||
</td>
|
||||
|
||||
<td valign="top">
|
||||
<h1 align="center">Boost.Threads</h1>
|
||||
|
||||
<h2 align="center">Definitions</h2>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<h2>Introduction</h2>
|
||||
|
||||
<p>The definitions are given in terms of the <a href=
|
||||
"bibliography.html#ISO-98">C++ Standard</a>. References to the standard
|
||||
are in the form [1.2.3/4], which represents the section number, with
|
||||
the paragraph number following the "/".</p>
|
||||
|
||||
<p>Because the definitions are written in something akin to
|
||||
"standardese", they can be difficult to understand. The
|
||||
intent isn't to confuse, but rather to clarify the additional
|
||||
requirements Boost.Threads places on a C++ implementation as defined by
|
||||
the C++ Standard.</p>
|
||||
|
||||
<h2>Definitions</h2>
|
||||
|
||||
<h3>Thread</h3>
|
||||
|
||||
<p>Thread is short for "thread of execution". A thread of
|
||||
execution is an execution environment [1.9/7] within the execution
|
||||
environment of a C++ program [1.9]. The main() function [3.6.1] of the
|
||||
program is the initial function of the initial thread. A program in a
|
||||
multi-threading environment always has an initial thread even if the
|
||||
program explicitly creates no additional threads.</p>
|
||||
|
||||
<p>Unless otherwise specified, each thread shares all aspects of its
|
||||
execution environment with other threads in the program. Shared aspects
|
||||
of the execution environment include, but are not limited to, the
|
||||
following:</p>
|
||||
|
||||
<ul>
|
||||
<li>Static storage duration (static, extern) objects [3.7.1].</li>
|
||||
</ul>
|
||||
|
||||
<ul>
|
||||
<li>Dynamic storage duration (heap) objects [3.7.3]. Thus each
|
||||
memory allocation will return a unique addresses, regardless of the
|
||||
thread making the allocation request.</li>
|
||||
</ul>
|
||||
|
||||
<ul>
|
||||
<li>Automatic storage duration (stack) objects [3.7.2] accessed via
|
||||
pointer or reference from another thread.</li>
|
||||
</ul>
|
||||
|
||||
<ul>
|
||||
<li>Resources provided by the operating system. For example,
|
||||
files.</li>
|
||||
</ul>
|
||||
|
||||
<ul>
|
||||
<li>The program itself. In other words, each thread is executing
|
||||
some function of the same program, not a totally different
|
||||
program.</li>
|
||||
</ul>
|
||||
|
||||
<p>Each thread has its own:</p>
|
||||
|
||||
<ul>
|
||||
<li>Registers and current execution sequence (program counter)
|
||||
[1.9/5].</li>
|
||||
</ul>
|
||||
|
||||
<ul>
|
||||
<li>Automatic storage duration (stack) objects [3.7.2].</li>
|
||||
</ul>
|
||||
|
||||
<h3><a name="Thread-safe">Thread-safe</a></h3>
|
||||
|
||||
<p>A program is thread-safe if it has no <a href="#Race condition">race
|
||||
conditions</a>, does not <a href="#Deadlock">deadlock</a>, and has no
|
||||
<a href="#Priority failure">priority failures</a>.</p>
|
||||
|
||||
<p>Note that thread-safety does not necessarily imply efficiency, and
|
||||
than while some thread-safety violations can be determined statically
|
||||
at compile time, many thread-safety errors can only only be detected at
|
||||
runtime.</p>
|
||||
|
||||
<h3>Thread <a name="State">State</a></h3>
|
||||
|
||||
<p>During the lifetime of a thread, it shall be in one of the following
|
||||
states:</p>
|
||||
|
||||
<table summary="thread states" border="1" cellpadding="5">
|
||||
<tr>
|
||||
<td><b>State</b></td>
|
||||
|
||||
<td><b>Description</b></td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td>Ready</td>
|
||||
|
||||
<td>Ready to run, but waiting for a processor.</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td>Running</td>
|
||||
|
||||
<td>Currently executing on a processor. Zero or more threads
|
||||
may be running at any time, with a maximum equal to the number
|
||||
of processors.</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td>Blocked</td>
|
||||
|
||||
<td>Waiting for some resource other than a processor which is
|
||||
not currently available, or for the completion of calls to
|
||||
library functions [1.9/6]. The term "waiting" is
|
||||
synonymous for "blocked"</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td>Terminated</td>
|
||||
|
||||
<td>Finished execution but not yet detached or joined.</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<p>Thread state transitions shall occur only as specified:</p>
|
||||
|
||||
<table summary="state transitions" border="1" cellpadding="5">
|
||||
<tr>
|
||||
<td><b>From</b></td>
|
||||
|
||||
<td><b>To</b></td>
|
||||
|
||||
<td><b>Cause</b></td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td>
|
||||
<p align="left">[none]</p>
|
||||
</td>
|
||||
|
||||
<td>Ready</td>
|
||||
|
||||
<td>Thread is created by a call to a library function. In the
|
||||
case of the initial thread, creation is implicit and occurs
|
||||
during the startup of the main() function [3.6.1].</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td>Ready</td>
|
||||
|
||||
<td>Running</td>
|
||||
|
||||
<td>Processor becomes available.</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td>Running</td>
|
||||
|
||||
<td>Ready</td>
|
||||
|
||||
<td>Thread preempted.</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td>Running</td>
|
||||
|
||||
<td>Blocked</td>
|
||||
|
||||
<td>Thread calls a library function which waits for a resource
|
||||
or for the completion of I/O.</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td>Running</td>
|
||||
|
||||
<td>Terminated</td>
|
||||
|
||||
<td>Thread returns from its initial function, calls a thread
|
||||
termination library function, or is cancelled by some other
|
||||
thread calling a thread termination library function.</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td>Blocked</td>
|
||||
|
||||
<td>Ready</td>
|
||||
|
||||
<td>The resource being waited for becomes available, or the
|
||||
blocking library function completes.</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td>Terminated</td>
|
||||
|
||||
<td>[none]</td>
|
||||
|
||||
<td>Thread is detached or joined by some other thread calling
|
||||
the appropriate library function, or by program termination
|
||||
[3.6.3].</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<p>[Note: if a suspend() function is added to the threading library,
|
||||
additional transitions to the blocked state will have to be added to
|
||||
the above table.]</p>
|
||||
|
||||
<h3><a name="Race condition">Race condition</a></h3>
|
||||
|
||||
<p>A race condition is what occurs when multiple threads read and write
|
||||
to the same memory without proper synchronization, resulting in an
|
||||
incorrect value being read or written. The result of a race condition
|
||||
may be a bit pattern which isn't even a valid value for the data
|
||||
type. A race condition results in undefined behavior [1.3.12].</p>
|
||||
|
||||
<p>Race conditions can be prevented by serializing memory access using
|
||||
the tools provided by Boost.Threads.</p>
|
||||
|
||||
<h3><a name="Deadlock">Deadlock</a></h3>
|
||||
|
||||
<p>Deadlock is an execution state where for some set of threads, each
|
||||
thread in the set is blocked waiting for some action by one of the
|
||||
other threads in the set. Since each is waiting on the others, none
|
||||
will ever become ready again.</p>
|
||||
|
||||
<h3><a name="Priority failure">Priority failure</a></h3>
|
||||
|
||||
<p>A priority failure (such as priority inversion or infinite
|
||||
overtaking) occurs when threads executed in such a sequence that
|
||||
required work is not performed in time to be useful.</p>
|
||||
|
||||
<h2>Memory visibility between threads</h2>
|
||||
|
||||
<p>An address [1.7] shall always point to the same memory byte,
|
||||
regardless of the thread or processor dereferencing the address.</p>
|
||||
|
||||
<p>An object [1.8, 1.9] is accessible from multiple threads if it is of
|
||||
static storage duration (static, extern) [3.7.1], or if a pointer or
|
||||
reference to it is explicitly or implicitly dereferenced in multiple
|
||||
threads.</p>
|
||||
|
||||
<p>For an object accessible from multiple threads, the value of the
|
||||
object accessed from one thread may be indeterminate or different than
|
||||
the value accessed from another thread, except under the conditions
|
||||
specified in the following table. For the same row of the table, the
|
||||
value of an object accessible at the indicated sequence point in thread
|
||||
A will be determinate and the same if accessed at or after the
|
||||
indicated sequence point in thread B, provided the object is not
|
||||
otherwise modified. In the table, the "sequence point at a
|
||||
call" is the sequence point after the evaluation of all function
|
||||
arguments [1.9/17], while the "sequence point after a call"
|
||||
is the sequence point after the copying of the returned value..."
|
||||
[1.9/17].</p>
|
||||
|
||||
<table summary="memory visibility" border="1" cellpadding="5">
|
||||
<tr>
|
||||
<td align="center"><b>Thread A</b></td>
|
||||
|
||||
<td align="center"><b>Thread B</b></td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td>The sequence point at a call to a library thread-creation
|
||||
function.</td>
|
||||
|
||||
<td>The first sequence point of the initial function in the new
|
||||
thread created by the Thread A call.</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td>The sequence point at a call to a library function which
|
||||
locks a mutex, directly or by waiting for a condition
|
||||
variable.</td>
|
||||
|
||||
<td>The sequence point after a call to a library function which
|
||||
unlocks the same mutex.</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td>The last sequence point before thread termination.</td>
|
||||
|
||||
<td>The sequence point after a call to a library function which
|
||||
joins the terminated thread.</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td>The sequence point at a call to a library function which
|
||||
signals or broadcasts a condition variable.</td>
|
||||
|
||||
<td>The sequence point after the call to the library function
|
||||
which was waiting on that same condition variable or
|
||||
signal.</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<p>The architecture of the execution environment and the observable
|
||||
behavior of the abstract machine [1.9] shall be the same on all
|
||||
processors.</p>
|
||||
|
||||
<p>The latitude granted by the C++ standard for an implementation to
|
||||
alter the definition of observable behavior of the abstract machine to
|
||||
include additional library I/O functions [1.9/6] is extended to include
|
||||
threading library functions.</p>
|
||||
|
||||
<p>When an exception is thrown and there is no matching exception
|
||||
handler in the same thread, behavior is undefined. The preferred
|
||||
behavior is the same as when there is no matching exception handler in
|
||||
a program [15.3/9]. That is, terminate() is called, and it is
|
||||
implementation defined whether or not the stack is unwound.</p>
|
||||
|
||||
<h2><a name="Acknowledgements">Acknowledgements</a></h2>
|
||||
|
||||
<p>This document has been much improved by the incorporation of
|
||||
comments from William Kempf.</p>
|
||||
|
||||
<p>The visibility rules are based on <a href=
|
||||
"bibliography.html#Butenhof-97">[Butenhof 97]</a>.</p>
|
||||
<hr>
|
||||
|
||||
<p>Revised
|
||||
<!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %b %Y" startspan -->05 Nov 2001<!--webbot bot="Timestamp" endspan i-checksum="15246" --></p>
|
||||
|
||||
<p>© Copyright Beman Dawes, 2001</p>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
365
doc/emulations.qbk
Normal file
365
doc/emulations.qbk
Normal file
@@ -0,0 +1,365 @@
|
||||
[/
|
||||
(C) Copyright 20012 Vicente J. Botet Escriba.
|
||||
Distributed under the Boost Software License, Version 1.0.
|
||||
(See accompanying file LICENSE_1_0.txt or copy at
|
||||
http://www.boost.org/LICENSE_1_0.txt).
|
||||
]
|
||||
|
||||
|
||||
[section:emulations Emulations]
|
||||
[section:delete `=delete` emulation]
|
||||
|
||||
C++11 allows to delete some implicitly generated functions as constructors and assignment using '= delete' as in
|
||||
|
||||
public:
|
||||
thread(thread const&) = delete;
|
||||
|
||||
On compilers not supporting this feature, Boost.Thread relays on a partial simulation, it declares the function as private without definition.
|
||||
|
||||
private:
|
||||
thread(thread &);
|
||||
|
||||
The emulation is partial as the private function can be used for overload resolution for some compilers and prefer it to other overloads that need a conversion. See below the consequences on the move semantic emulation.
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:move Move semantics]
|
||||
|
||||
In order to implement Movable classes, move parameters and return types Boost.Thread uses the rvalue reference when the compiler support it.
|
||||
On compilers not supporting it Boost.Thread uses either the emulation provided by Boost.Move or the emulation provided by the previous versions of Boost.Thread depending whether `BOOST_THREAD_USES_MOVE` is defined or not. This macros is unset by default when `BOOST_THREAD_VERSION` is 2. Since `BOOST_THREAD_VERSION` 3, `BOOST_THREAD_USES_MOVE` is defined.
|
||||
|
||||
[section:deprecated Deprecated Version 2 interface]
|
||||
|
||||
Previous to version 1.50, Boost.Thread make use of its own move semantic emulation which had more limitations than the provided by Boost.Move. In addition, it is of interest of the whole Boost community that Boost.Thread uses Boost.Move so that boost::thread can be stored on Movable aware containers.
|
||||
|
||||
To preserve backward compatibility at least during some releases, Boost.Thread allows the user to use the deprecated move semantic emulation defining BOOST_THREAD_DONT_USE_MOVE.
|
||||
|
||||
Many aspects of move semantics can be emulated for compilers not supporting rvalue references and Boost.Thread legacy offers tools for that purpose.
|
||||
|
||||
[section:Helper Helpers class and function]
|
||||
|
||||
Next follows the interface of the legacy move semantic helper class and function.
|
||||
|
||||
namespace boost
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
template<typename T>
|
||||
struct thread_move_t
|
||||
{
|
||||
explicit thread_move_t(T& t_);
|
||||
T& operator*() const;
|
||||
T* operator->() const;
|
||||
private:
|
||||
void operator=(thread_move_t&);
|
||||
};
|
||||
}
|
||||
template<typename T>
|
||||
boost::detail::thread_move_t<T> move(boost::detail::thread_move_t<T> t);
|
||||
}
|
||||
[endsect]
|
||||
|
||||
[section:movable Movable emulation]
|
||||
|
||||
We can write a MovableOny class as follows. You just need to follow these simple steps:
|
||||
|
||||
* Add a conversion to the `detail::thread_move_t<classname>`
|
||||
* Make the copy constructor private.
|
||||
* Write a constructor taking the parameter as `detail::thread_move_t<classname>`
|
||||
* Write an assignment taking the parameter as `detail::thread_move_t<classname>`
|
||||
|
||||
For example the thread class defines the following:
|
||||
|
||||
class thread
|
||||
{
|
||||
// ...
|
||||
private:
|
||||
thread(thread&);
|
||||
thread& operator=(thread&);
|
||||
public:
|
||||
detail::thread_move_t<thread> move()
|
||||
{
|
||||
detail::thread_move_t<thread> x(*this);
|
||||
return x;
|
||||
}
|
||||
operator detail::thread_move_t<thread>()
|
||||
{
|
||||
return move();
|
||||
}
|
||||
thread(detail::thread_move_t<thread> x)
|
||||
{
|
||||
thread_info=x->thread_info;
|
||||
x->thread_info.reset();
|
||||
}
|
||||
thread& operator=(detail::thread_move_t<thread> x)
|
||||
{
|
||||
thread new_thread(x);
|
||||
swap(new_thread);
|
||||
return *this;
|
||||
}
|
||||
// ...
|
||||
|
||||
};
|
||||
|
||||
[endsect]
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:portable Portable interface]
|
||||
|
||||
In order to make the library code portable Boost.Thread uses some macros that will use either the ones provided by Boost.Move or the deprecated move semantics provided by previous versions of Boost.Thread.
|
||||
|
||||
See the Boost.Move documentation for a complete description on how to declare new Movable classes and its limitations.
|
||||
|
||||
* `BOOST_THREAD_RV_REF(TYPE)` is the equivalent of `BOOST_RV_REF(TYPE)`
|
||||
* `BOOST_THREAD_RV_REF_BEG` is the equivalent of `BOOST_RV_REF_BEG(TYPE)`
|
||||
* `BOOST_THREAD_RV_REF_END` is the equivalent of `BOOST_RV_REF_END(TYPE)`
|
||||
* `BOOST_THREAD_FWD_REF(TYPE)` is the equivalent of `BOOST_FWD_REF(TYPE)
|
||||
|
||||
In addition the following macros are needed to make the code portable:
|
||||
|
||||
* `BOOST_THREAD_RV(V)` macro to access the rvalue from a BOOST_THREAD_RV_REF(TYPE),
|
||||
* `BOOST_THREAD_MAKE_RV_REF(RVALUE)` makes a rvalue.
|
||||
* `BOOST_THREAD_DCL_MOVABLE(CLASS)` to avoid conflicts with Boost.Move
|
||||
* `BOOST_THREAD_DCL_MOVABLE_BEG(T1)` and `BOOST_THREAD_DCL_MOVABLE_END` are variant of `BOOST_THREAD_DCL_MOVABLE` when the parameter is a template instantiation.
|
||||
|
||||
Other macros are provided and must be included on the public section:
|
||||
|
||||
* `BOOST_THREAD_NO_COPYABLE` declares a class no-copyable either deleting the copy constructors and copy assignment or moving them to the private section.
|
||||
* `BOOST_THREAD_MOVABLE(CLASS)` declares all the implicit conversions to an rvalue-reference.
|
||||
* `BOOST_THREAD_MOVABLE_ONLY(CLASS)` is the equivalent of `BOOST_MOVABLE_BUT_NOT_COPYABLE(CLASS)`
|
||||
* `BOOST_THREAD_COPYABLE_AND_MOVABLE(CLASS)` is the equivalent of `BOOST_COPYABLE_AND_MOVABLE(CLASS)`
|
||||
|
||||
|
||||
[section:NO_COPYABLE `BOOST_THREAD_NO_COPYABLE(CLASS)`]
|
||||
|
||||
This macro marks a class as no copyable, disabling copy construction and assignment.
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:MOVABLE `BOOST_THREAD_MOVABLE(CLASS)`]
|
||||
|
||||
This macro marks a class as movable, declaring all the implicit conversions to an rvalue-reference.
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:MOVABLE_ONLY `BOOST_THREAD_MOVABLE_ONLY(CLASS)`]
|
||||
|
||||
This macro marks a type as movable but not copyable, disabling copy construction and assignment. The user will need to write a move constructor/assignment to fully write a movable but not copyable class.
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:COPYABLE_AND_MOVABLE `BOOST_THREAD_COPYABLE_AND_MOVABLE(CLASS)`]
|
||||
|
||||
This macro marks a type as copyable and movable. The user will need to write a move constructor/assignment and a copy assignment to fully write a copyable and movable class.
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:RV_REF `BOOST_THREAD_RV_REF(TYPE)`, `BOOST_THREAD_RV_REF_BEG` and `BOOST_THREAD_RV_REF_END`]
|
||||
|
||||
This macro is used to achieve portable syntax in move constructors and assignments for classes marked as `BOOST_THREAD_COPYABLE_AND_MOVABLE` or `BOOST_THREAD_MOVABLE_ONLY`.
|
||||
|
||||
`BOOST_THREAD_RV_REF_BEG` and `BOOST_THREAD_RV_REF_END` are used when the parameter end with a `>` to avoid the compiler error.
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:RV `BOOST_THREAD_RV(V)`]
|
||||
|
||||
While Boost.Move emulation allows to access an rvalue reference `BOOST_THREAD_RV_REF(TYPE)` using the dot operator, the legacy defines the `operator->`. We need then a macro `BOOST_THREAD_RV` that mask this difference. E.g.
|
||||
|
||||
thread(BOOST_THREAD_RV_REF(thread) x)
|
||||
{
|
||||
thread_info=BOOST_THREAD_RV(x).thread_info;
|
||||
BOOST_THREAD_RV(x).thread_info.reset();
|
||||
}
|
||||
|
||||
The use of this macros has reduced considerably the size of the Boost.Thread move related code.
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:MAKE_RV_REF `BOOST_THREAD_MAKE_RV_REF(RVALUE)`]
|
||||
|
||||
While Boost.Move is the best C++03 move emulation there are some limitations that impact the way the library can be used.
|
||||
For example, with the following declarations
|
||||
|
||||
class thread {
|
||||
// ...
|
||||
private:
|
||||
thread(thread &);
|
||||
public:
|
||||
thread(rv<thread>&);
|
||||
// ...
|
||||
};
|
||||
|
||||
This could not work on some compilers even if thread is convertible to `rv<thread>` because the compiler prefers the private copy constructor.
|
||||
|
||||
thread mkth()
|
||||
{
|
||||
return thread(f);
|
||||
}
|
||||
|
||||
On these compilers we need to use instead an explicit conversion. The library provides a move member function that allows to workaround the issue.
|
||||
|
||||
thread mkth()
|
||||
{
|
||||
return thread(f).move();
|
||||
}
|
||||
|
||||
Note that `::boost::move` can not be used in this case as thread is not implicitly convertible to `thread&`.
|
||||
|
||||
thread mkth()
|
||||
{
|
||||
return ::boost::move(thread(f));
|
||||
}
|
||||
|
||||
To make the code portable Boost.Thread the user needs to use a macro `BOOST_THREAD_MAKE_RV_REF` that can be used as in
|
||||
|
||||
thread mkth()
|
||||
{
|
||||
return BOOST_THREAD_MAKE_RV_REF(thread(f));
|
||||
}
|
||||
|
||||
Note that this limitation is shared also by the legacy Boost.Thread move emulation.
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:DCL_MOVABLE `BOOST_THREAD_DCL_MOVABLE`, `BOOST_THREAD_DCL_MOVABLE_BEG(T1)` and `BOOST_THREAD_DCL_MOVABLE_END`]
|
||||
|
||||
As Boost.Move defines also the `boost::move` function we need to specialize the `has_move_emulation_enabled_aux` metafunction.
|
||||
|
||||
template <>
|
||||
struct has_move_emulation_enabled_aux<thread>
|
||||
: BOOST_MOVE_BOOST_NS::integral_constant<bool, true>
|
||||
{};
|
||||
|
||||
so that the following Boost.Move overload is disabled
|
||||
|
||||
template <class T>
|
||||
inline typename BOOST_MOVE_BOOST_NS::disable_if<has_move_emulation_enabled_aux<T>, T&>::type move(T& x);
|
||||
|
||||
The macros `BOOST_THREAD_DCL_MOVABLE(CLASS)`, `BOOST_THREAD_DCL_MOVABLE_BEG(T1)` and `BOOST_THREAD_DCL_MOVABLE_END` are used for this purpose. E.g.
|
||||
|
||||
BOOST_THREAD_DCL_MOVABLE(thread)
|
||||
|
||||
and
|
||||
|
||||
BOOST_THREAD_DCL_MOVABLE_BEG(T) promise<T> BOOST_THREAD_DCL_MOVABLE_END
|
||||
|
||||
|
||||
[endsect]
|
||||
[endsect]
|
||||
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:bool_explicit_conversion Bool explicit conversion]
|
||||
|
||||
Locks provide an explicit bool conversion operator when the compiler provides them.
|
||||
|
||||
explicit operator bool() const;
|
||||
|
||||
The library provides un implicit conversion to an undefined type that can be used as a conditional expression.
|
||||
|
||||
#if defined(BOOST_NO_EXPLICIT_CONVERSION_OPERATORS)
|
||||
operator ``['unspecified-bool-type]``() const;
|
||||
bool operator!() const;
|
||||
#else
|
||||
explicit operator bool() const;
|
||||
#endif
|
||||
|
||||
The user should use the lock.owns_lock() when a explicit conversion is required.
|
||||
|
||||
[section:bool_conversion `operator `['unspecified-bool-type]`() const`]
|
||||
|
||||
[variablelist
|
||||
|
||||
[[Returns:] [If __owns_lock_ref__ would return `true`, a value that evaluates to
|
||||
`true` in boolean contexts, otherwise a value that evaluates to `false` in
|
||||
boolean contexts.]]
|
||||
|
||||
[[Throws:] [Nothing.]]
|
||||
|
||||
]
|
||||
|
||||
[endsect]
|
||||
|
||||
|
||||
[section:operator_not `bool operator!() const`]
|
||||
|
||||
[variablelist
|
||||
|
||||
[[Returns:] [`!` __owns_lock_ref__.]]
|
||||
|
||||
[[Throws:] [Nothing.]]
|
||||
|
||||
]
|
||||
|
||||
[endsect]
|
||||
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:scoped_enums Scoped Enums]
|
||||
|
||||
Some of the enumerations defined in the standard library are scoped enums.
|
||||
|
||||
On compilers that don't support them, the library uses a class to wrap the underlying type. Instead of
|
||||
|
||||
enum class future_errc
|
||||
{
|
||||
broken_promise,
|
||||
future_already_retrieved,
|
||||
promise_already_satisfied,
|
||||
no_state
|
||||
};
|
||||
|
||||
the library declare these types as
|
||||
|
||||
BOOST_SCOPED_ENUM_DECLARE_BEGIN(future_errc)
|
||||
{
|
||||
broken_promise,
|
||||
future_already_retrieved,
|
||||
promise_already_satisfied,
|
||||
no_state
|
||||
}
|
||||
BOOST_SCOPED_ENUM_DECLARE_END(future_errc)
|
||||
|
||||
These macros allows to use 'future_errc' in almost all the cases as an scoped enum.
|
||||
|
||||
There are however some limitations:
|
||||
|
||||
* The type is not a C++ enum, so 'is_enum<future_errc>' will be false_type.
|
||||
* The emulated scoped enum can not be used in switch nor in template arguments. For these cases the user needs to use some macros.
|
||||
|
||||
Instead of
|
||||
|
||||
switch (ev)
|
||||
{
|
||||
case future_errc::broken_promise:
|
||||
// ...
|
||||
|
||||
use
|
||||
|
||||
switch (boost::native_value(ev))
|
||||
{
|
||||
case future_errc::broken_promise:
|
||||
|
||||
And instead of
|
||||
|
||||
#ifdef BOOST_NO_SCOPED_ENUMS
|
||||
template <>
|
||||
struct BOOST_SYMBOL_VISIBLE is_error_code_enum<future_errc> : public true_type { };
|
||||
#endif
|
||||
|
||||
use
|
||||
|
||||
#ifdef BOOST_NO_SCOPED_ENUMS
|
||||
template <>
|
||||
struct BOOST_SYMBOL_VISIBLE is_error_code_enum<future_errc::enum_type> : public true_type { };
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
[endsect]
|
||||
|
||||
[endsect]
|
||||
583
doc/external_locking.qbk
Normal file
583
doc/external_locking.qbk
Normal file
@@ -0,0 +1,583 @@
|
||||
[/
|
||||
/ Copyright (c) 2008,2012 Vicente J. Botet Escriba
|
||||
/
|
||||
/ Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
/ file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
/]
|
||||
|
||||
[section External Locking -- `strict_lock` and `externally_locked` classes]
|
||||
|
||||
|
||||
[note This tutorial is an adaptation of the paper of Andrei Alexandrescu "Multithreading and the C++ Type System"
|
||||
to the Boost library.]
|
||||
|
||||
[/
|
||||
[section Internal locking]
|
||||
|
||||
Consider, for example, modeling a bank account class that supports simultaneous deposits and withdrawals from multiple locations (arguably the "Hello, World" of multi-threaded programming). In the code below, guard's constructor locks the passed-in object this, and guard's destructor unlocks this.
|
||||
|
||||
class BankAccount {
|
||||
boost::mutex mtx_; // explicit mutex declaration
|
||||
int balance_;
|
||||
public:
|
||||
void Deposit(int amount) {
|
||||
boost::lock_guard<boost::mutex> guard(mtx_);
|
||||
balance_ += amount;
|
||||
}
|
||||
void Withdraw(int amount) {
|
||||
boost::lock_guard<boost::mutex> guard(mtx_);
|
||||
balance_ -= amount;
|
||||
}
|
||||
int GetBalance() {
|
||||
boost::lock_guard<boost::mutex> guard(mtx_);
|
||||
return balance_;
|
||||
}
|
||||
};
|
||||
|
||||
The object-level locking idiom doesn't cover the entire richness of a threading model. For example, the model above is quite deadlock-prone when you try to coordinate multi-object transactions. Nonetheless, object-level locking is useful in many cases, and in combination with other mechanisms can provide a satisfactory solution to many threaded access problems in object-oriented programs.
|
||||
|
||||
[endsect]
|
||||
|
||||
[section Internal and external locking]
|
||||
|
||||
The BankAccount class above uses internal locking. Basically, a class that uses internal locking guarantees that any concurrent calls to its public member functions don't corrupt an instance of that class. This is typically ensured by having each public member function acquire a lock on the object upon entry. This way, for any given object of that class, there can be only one member function call active at any moment, so the operations are nicely serialized.
|
||||
|
||||
This approach is reasonably easy to implement and has an attractive simplicity. Unfortunately, "simple" might sometimes morph into "simplistic."
|
||||
|
||||
Internal locking is insufficient for many real-world synchronization tasks. Imagine that you want to implement an ATM withdrawal transaction with the BankAccount class. The requirements are simple. The ATM transaction consists of two withdrawals-one for the actual money and one for the $2 commission. The two withdrawals must appear in strict sequence; that is, no other transaction can exist between them.
|
||||
|
||||
The obvious implementation is erratic:
|
||||
|
||||
void ATMWithdrawal(BankAccount& acct, int sum) {
|
||||
acct.Withdraw(sum);
|
||||
// preemption possible
|
||||
acct.Withdraw(2);
|
||||
}
|
||||
|
||||
The problem is that between the two calls above, another thread can perform another operation on the account, thus breaking the second design requirement.
|
||||
|
||||
In an attempt to solve this problem, let's lock the account from the outside during the two operations:
|
||||
|
||||
void ATMWithdrawal(BankAccount& acct, int sum) {
|
||||
boost::lock_guard<boost::mutex> guard(acct.mtx_); // mtx_ field is private
|
||||
acct.Withdraw(sum);
|
||||
acct.Withdraw(2);
|
||||
}
|
||||
|
||||
|
||||
Notice that the code above doesn't compiles, the `mtx_` field is private.
|
||||
We have two possibilities:
|
||||
|
||||
* make `mtx_` public which seams odd
|
||||
* make the `BankAccount` lockable by adding the lock/unlock functions
|
||||
|
||||
We can add these functions explicitly
|
||||
|
||||
class BankAccount {
|
||||
boost::mutex mtx_;
|
||||
int balance_;
|
||||
public:
|
||||
void Deposit(int amount) {
|
||||
boost::lock_guard<boost::mutex> guard(mtx_);
|
||||
balance_ += amount;
|
||||
}
|
||||
void Withdraw(int amount) {
|
||||
boost::lock_guard<boost::mutex> guard(mtx_);
|
||||
balance_ -= amount;
|
||||
}
|
||||
void lock() {
|
||||
mtx_.lock();
|
||||
}
|
||||
void unlock() {
|
||||
mtx_.unlock();
|
||||
}
|
||||
};
|
||||
|
||||
or inheriting from a class which add these lockable functions.
|
||||
|
||||
The `basic_lockable_adapter` class helps to define the `BankAccount` class as
|
||||
|
||||
class BankAccount
|
||||
: public basic_lockable_adapter<thread_mutex>
|
||||
{
|
||||
int balance_;
|
||||
public:
|
||||
void Deposit(int amount) {
|
||||
boost::lock_guard<BankAccount> guard(*this);
|
||||
// boost::lock_guard<boost::mutex> guard(*this->mutex());
|
||||
balance_ += amount;
|
||||
}
|
||||
void Withdraw(int amount) {
|
||||
boost::lock_guard<BankAccount> guard(*this);
|
||||
// boost::lock_guard<boost::mutex> guard(*this->mutex());
|
||||
balance_ -= amount;
|
||||
}
|
||||
int GetBalance() {
|
||||
boost::lock_guard<BankAccount> guard(*this);
|
||||
// boost::lock_guard<boost::mutex> guard(*this->mutex());
|
||||
return balance_;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
and the code that does not compiles becomes
|
||||
|
||||
void ATMWithdrawal(BankAccount& acct, int sum) {
|
||||
// boost::lock_guard<boost::mutex> guard(*acct.mutex());
|
||||
boost::lock_guard<BankAccount> guard(acct);
|
||||
acct.Withdraw(sum);
|
||||
acct.Withdraw(2);
|
||||
}
|
||||
|
||||
Notice that now acct is being locked by Withdraw after it has already been locked by guard. When running such code, one of two things happens.
|
||||
|
||||
* Your mutex implementation might support the so-called recursive mutex semantics. This means that the same thread can lock the same mutex several times successfully. In this case, the implementation works but has a performance overhead due to unnecessary locking. (The locking/unlocking sequence in the two Withdraw calls is not needed but performed anyway-and that costs time.)
|
||||
* Your mutex implementation might not support recursive locking, which means that as soon as you try to acquire it the second time, it blocks-so the ATMWithdrawal function enters the dreaded deadlock.
|
||||
|
||||
As `boost::mutex` is not recursive, we need to use its recursive version `boost::recursive_mutex`.
|
||||
|
||||
class BankAccount
|
||||
: public basic_lockable_adapter<recursive_mutex>
|
||||
{
|
||||
|
||||
// ...
|
||||
};
|
||||
|
||||
The caller-ensured locking approach is more flexible and the most efficient, but very dangerous. In an implementation using caller-ensured locking, BankAccount still holds a mutex, but its member functions don't manipulate it at all. Deposit and Withdraw are not thread-safe anymore. Instead, the client code is responsible for locking BankAccount properly.
|
||||
|
||||
class BankAccount
|
||||
: public basic_lockable_adapter<boost:mutex> {
|
||||
int balance_;
|
||||
public:
|
||||
void Deposit(int amount) {
|
||||
balance_ += amount;
|
||||
}
|
||||
void Withdraw(int amount) {
|
||||
balance_ -= amount;
|
||||
}
|
||||
};
|
||||
|
||||
Obviously, the caller-ensured locking approach has a safety problem. BankAccount's implementation code is finite, and easy to reach and maintain, but there's an unbounded amount of client code that manipulates BankAccount objects. In designing applications, it's important to differentiate between requirements imposed on bounded code and unbounded code. If your class makes undue requirements on unbounded code, that's usually a sign that encapsulation is out the window.
|
||||
|
||||
To conclude, if in designing a multi-threaded class you settle on internal locking, you expose yourself to inefficiency or deadlocks. On the other hand, if you rely on caller-provided locking, you make your class error-prone and difficult to use. Finally, external locking completely avoids the issue by leaving it all to the client code.
|
||||
[endsect]
|
||||
]
|
||||
[section Locks as Permits]
|
||||
|
||||
So what to do? Ideally, the BankAccount class should do the following:
|
||||
|
||||
* Support both locking models (internal and external).
|
||||
* Be efficient; that is, use no unnecessary locking.
|
||||
* Be safe; that is, BankAccount objects cannot be manipulated without appropriate locking.
|
||||
|
||||
Let's make a worthwhile observation: Whenever you lock a BankAccount, you do so by using a `lock_guard<BankAccount>` object. Turning this statement around, wherever there's a `lock_guard<BankAccount>`, there's also a locked `BankAccount` somewhere. Thus, you can think of-and use-a `lock_guard<BankAccount>` object as a permit. Owning a `lock_guard<BankAccount>` gives you rights to do certain things. The `lock_guard<BankAccount>` object should not be copied or aliased (it's not a transmissible permit).
|
||||
|
||||
# As long as a permit is still alive, the `BankAccount` object stays locked.
|
||||
# When the `lock_guard<BankAccount>` is destroyed, the `BankAccount`'s mutex is released.
|
||||
|
||||
The net effect is that at any point in your code, having access to a `lock_guard<BankAccount>` object guarantees that a `BankAccount` is locked. (You don't know exactly which `BankAccount` is locked, however-an issue that we'll address soon.)
|
||||
|
||||
For now, let's make a couple of enhancements to the `lock_guard` class template defined in Boost.Thread.
|
||||
We'll call the enhanced version `strict_lock`. Essentially, a `strict_lock`'s role is only to live on the stack as an automatic variable.
|
||||
`strict_lock` must adhere to a non-copy and non-alias policy.
|
||||
`strict_lock` disables copying by making the copy constructor and the assignment operator private.
|
||||
While we're at it, let's disable operator new and operator delete;
|
||||
`strict_lock` are not intended to be allocated on the heap.
|
||||
`strict_lock` avoids aliasing by using a slightly less orthodox and less well-known technique: disable address taking.
|
||||
|
||||
|
||||
template <typename Lockable>
|
||||
class strict_lock {
|
||||
public:
|
||||
typedef Lockable lockable_type;
|
||||
|
||||
|
||||
explicit strict_lock(lockable_type& obj) : obj_(obj) {
|
||||
obj.lock(); // locks on construction
|
||||
}
|
||||
strict_lock() = delete;
|
||||
strict_lock(strict_lock const&) = delete;
|
||||
strict_lock& operator=(strict_lock const&) = delete;
|
||||
|
||||
~strict_lock() { obj_.unlock(); } // unlocks on destruction
|
||||
|
||||
bool owns_lock(mutex_type const* l) const noexcept // strict lockers specific function
|
||||
{
|
||||
return l == &obj_;
|
||||
}
|
||||
private:
|
||||
lockable_type& obj_;
|
||||
};
|
||||
|
||||
Silence can be sometimes louder than words-what's forbidden to do with a `strict_lock` is as important as what you can do. Let's see what you can and what you cannot do with a `strict_lock` instantiation:
|
||||
|
||||
* You can create a `strict_lock<T>` only starting from a valid T object. Notice that there is no other way you can create a `strict_lock<T>`.
|
||||
|
||||
BankAccount myAccount("John Doe", "123-45-6789");
|
||||
strict_locerk<BankAccount> myLock(myAccount); // ok
|
||||
|
||||
* You cannot copy `strict_lock`s to one another. In particular, you cannot pass `strict_lock`s by value to functions or have them returned by functions:
|
||||
|
||||
extern strict_lock<BankAccount> Foo(); // compile-time error
|
||||
extern void Bar(strict_lock<BankAccount>); // compile-time error
|
||||
|
||||
* However, you still can pass `strict_lock`s by reference to and from functions:
|
||||
|
||||
// ok, Foo returns a reference to strict_lock<BankAccount>
|
||||
extern strict_lock<BankAccount>& Foo();
|
||||
// ok, Bar takes a reference to strict_lock<BankAccount>
|
||||
extern void Bar(strict_lock<BankAccount>&);
|
||||
|
||||
* You cannot allocate a `strict_lock` on the heap. However, you still can put `strict_lock`s on the heap if they're members of a class.
|
||||
|
||||
strict_lock<BankAccount>* pL =
|
||||
new strict_lock<BankAccount>(myAcount); //error!
|
||||
// operator new is not accessible
|
||||
class Wrapper {
|
||||
strict_lock memberLock_;
|
||||
...
|
||||
};
|
||||
Wrapper* pW = new Wrapper; // ok
|
||||
|
||||
(Making `strict_lock` a member variable of a class is not recommended. Fortunately, disabling copying and default construction makes `strict_lock` quite an unfriendly member variable.)
|
||||
|
||||
* You cannot take the address of a `strict_lock` object. This interesting feature, implemented by disabling unary operator&, makes it very unlikely to alias a `strict_lock` object. Aliasing is still possible by taking references to a `strict_lock`:
|
||||
|
||||
strict_lock<BankAccount> myLock(myAccount); // ok
|
||||
strict_lock<BankAccount>* pAlias = &myLock; // error!
|
||||
// strict_lock<BankAccount>::operator& is not accessible
|
||||
strict_lock<BankAccount>& rAlias = myLock; // ok
|
||||
|
||||
Fortunately, references don't engender as bad aliasing as pointers because they're much less versatile (references cannot be copied or reseated).
|
||||
|
||||
* You can even make `strict_lock` final; that is, impossible to derive from. This task is left in the form of an exercise to the reader.
|
||||
|
||||
All these rules were put in place with one purpose-enforcing that owning a `strict_lock<T>` is a reasonably strong guarantee that
|
||||
|
||||
# you locked a T object, and
|
||||
# that object will be unlocked at a later point.
|
||||
|
||||
Now that we have such a strict `strict_lock`, how do we harness its power in defining a safe, flexible interface for BankAccount? The idea is as follows:
|
||||
|
||||
* Each of BankAccount's interface functions (in our case, Deposit and Withdraw) comes in two overloaded variants.
|
||||
* One version keeps the same signature as before, and the other takes an additional argument of type `strict_lock<BankAccount>`. The first version is internally locked; the second one requires external locking. External locking is enforced at compile time by requiring client code to create a `strict_lock<BankAccount>` object.
|
||||
* BankAccount avoids code bloating by having the internal locked functions forward to the external locked functions, which do the actual job.
|
||||
|
||||
A little code is worth 1,000 words, a (hacked into) saying goes, so here's the new BankAccount class:
|
||||
|
||||
class BankAccount
|
||||
: public basic_lockable_adapter<boost:recursive_mutex>
|
||||
{
|
||||
int balance_;
|
||||
public:
|
||||
void Deposit(int amount, strict_lock<BankAccount>&) {
|
||||
// Externally locked
|
||||
balance_ += amount;
|
||||
}
|
||||
void Deposit(int amount) {
|
||||
strict_lock<boost:mutex> guard(*this); // Internally locked
|
||||
Deposit(amount, guard);
|
||||
}
|
||||
void Withdraw(int amount, strict_lock<BankAccount>&) {
|
||||
// Externally locked
|
||||
balance_ -= amount;
|
||||
}
|
||||
void Withdraw(int amount) {
|
||||
strict_lock<boost:mutex> guard(*this); // Internally locked
|
||||
Withdraw(amount, guard);
|
||||
}
|
||||
};
|
||||
|
||||
Now, if you want the benefit of internal locking, you simply call Deposit(int) and Withdraw(int). If you want to use external locking, you lock the object by constructing a `strict_lock<BankAccount>` and then you call `Deposit(int, strict_lock<BankAccount>&)` and `Withdraw(int, strict_lock<BankAccount>&)`. For example, here's the `ATMWithdrawal` function implemented correctly:
|
||||
|
||||
void ATMWithdrawal(BankAccount& acct, int sum) {
|
||||
strict_lock<BankAccount> guard(acct);
|
||||
acct.Withdraw(sum, guard);
|
||||
acct.Withdraw(2, guard);
|
||||
}
|
||||
|
||||
This function has the best of both worlds-it's reasonably safe and efficient at the same time.
|
||||
|
||||
It's worth noting that `strict_lock` being a template gives extra safety compared to a straight polymorphic approach. In such a design, BankAccount would derive from a Lockable interface. `strict_lock` would manipulate Lockable references so there's no need for templates. This approach is sound; however, it provides fewer compile-time guarantees. Having a `strict_lock` object would only tell that some object derived from Lockable is currently locked. In the templated approach, having a `strict_lock<BankAccount>` gives a stronger guarantee-it's a `BankAccount` that stays locked.
|
||||
|
||||
There's a weasel word in there-I mentioned that ATMWithdrawal is reasonably safe. It's not really safe because there's no enforcement that the `strict_lock<BankAccount>` object locks the appropriate BankAccount object. The type system only ensures that some BankAccount object is locked. For example, consider the following phony implementation of ATMWithdrawal:
|
||||
|
||||
void ATMWithdrawal(BankAccount& acct, int sum) {
|
||||
BankAccount fakeAcct("John Doe", "123-45-6789");
|
||||
strict_lock<BankAccount> guard(fakeAcct);
|
||||
acct.Withdraw(sum, guard);
|
||||
acct.Withdraw(2, guard);
|
||||
}
|
||||
|
||||
This code compiles warning-free but obviously doesn't do the right thing-it locks one account and uses another.
|
||||
|
||||
It's important to understand what can be enforced within the realm of the C++ type system and what needs to be enforced at runtime. The mechanism we've put in place so far ensures that some BankAccount object is locked during the call to `BankAccount::Withdraw(int, strict_lock<BankAccount>&)`. We must enforce at runtime exactly what object is locked.
|
||||
|
||||
If our scheme still needs runtime checks, how is it useful? An unwary or malicious programmer can easily lock the wrong object and manipulate any BankAccount without actually locking it.
|
||||
|
||||
First, let's get the malice issue out of the way. C is a language that requires a lot of attention and discipline from the programmer. C++ made some progress by asking a little less of those, while still fundamentally trusting the programmer. These languages are not concerned with malice (as Java is, for example). After all, you can break any C/C++ design simply by using casts "appropriately" (if appropriately is an, er, appropriate word in this context).
|
||||
|
||||
The scheme is useful because the likelihood of a programmer forgetting about any locking whatsoever is much greater than the likelihood of a programmer who does remember about locking, but locks the wrong object.
|
||||
|
||||
Using `strict_lock` permits compile-time checking of the most common source of errors, and runtime checking of the less frequent problem.
|
||||
|
||||
Let's see how to enforce that the appropriate BankAccount object is locked. First, we need to add a member function to the `strict_lock` class template.
|
||||
The `bool strict_lock<T>::owns_lock(Loclable*)` function returns a reference to the locked object.
|
||||
|
||||
template <class Lockable> class strict_lock {
|
||||
... as before ...
|
||||
public:
|
||||
bool owns_lock(Lockable* mtx) const { return mtx==&obj_; }
|
||||
};
|
||||
|
||||
Second, BankAccount needs to use this function compare the locked object against this:
|
||||
|
||||
class BankAccount {
|
||||
: public basic_lockable_adapter<boost::recursive_mutex>
|
||||
int balance_;
|
||||
public:
|
||||
void Deposit(int amount, strict_lock<BankAccount>& guard) {
|
||||
// Externally locked
|
||||
if (!guard.owns_lock(*this))
|
||||
throw "Locking Error: Wrong Object Locked";
|
||||
balance_ += amount;
|
||||
}
|
||||
// ...
|
||||
};
|
||||
|
||||
The overhead incurred by the test above is much lower than locking a recursive mutex for the second time.
|
||||
|
||||
[endsect]
|
||||
|
||||
[section Improving External Locking]
|
||||
|
||||
Now let's assume that BankAccount doesn't use its own locking at all, and has only a thread-neutral implementation:
|
||||
|
||||
class BankAccount {
|
||||
int balance_;
|
||||
public:
|
||||
void Deposit(int amount) {
|
||||
balance_ += amount;
|
||||
}
|
||||
void Withdraw(int amount) {
|
||||
balance_ -= amount;
|
||||
}
|
||||
};
|
||||
|
||||
Now you can use BankAccount in single-threaded and multi-threaded applications alike, but you need to provide your own synchronization in the latter case.
|
||||
|
||||
Say we have an AccountManager class that holds and manipulates a BankAccount object:
|
||||
|
||||
class AccountManager
|
||||
: public basic_lockable_adapter<boost::mutex>
|
||||
{
|
||||
BankAccount checkingAcct_;
|
||||
BankAccount savingsAcct_;
|
||||
...
|
||||
};
|
||||
|
||||
Let's also assume that, by design, AccountManager must stay locked while accessing its BankAccount members. The question is, how can we express this design constraint using the C++ type system? How can we state "You have access to this BankAccount object only after locking its parent AccountManager object"?
|
||||
|
||||
The solution is to use a little bridge template `externally_locked` that controls access to a BankAccount.
|
||||
|
||||
template <typename T, typename Lockable>
|
||||
class externally_locked {
|
||||
BOOST_CONCEPT_ASSERT((LockableConcept<Lockable>));
|
||||
|
||||
public:
|
||||
externally_locked(T& obj, Lockable& lockable)
|
||||
: obj_(obj)
|
||||
, lockable_(lockable)
|
||||
{}
|
||||
|
||||
externally_locked(Lockable& lockable)
|
||||
: obj_()
|
||||
, lockable_(lockable)
|
||||
{}
|
||||
|
||||
T& get(strict_lock<Lockable>& lock) {
|
||||
|
||||
#ifndef BOOST_THREAD_EXTERNALLY_LOCKED_DONT_CHECK_SAME // define BOOST_THREAD_EXTERNALLY_LOCKED_DONT_CHECK_SAME if you don't want to check locker check the same lockable
|
||||
if (!lock.is_locking(&lockable_)) throw lock_error(); run time check throw if not locks the same
|
||||
#endif
|
||||
return obj_;
|
||||
}
|
||||
void set(const T& obj, Lockable& lockable) {
|
||||
obj_ = obj;
|
||||
lockable_=lockable;
|
||||
}
|
||||
private:
|
||||
T obj_;
|
||||
Lockable& lockable_;
|
||||
};
|
||||
|
||||
`externally_locked` cloaks an object of type T, and actually provides full access to that object through the get and set member functions, provided you pass a reference to a `strict_lock<Owner>` object.
|
||||
|
||||
Instead of making `checkingAcct_` and `savingsAcct_` of type `BankAccount`, `AccountManager` holds objects of type `externally_locked<BankAccount, AccountManager>`:
|
||||
|
||||
class AccountManager
|
||||
: public basic_lockable_adapter<thread_mutex>
|
||||
{
|
||||
public:
|
||||
typedef basic_lockable_adapter<thread_mutex> lockable_base_type;
|
||||
AccountManager()
|
||||
: checkingAcct_(*this)
|
||||
, savingsAcct_(*this)
|
||||
{}
|
||||
inline void Checking2Savings(int amount);
|
||||
inline void AMoreComplicatedChecking2Savings(int amount);
|
||||
private:
|
||||
|
||||
externally_locked<BankAccount, AccountManager> checkingAcct_;
|
||||
externally_locked<BankAccount, AccountManager> savingsAcct_;
|
||||
};
|
||||
|
||||
The pattern is the same as before - to access the BankAccount object cloaked by `checkingAcct_`, you need to call `get`. To call `get`, you need to pass it a `strict_lock<AccountManager>`. The one thing you have to take care of is to not hold pointers or references you obtained by calling `get`. If you do that, make sure that you don't use them after the strict_lock has been destroyed. That is, if you alias the cloaked objects, you're back from "the compiler takes care of that" mode to "you must pay attention" mode.
|
||||
|
||||
Typically, you use `externally_locked` as shown below. Suppose you want to execute an atomic transfer from your checking account to your savings account:
|
||||
|
||||
void AccountManager::Checking2Savings(int amount) {
|
||||
strict_lock<AccountManager> guard(*this);
|
||||
checkingAcct_.get(guard).Withdraw(amount);
|
||||
savingsAcct_.get(guard).Deposit(amount);
|
||||
}
|
||||
|
||||
We achieved two important goals. First, the declaration of `checkingAcct_` and `savingsAcct_` makes it clear to the code reader that that variable is protected by a lock on an AccountManager. Second, the design makes it impossible to manipulate the two accounts without actually locking a BankAccount. `externally_locked` is what could be called active documentation.
|
||||
|
||||
[endsect]
|
||||
|
||||
[section Allowing other strict locks]
|
||||
|
||||
Now imagine that the AccountManager function needs to take a `unique_lock` in order to reduce the critical regions. And at some time it needs to access to the `checkingAcct_`. As `unique_lock` is not a strict lock the following code doesn't compiles:
|
||||
|
||||
void AccountManager::AMoreComplicatedChecking2Savings(int amount) {
|
||||
unique_lock<AccountManager> guard(*this, defer_lock);
|
||||
if (some_condition()) {
|
||||
guard.lock();
|
||||
}
|
||||
checkingAcct_.get(guard).Withdraw(amount); // COMPILE ERROR
|
||||
savingsAcct_.get(guard).Deposit(amount); // COMPILE ERROR
|
||||
do_something_else();
|
||||
}
|
||||
|
||||
We need a way to transfer the ownership from the `unique_lock` to a `strict_lock` the time we are working with `savingsAcct_` and then restore the ownership on `unique_lock`.
|
||||
|
||||
void AccountManager::AMoreComplicatedChecking2Savings(int amount) {
|
||||
unique_lock<AccountManager> guard(*this, defer_lock);
|
||||
if (some_condition()) {
|
||||
guard1.lock();
|
||||
}
|
||||
{
|
||||
strict_lock<AccountManager> guard(guard1);
|
||||
checkingAcct_.get(guard).Withdraw(amount);
|
||||
savingsAcct_.get(guard).Deposit(amount);
|
||||
}
|
||||
guard1.unlock();
|
||||
}
|
||||
|
||||
In order to make this code compilable we need to store either a Lockable or a `unique_lock<Lockable>` reference depending on the constructor. Store which kind of reference we have stored,and in the destructor call either to the Lockable `unlock` or restore the ownership.
|
||||
|
||||
This seams too complicated to me. Another possibility is to define a nested strict lock class. The drawback is that instead of having only one strict lock we have two and we need either to duplicate every function taking a `strict\_lock` or make these function templates functions. The problem with template functions is that we don't profit anymore of the C++ type system. We must add some static metafunction that check that the Locker parameter is a strict lock. The problem is that we can not really check this or can we?. The `is_strict_lock` metafunction must be specialized by the strict lock developer. We need to belive it "sur parolle". The advantage is that now we can manage with more than two strict locks without changing our code. Ths is really nice.
|
||||
|
||||
Now we need to state that both classes are `strict_lock`s.
|
||||
|
||||
template <typename Locker>
|
||||
struct is_strict_lock : mpl::false_ {};
|
||||
|
||||
template <typename Lockable>
|
||||
struct is_strict_lock<strict_lock<Lockable> > : mpl::true_ {}
|
||||
|
||||
template <typename Locker>
|
||||
struct is_strict_lock<nested_strict_lock<Locker> > : mpl::true_ {}
|
||||
|
||||
|
||||
Well let me show how this `nested_strict_lock` class looks like and the impacts on the `externally_locked` class and the `AccountManager::AMoreComplicatedFunction` function.
|
||||
|
||||
First `nested_strict_lock` class will store on a temporary lock the `Locker`, and transfer the lock ownership on the constructor. On destruction he will restore the ownership. Note also that the Locker needs to have already a reference to the mutex otherwise an exception is thrown and the use of the `lock_traits`.
|
||||
|
||||
template <typename Locker >
|
||||
class nested_strict_lock
|
||||
{
|
||||
BOOST_CONCEPT_ASSERT((MovableLockerConcept<Locker>));
|
||||
public:
|
||||
typedef typename lockable_type<Locker>::type lockable_type;
|
||||
typedef typename syntactic_lock_traits<lockable_type>::lock_error lock_error;
|
||||
|
||||
nested_strict_lock(Locker& lock)
|
||||
: lock_(lock) // Store reference to locker
|
||||
, tmp_lock_(lock.move()) // Move ownership to temporaty locker
|
||||
{
|
||||
#ifndef BOOST_THREAD_STRCIT_LOCKER_DONT_CHECK_OWNERSHIP // Define BOOST_THREAD_EXTERNALLY_LOCKED_DONT_CHECK_OWNERSHIP if you don't want to check locker ownership
|
||||
if (tmp_lock_.mutex()==0) {
|
||||
lock_=tmp_lock_.move(); // Rollback for coherency purposes
|
||||
throw lock_error();
|
||||
}
|
||||
#endif
|
||||
if (!tmp_lock_) tmp_lock_.lock(); // ensures it is locked
|
||||
}
|
||||
~nested_strict_lock() {
|
||||
lock_=tmp_lock_.move(); // Move ownership to nesting locker
|
||||
}
|
||||
typedef bool (nested_strict_lock::*bool_type)() const;
|
||||
operator bool_type() const { return &nested_strict_lock::owns_lock; }
|
||||
bool operator!() const { return false; }
|
||||
bool owns_lock() const { return true; }
|
||||
const lockable_type* mutex() const { return tmp_lock_.mutex(); }
|
||||
bool is_locking(lockable_type* l) const { return l==mutex(); }
|
||||
|
||||
BOOST_ADRESS_OF_DELETE(nested_strict_lock)
|
||||
BOOST_HEAP_ALLOCATEION_DELETE(nested_strict_lock)
|
||||
BOOST_DEFAULT_CONSTRUCTOR_DELETE(nested_strict_lock) 8
|
||||
BOOST_COPY_CONSTRUCTOR_DELETE(nested_strict_lock) 9
|
||||
BOOST_COPY_ASSIGNEMENT_DELETE(nested_strict_lock) 10
|
||||
|
||||
private:
|
||||
Locker& lock_;
|
||||
Locker tmp_lock_;
|
||||
};
|
||||
|
||||
The `externally_locked` get function is now a template function taking a Locker as parameters instead of a `strict_lock`.
|
||||
We can add test in debug mode that ensure that the Lockable object is locked.
|
||||
|
||||
template <typename T, typename Lockable>
|
||||
class externally_locked {
|
||||
public:
|
||||
// ...
|
||||
template <class Locker>
|
||||
T& get(Locker& lock) {
|
||||
BOOST_CONCEPT_ASSERT((StrictLockerConcept<Locker>));
|
||||
|
||||
BOOST_STATIC_ASSERT((is_strict_lock<Locker>::value)); // locker is a strict locker "sur parolle"
|
||||
BOOST_STATIC_ASSERT((is_same<Lockable,
|
||||
typename lockable_type<Locker>::type>::value)); // that locks the same type
|
||||
#ifndef BOOST_THREAD_EXTERNALLY_LOCKED_DONT_CHECK_OWNERSHIP // define BOOST_THREAD_EXTERNALLY_LOCKED_NO_CHECK_OWNERSHIP if you don't want to check locker ownership
|
||||
if (! lock ) throw lock_error(); // run time check throw if no locked
|
||||
#endif
|
||||
#ifndef BOOST_THREAD_EXTERNALLY_LOCKED_DONT_CHECK_SAME
|
||||
if (!lock.is_locking(&lockable_)) throw lock_error();
|
||||
#endif
|
||||
return obj_;
|
||||
}
|
||||
};
|
||||
|
||||
The `AccountManager::AMoreComplicatedFunction` function needs only to replace the `strict_lock` by a `nested_strict_lock`.
|
||||
|
||||
void AccountManager::AMoreComplicatedChecking2Savings(int amount) {
|
||||
unique_lock<AccountManager> guard1(*this);
|
||||
if (some_condition()) {
|
||||
guard1.lock();
|
||||
}
|
||||
{
|
||||
nested_strict_lock<unique_lock<AccountManager> > guard(guard1);
|
||||
checkingAcct_.get(guard).Withdraw(amount);
|
||||
savingsAcct_.get(guard).Deposit(amount);
|
||||
}
|
||||
guard1.unlock();
|
||||
}
|
||||
|
||||
[endsect]
|
||||
|
||||
[endsect]
|
||||
|
||||
|
||||
193
doc/faq.html
193
doc/faq.html
@@ -1,193 +0,0 @@
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content=
|
||||
"text/html; charset=iso-8859-1">
|
||||
<meta name="keywords" content="threads, BTL, thread library, C++">
|
||||
<link rel="stylesheet" type="text/css" href="styles.css">
|
||||
|
||||
<title>Boost.Threads, FAQ</title>
|
||||
</head>
|
||||
|
||||
<body bgcolor="#FFFFFF" link="#0000FF" vlink="#800080">
|
||||
<table summary="header" border="0" cellpadding="7" cellspacing="0"
|
||||
width="100%">
|
||||
<tr>
|
||||
<td valign="top" width="300">
|
||||
<h3><img src="../../../c++boost.gif" alt="C++ Boost" width="277" height="86"></h3>
|
||||
</td>
|
||||
|
||||
<td valign="top">
|
||||
<h1 align="center">Boost.Threads</h1>
|
||||
|
||||
<h2 align="center">Frequently Asked Questions</h2>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<hr>
|
||||
|
||||
<h2>1. Are lock objects <a href="definitions.html#Thread-safe">
|
||||
thread-safe</a>?</h2>
|
||||
|
||||
<p><b>No!</b> Lock objects are not meant to be shared between threads.
|
||||
They are meant to be short lived objects created on automatic storage
|
||||
within a code block. Any other usage is just likely to lead to errors
|
||||
and won't really be of actual benefit any way. Share <a href=
|
||||
"mutex_concept.html">mutexes</a>, not locks. For more information see
|
||||
the <a href="rationale.html#lock_objects">rationale</a> behind the
|
||||
design for lock objects.</p>
|
||||
|
||||
<h2>2a. Why was Boost.Threads modeled after (specific library
|
||||
name)?</h2>
|
||||
|
||||
<p>It wasn't. Boost.Threads was designed from scratch. Extensive
|
||||
design discussions involved numerous people representing a wide range
|
||||
of experience across many platforms. To ensure portability, the initial
|
||||
implements were done in parallel using POSIX Threads and theWin32
|
||||
threading API. But the Boost.Threads design is very much in the spirit
|
||||
of C++, and thus doesn't model such C based APIs.</p>
|
||||
|
||||
<h2>2b. Why wasn't Boost.Threads modeled after (specific library
|
||||
name)?</h2>
|
||||
|
||||
<p>Existing C++ libraries either seemed dangerous (often failing to
|
||||
take advantage of prior art to reduce errors) or had excessive
|
||||
dependencies on library components unrelated to threading. Existing C
|
||||
libraries couldn't meet our C++ requirements, and were also missing
|
||||
certain features. For instance, the WIN32 thread API lacks condition
|
||||
variables, even though these are critical for the important Monitor
|
||||
pattern <a href="bibliography.html#Schmidt-00">[Schmidt 00]</a>.</p>
|
||||
|
||||
<h2>3. Why do <a href="mutex_concept.html">Mutexes</a> have noncopyable
|
||||
semantics?</h2>
|
||||
|
||||
<p>To ensure that <a href="definitions.html#Deadlock">deadlocks</a>
|
||||
don't occur. The only logical form of copy would be to use some
|
||||
sort of shallow copy semantics in which multiple mutex objects could
|
||||
refer to the same mutex state. This means that if ObjA has a mutex
|
||||
object as part of its state and ObjB is copy constructed from it, then
|
||||
when ObjB::foo() locks the mutex it has effectively locked ObjA as
|
||||
well. This behavior can result in deadlock. Other copy semantics result
|
||||
in similar problems (if you think you can prove this to be wrong then
|
||||
supply us with an alternative and we'll reconsider).</p>
|
||||
|
||||
<h2>4. How can you prevent <a href="definitions.html#Deadlock">
|
||||
deadlock</a> from occurring when a thread must lock multiple
|
||||
mutexes?</h2>
|
||||
|
||||
<p>Always lock them in the same order. One easy way of doing this is to
|
||||
use each mutex's address to determine the order in which they are
|
||||
locked. A future Boost.Threads concept may wrap this pattern up in a
|
||||
reusable class.</p>
|
||||
|
||||
<h2>5. Don't noncopyable <a href="mutex_concept.html">mutex</a>
|
||||
semantics mean that a class with a mutex member will be noncopyable as
|
||||
well?</h2>
|
||||
|
||||
<p>No, but what it does mean is that the compiler can't generate a
|
||||
copy constructor and assignment operator, so they will have to be coded
|
||||
explicitly. This is a <b>good thing</b>, however, since the compiler
|
||||
generated operations would not be <a href=
|
||||
"definitions.html#Thread-safe">thread-safe</a>. The following is a
|
||||
simple example of a class with copyable semantics and internal
|
||||
synchronization through a mutex member.</p>
|
||||
<pre>
|
||||
class counter
|
||||
{
|
||||
public:
|
||||
// Doesn't need synchronization since there can be no references to *this
|
||||
// until after it's constructed!
|
||||
explicit counter(int initial_value)
|
||||
: m_value(initial_value)
|
||||
{
|
||||
}
|
||||
|
||||
// We only need to syncronize other for the same reason we don't have to
|
||||
// synchronize on construction!
|
||||
counter(const counter& other)
|
||||
{
|
||||
boost::mutex::scoped_lock scoped_lock(other.m_mutex);
|
||||
m_value = other.m_value;
|
||||
}
|
||||
|
||||
// For assignment we need to synchronize both objects!
|
||||
const counter& operator=(const counter& other)
|
||||
{
|
||||
if (this == &other)
|
||||
return *this;
|
||||
|
||||
boost::mutex::scoped_lock lock1(&m_mutex < &other.m_mutex ? m_mutex : other.m_mutex);
|
||||
boost::mutex::scoped_lock lock2(&m_mutex > &other.m_mutex ? m_mutex : other.m_mutex);
|
||||
m_value = other.m_value;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
int value() const
|
||||
{
|
||||
boost::mutex::scoped_lock scoped_lock(m_mutex);
|
||||
return m_value;
|
||||
}
|
||||
int increment()
|
||||
{
|
||||
boost::mutex::scoped_lock scoped_lock(m_mutex);
|
||||
return ++m_value;
|
||||
}
|
||||
|
||||
private:
|
||||
mutable boost::mutex m_mutex;
|
||||
int m_value;
|
||||
};
|
||||
</pre>
|
||||
|
||||
<h2>6. How can you lock a <a href="mutex_concept.html">mutex</a> member
|
||||
in a const member function, in order to implement the Monitor
|
||||
Pattern?</h2>
|
||||
|
||||
<p>The Monitor Pattern mutex <a href="bibliography.html#Schmidt-00">
|
||||
[Schmidt 00]</a> should simply be declared as mutable. See the example
|
||||
code above. The internal state of mutex types could have been made
|
||||
mutable, with all lock calls made via const functions, but this does a
|
||||
poor job of documenting the actual semantics. Declaring a mutex member
|
||||
as mutable clearly documentations the intended semantics.</p>
|
||||
|
||||
<h2>7. Why supply <a href="condition.html">condition variables</a>
|
||||
rather than <a href="rationale.html#Events">event variables</a>?</h2>
|
||||
|
||||
<p>Condition variables result in user code much less prone to <a href=
|
||||
"definitions.html#Race condition">race conditions</a> than event
|
||||
variables. See <a href="rationale.html#Events">Rationale</a> for
|
||||
analysis. Also see <a href="bibliography.html#Hoare-74">[Hoare74]</a>
|
||||
and <a href="bibliography.html#Schmidt-00">[Schmidt 00]</a>.</p>
|
||||
|
||||
<h2>8. Why isn't thread cancellation or termination provided?</h2>
|
||||
|
||||
<p>There's a valid need for thread termination, so at some point
|
||||
Boost.Threads probably will include it, but only after we can find a
|
||||
truly safe (and portable) mechanism for this concept.</p>
|
||||
|
||||
<h2>9. Is it safe for threads to share automatic storage duration
|
||||
(stack) objects via pointers or references?</h2>
|
||||
|
||||
<p>Only if you can guarantee that the lifetime of the stack object will
|
||||
not end while other threads might still access the object. Thus the
|
||||
safest practice is to avoid sharing stack objects, particularly in
|
||||
designs where threads are created and destroyed dynamically. Restrict
|
||||
sharing of stack objects to simple designs with very clear and
|
||||
unchanging function and thread lifetimes. (Suggested by Darryl
|
||||
Green).</p>
|
||||
|
||||
<h2>10. Why has class semaphore disappeared?</h2>
|
||||
|
||||
<p>Semaphore was removed as too error prone. The same effect can be
|
||||
achieved with greater safety by the combination of a mutex and a
|
||||
condition variable.</p>
|
||||
<hr>
|
||||
|
||||
<p>Revised
|
||||
<!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan -->05 November, 2001<!--webbot bot="Timestamp" endspan i-checksum="39359" --></p>
|
||||
|
||||
<p><i>© Copyright <a href="mailto:williamkempf@hotmail.com">
|
||||
William E. Kempf</a> 2001 all rights reserved.</i></p>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
1896
doc/future_ref.qbk
Normal file
1896
doc/future_ref.qbk
Normal file
File diff suppressed because it is too large
Load Diff
466
doc/futures.qbk
Executable file
466
doc/futures.qbk
Executable file
@@ -0,0 +1,466 @@
|
||||
[/
|
||||
(C) Copyright 2008-11 Anthony Williams.
|
||||
Distributed under the Boost Software License, Version 1.0.
|
||||
(See accompanying file LICENSE_1_0.txt or copy at
|
||||
http://www.boost.org/LICENSE_1_0.txt).
|
||||
]
|
||||
|
||||
[section:futures Futures]
|
||||
|
||||
[template future_state_link[link_text] [link thread.synchronization.futures.reference.future_state [link_text]]]
|
||||
[def __uninitialized__ [future_state_link `boost::future_state::uninitialized`]]
|
||||
[def __ready__ [future_state_link `boost::future_state::ready`]]
|
||||
[def __waiting__ [future_state_link `boost::future_state::waiting`]]
|
||||
|
||||
[def __future_uninitialized__ `boost::future_uninitialized`]
|
||||
[def __broken_promise__ `boost::broken_promise`]
|
||||
[def __future_already_retrieved__ `boost::future_already_retrieved`]
|
||||
[def __task_moved__ `boost::task_moved`]
|
||||
[def __task_already_started__ `boost::task_already_started`]
|
||||
[def __promise_already_satisfied__ `boost::promise_already_satisfied`]
|
||||
|
||||
[def __thread_interrupted__ `boost::thread_interrupted`]
|
||||
|
||||
|
||||
[template unique_future_link[link_text] [link thread.synchronization.futures.reference.unique_future [link_text]]]
|
||||
[def __unique_future__ [unique_future_link `future`]]
|
||||
[def __unique_future `future`]
|
||||
|
||||
[template unique_future_get_link[link_text] [link thread.synchronization.futures.reference.unique_future.get [link_text]]]
|
||||
[def __unique_future_get__ [unique_future_get_link `boost::future<R>::get()`]]
|
||||
|
||||
[template unique_future_wait_link[link_text] [link thread.synchronization.futures.reference.unique_future.wait [link_text]]]
|
||||
[def __unique_future_wait__ [unique_future_wait_link `boost::future<R>::wait()`]]
|
||||
|
||||
[template unique_future_is_ready_link[link_text] [link thread.synchronization.futures.reference.unique_future.is_ready [link_text]]]
|
||||
[def __unique_future_is_ready__ [unique_future_is_ready_link `boost::future<R>::is_ready()`]]
|
||||
|
||||
[template unique_future_has_value_link[link_text] [link thread.synchronization.futures.reference.unique_future.has_value [link_text]]]
|
||||
[def __unique_future_has_value__ [unique_future_has_value_link `boost::future<R>::has_value()`]]
|
||||
|
||||
[template unique_future_has_exception_link[link_text] [link thread.synchronization.futures.reference.unique_future.has_exception [link_text]]]
|
||||
[def __unique_future_has_exception__ [unique_future_has_exception_link `boost::future<R>::has_exception()`]]
|
||||
|
||||
[template unique_future_get_state_link[link_text] [link thread.synchronization.futures.reference.unique_future.get_state [link_text]]]
|
||||
[def __unique_future_get_state__ [unique_future_get_state_link `boost::future<R>::get_state()`]]
|
||||
|
||||
[template shared_future_link[link_text] [link thread.synchronization.futures.reference.shared_future [link_text]]]
|
||||
[def __shared_future__ [shared_future_link `boost::shared_future`]]
|
||||
|
||||
[template shared_future_get_link[link_text] [link thread.synchronization.futures.reference.shared_future.get [link_text]]]
|
||||
[def __shared_future_get__ [shared_future_get_link `boost::shared_future<R>::get()`]]
|
||||
|
||||
[template shared_future_wait_link[link_text] [link thread.synchronization.futures.reference.shared_future.wait [link_text]]]
|
||||
[def __shared_future_wait__ [shared_future_wait_link `boost::shared_future<R>::wait()`]]
|
||||
|
||||
[template shared_future_is_ready_link[link_text] [link thread.synchronization.futures.reference.shared_future.is_ready [link_text]]]
|
||||
[def __shared_future_is_ready__ [shared_future_is_ready_link `boost::shared_future<R>::is_ready()`]]
|
||||
|
||||
[template shared_future_has_value_link[link_text] [link thread.synchronization.futures.reference.shared_future.has_value [link_text]]]
|
||||
[def __shared_future_has_value__ [shared_future_has_value_link `boost::shared_future<R>::has_value()`]]
|
||||
|
||||
[template shared_future_has_exception_link[link_text] [link thread.synchronization.futures.reference.shared_future.has_exception [link_text]]]
|
||||
[def __shared_future_has_exception__ [shared_future_has_exception_link `boost::shared_future<R>::has_exception()`]]
|
||||
|
||||
[template shared_future_get_state_link[link_text] [link thread.synchronization.futures.reference.shared_future.get_state [link_text]]]
|
||||
[def __shared_future_get_state__ [shared_future_get_state_link `boost::shared_future<R>::get_state()`]]
|
||||
|
||||
[template promise_link[link_text] [link thread.synchronization.futures.reference.promise [link_text]]]
|
||||
[def __promise__ [promise_link `boost::promise`]]
|
||||
|
||||
[template packaged_task_link[link_text] [link thread.synchronization.futures.reference.packaged_task [link_text]]]
|
||||
[def __packaged_task__ [packaged_task_link `boost::packaged_task`]]
|
||||
[def __packaged_task [packaged_task_link `boost::packaged_task`]]
|
||||
|
||||
[template wait_for_any_link[link_text] [link thread.synchronization.futures.reference.wait_for_any [link_text]]]
|
||||
[def __wait_for_any__ [wait_for_any_link `boost::wait_for_any()`]]
|
||||
|
||||
[template wait_for_all_link[link_text] [link thread.synchronization.futures.reference.wait_for_all [link_text]]]
|
||||
[def __wait_for_all__ [wait_for_all_link `boost::wait_for_all()`]]
|
||||
|
||||
|
||||
[section:overview Overview]
|
||||
|
||||
The futures library provides a means of handling synchronous future values, whether those values are generated by another thread, or
|
||||
on a single thread in response to external stimuli, or on-demand.
|
||||
|
||||
This is done through the provision of four class templates: __unique_future__ and __shared_future__ which are used to retrieve the
|
||||
asynchronous results, and __promise__ and __packaged_task__ which are used to generate the asynchronous results.
|
||||
|
||||
An instance of __unique_future__ holds the one and only reference to a result. Ownership can be transferred between instances using
|
||||
the move constructor or move-assignment operator, but at most one instance holds a reference to a given asynchronous result. When
|
||||
the result is ready, it is returned from __unique_future_get__ by rvalue-reference to allow the result to be moved or copied as
|
||||
appropriate for the type.
|
||||
|
||||
On the other hand, many instances of __shared_future__ may reference the same result. Instances can be freely copied and assigned,
|
||||
and __shared_future_get__ returns a non `const` reference so that multiple calls to __shared_future_get__ are safe. You can move an
|
||||
instance of __unique_future__ into an instance of __shared_future__, thus transferring ownership of the associated asynchronous
|
||||
result, but not vice-versa.
|
||||
|
||||
`boost::async` is a simple way of running asynchronous tasks. A call to `boost::async` returns a __unique_future__ that will contain the result of the task.
|
||||
|
||||
|
||||
You can wait for futures either individually or with one of the __wait_for_any__ and __wait_for_all__ functions.
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:creating Creating asynchronous values]
|
||||
|
||||
You can set the value in a future with either a __promise__ or a __packaged_task__. A __packaged_task__ is a callable object that
|
||||
wraps a function or callable object. When the packaged task is invoked, it invokes the contained function in turn, and populates a
|
||||
future with the return value. This is an answer to the perennial question: "how do I return a value from a thread?": package the
|
||||
function you wish to run as a __packaged_task__ and pass the packaged task to the thread constructor. The future retrieved from the
|
||||
packaged task can then be used to obtain the return value. If the function throws an exception, that is stored in the future in
|
||||
place of the return value.
|
||||
|
||||
int calculate_the_answer_to_life_the_universe_and_everything()
|
||||
{
|
||||
return 42;
|
||||
}
|
||||
|
||||
boost::packaged_task<int> pt(calculate_the_answer_to_life_the_universe_and_everything);
|
||||
boost::__unique_future__<int> fi=pt.get_future();
|
||||
|
||||
boost::thread task(boost::move(pt)); // launch task on a thread
|
||||
|
||||
fi.wait(); // wait for it to finish
|
||||
|
||||
assert(fi.is_ready());
|
||||
assert(fi.has_value());
|
||||
assert(!fi.has_exception());
|
||||
assert(fi.get_state()==boost::future_state::ready);
|
||||
assert(fi.get()==42);
|
||||
|
||||
|
||||
A __promise__ is a bit more low level: it just provides explicit functions to store a value or an exception in the associated
|
||||
future. A promise can therefore be used where the value may come from more than one possible source, or where a single operation may
|
||||
produce multiple values.
|
||||
|
||||
boost::promise<int> pi;
|
||||
boost::__unique_future__<int> fi;
|
||||
fi=pi.get_future();
|
||||
|
||||
pi.set_value(42);
|
||||
|
||||
assert(fi.is_ready());
|
||||
assert(fi.has_value());
|
||||
assert(!fi.has_exception());
|
||||
assert(fi.get_state()==boost::future_state::ready);
|
||||
assert(fi.get()==42);
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:lazy_futures Wait Callbacks and Lazy Futures]
|
||||
|
||||
Both __promise__ and __packaged_task__ support ['wait callbacks] that are invoked when a thread blocks in a call to `wait()` or
|
||||
`timed_wait()` on a future that is waiting for the result from the __promise__ or __packaged_task__, in the thread that is doing the
|
||||
waiting. These can be set using the `set_wait_callback()` member function on the __promise__ or __packaged_task__ in question.
|
||||
|
||||
This allows ['lazy futures] where the result is not actually computed until it is needed by some thread. In the example below, the
|
||||
call to `f.get()` invokes the callback `invoke_lazy_task`, which runs the task to set the value. If you remove the call to
|
||||
`f.get()`, the task is not ever run.
|
||||
|
||||
int calculate_the_answer_to_life_the_universe_and_everything()
|
||||
{
|
||||
return 42;
|
||||
}
|
||||
|
||||
void invoke_lazy_task(boost::packaged_task<int>& task)
|
||||
{
|
||||
try
|
||||
{
|
||||
task();
|
||||
}
|
||||
catch(boost::task_already_started&)
|
||||
{}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
boost::packaged_task<int> task(calculate_the_answer_to_life_the_universe_and_everything);
|
||||
task.set_wait_callback(invoke_lazy_task);
|
||||
boost::__unique_future__<int> f(task.get_future());
|
||||
|
||||
assert(f.get()==42);
|
||||
}
|
||||
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:at_thread_exit Handling Detached Threads and Thread Specific Variables]
|
||||
|
||||
Detached threads pose a problem for objects with thread storage duration.
|
||||
If we use a mechanism other than `thread::__join` to wait for a __thread to complete its work - such as waiting for a future to be ready -
|
||||
then the destructors of thread specific variables will still be running after the waiting thread has resumed.
|
||||
This section explain how the standard mechanism can be used to make such synchronization safe by ensuring that the
|
||||
objects with thread storage duration are destroyed prior to the future being made ready. e.g.
|
||||
|
||||
int find_the_answer(); // uses thread specific objects
|
||||
void thread_func(boost::promise<int>&& p)
|
||||
{
|
||||
p.set_value_at_thread_exit(find_the_answer());
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
boost::promise<int> p;
|
||||
boost::thread t(thread_func,boost::move(p));
|
||||
t.detach(); // we're going to wait on the future
|
||||
std::cout<<p.get_future().get()<<std::endl;
|
||||
}
|
||||
|
||||
When the call to `get()` returns, we know that not only is the future value ready, but the thread specific variables
|
||||
on the other thread have also been destroyed.
|
||||
|
||||
Such mechanisms are provided for `boost::condition_variable`, `boost::promise` and `boost::packaged_task`. e.g.
|
||||
|
||||
void task_executor(boost::packaged_task<void(int)> task,int param)
|
||||
{
|
||||
task.make_ready_at_thread_exit(param); // execute stored task
|
||||
} // destroy thread specific and wake threads waiting on futures from task
|
||||
|
||||
Other threads can wait on a future obtained from the task without having to worry about races due to the execution of
|
||||
destructors of the thread specific objects from the task's thread.
|
||||
|
||||
boost::condition_variable cv;
|
||||
boost::mutex m;
|
||||
complex_type the_data;
|
||||
bool data_ready;
|
||||
|
||||
void thread_func()
|
||||
{
|
||||
boost::unique_lock<std::mutex> lk(m);
|
||||
the_data=find_the_answer();
|
||||
data_ready=true;
|
||||
boost::notify_all_at_thread_exit(cv,boost::move(lk));
|
||||
} // destroy thread specific objects, notify cv, unlock mutex
|
||||
|
||||
void waiting_thread()
|
||||
{
|
||||
boost::unique_lock<std::mutex> lk(m);
|
||||
while(!data_ready)
|
||||
{
|
||||
cv.wait(lk);
|
||||
}
|
||||
process(the_data);
|
||||
}
|
||||
|
||||
The waiting thread is guaranteed that the thread specific objects used by `thread_func()` have been destroyed by the time
|
||||
`process(the_data)` is called. If the lock on `m` is released and re-acquired after setting `data_ready` and before calling
|
||||
`boost::notify_all_at_thread_exit()` then this does NOT hold, since the thread may return from the wait due to a
|
||||
spurious wake-up.
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:async Executing asynchronously]
|
||||
|
||||
`boost::async` is a simple way of running asynchronous tasks to make use of the available hardware concurrency.
|
||||
A call to `boost::async` returns a `boost::future` that will contain the result of the task. Depending on
|
||||
the launch policy, the task is either run asynchronously on its own thread or synchronously on whichever thread
|
||||
calls the `wait()` or `get()` member functions on that `future`.
|
||||
|
||||
A launch policy of either boost::launch::async, which asks the runtime to create an asynchronous thread,
|
||||
or boost::launch::deferred, which indicates you simply want to defer the function call until a later time (lazy evaluation).
|
||||
This argument is optional - if you omit it your function will use the default policy.
|
||||
|
||||
For example, consider computing the sum of a very large array. The first task is to not compute asynchronously when
|
||||
the overhead would be significant. The second task is to split the work into two pieces, one executed by the host
|
||||
thread and one executed asynchronously.
|
||||
|
||||
|
||||
int parallel_sum(int* data, int size)
|
||||
{
|
||||
int sum = 0;
|
||||
if ( size < 1000 )
|
||||
for ( int i = 0; i < size; ++i )
|
||||
sum += data[i];
|
||||
else {
|
||||
auto handle = boost::async(parallel_sum, data+size/2, size-size/2);
|
||||
sum += parallel_sum(data, size/2);
|
||||
sum += handle.get();
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:shared Shared Futures]
|
||||
|
||||
`shared_future` is designed to be shared between threads,
|
||||
that is to allow multiple concurrent get operations.
|
||||
|
||||
[heading Multiple get]
|
||||
|
||||
The second `get()` call in the following example future
|
||||
|
||||
void bad_second_use( type arg ) {
|
||||
|
||||
auto ftr = async( [=]{ return work( arg ); } );
|
||||
if ( cond1 )
|
||||
{
|
||||
use1( ftr.get() );
|
||||
} else
|
||||
{
|
||||
use2( ftr.get() );
|
||||
}
|
||||
use3( ftr.get() ); // second use is undefined
|
||||
}
|
||||
|
||||
Using a `shared_mutex` solves the issue
|
||||
|
||||
void good_second_use( type arg ) {
|
||||
|
||||
shared_future<type> ftr = async( [=]{ return work( arg ); } );
|
||||
if ( cond1 )
|
||||
{
|
||||
use1( ftr.get() );
|
||||
} else
|
||||
{
|
||||
use2( ftr.get() );
|
||||
}
|
||||
use3( ftr.get() ); // second use is defined
|
||||
}
|
||||
|
||||
[heading share()]
|
||||
|
||||
Namming the return type when declaring the `shared_future` is needed; auto is not available within template argument lists.
|
||||
Here `share()` could be used to simplify the code
|
||||
|
||||
void better_second_use( type arg ) {
|
||||
|
||||
auto ftr = async( [=]{ return work( arg ); } ).share();
|
||||
if ( cond1 )
|
||||
{
|
||||
use1( ftr.get() );
|
||||
} else
|
||||
{
|
||||
use2( ftr.get() );
|
||||
}
|
||||
use3( ftr.get() ); // second use is defined
|
||||
}
|
||||
|
||||
[heading Writting on get()]
|
||||
|
||||
The user can either read or write the future avariable.
|
||||
|
||||
void write_to_get( type arg ) {
|
||||
|
||||
auto ftr = async( [=]{ return work( arg ); } ).share();
|
||||
if ( cond1 )
|
||||
{
|
||||
use1( ftr.get() );
|
||||
} else
|
||||
{
|
||||
if ( cond2 )
|
||||
use2( ftr.get() );
|
||||
else
|
||||
ftr.get() = something(); // assign to non-const reference.
|
||||
}
|
||||
use3( ftr.get() ); // second use is defined
|
||||
}
|
||||
|
||||
This works because the `shared_future<>::get()` function returns a non-const reference to the appropriate storage.
|
||||
Of course the access to this storage must be ensured by the user. The library doesn't ensure the access to the internal storage is thread safe.
|
||||
|
||||
There has been some work by the C++ standard committe on an `atomic_future` that behaves as an `atomic` variable, that is is thread_safe,
|
||||
and a `shared_future` that can be shared between several threads, but there were not enough consensus and time to get it ready for C++11.
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:make_future Making immediate futures easier]
|
||||
|
||||
Some functions may know the value at the point of construction. In these cases the value is immediately available,
|
||||
but needs to be returned as a future or shared_future. By using make_future (make_shared_future) a future (shared_future)
|
||||
can be created which holds a pre-computed result in its shared state.
|
||||
|
||||
Without these features it is non-trivial to create a future directly from a value.
|
||||
First a promise must be created, then the promise is set, and lastly the future is retrieved from the promise.
|
||||
This can now be done with one operation.
|
||||
|
||||
[heading make_future / make_shared_future]
|
||||
|
||||
This function creates a future for a given value. If no value is given then a future<void> is returned.
|
||||
This function is primarily useful in cases where sometimes, the return value is immediately available, but sometimes
|
||||
it is not. The example below illustrates, that in an error path the value is known immediately, however in other paths
|
||||
the function must return an eventual value represented as a future.
|
||||
|
||||
|
||||
boost::future<int> compute(int x)
|
||||
{
|
||||
if (x == 0) return boost::make_future(0);
|
||||
if (x < 0) return boost::make_future(-1);
|
||||
boost::future<int> f1 = boost::async([]() { return x+1; });
|
||||
return f1;
|
||||
}
|
||||
|
||||
There are two variations of this function. The first takes a value of any type, and returns a future of that type.
|
||||
The input value is passed to the shared state of the returned future. The second version takes no input and returns a future<void>.
|
||||
make_shared_future has the same functionality as make_future, except has a return type of shared_future.
|
||||
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:then Associating future continuations]
|
||||
|
||||
In asynchronous programming, it is very common for one asynchronous operation, on completion, to invoke a second
|
||||
operation and pass data to it. The current C++ standard does not allow one to register a continuation to a future.
|
||||
With .then, instead of waiting for the result, a continuation is "attached" to the asynchronous operation, which is
|
||||
invoked when the result is ready. Continuations registered using the .then function will help to avoid blocking waits
|
||||
or wasting threads on polling, greatly improving the responsiveness and scalability of an application.
|
||||
|
||||
future.then provides the ability to sequentially compose two futures by declaring one to be the continuation of another.
|
||||
With .then the antecedent future is ready (has a value or exception stored in the shared state) before the continuation
|
||||
starts as instructed by the lambda function.
|
||||
|
||||
In the example below the future<int> f2 is registered to be a continuation of future<int> f1 using the .then member
|
||||
function. This operation takes a lambda function which describes how f2 should proceed after f1 is ready.
|
||||
|
||||
|
||||
#include <boost/thread/future.hpp>
|
||||
using namespace boost;
|
||||
int main()
|
||||
{
|
||||
future<int> f1 = async([]() { return 123; });
|
||||
future<string> f2 = f1.then([](future<int> f) { return f.get().to_string(); // here .get() won't block });
|
||||
}
|
||||
|
||||
One key feature of this function is the ability to chain multiple asynchronous operations. In asynchronous programming,
|
||||
it's common to define a sequence of operations, in which each continuation executes only when the previous one completes.
|
||||
In some cases, the antecedent future produces a value that the continuation accepts as input. By using future.then,
|
||||
creating a chain of continuations becomes straightforward and intuitive:
|
||||
|
||||
myFuture.then(...).then(...).then(...).
|
||||
|
||||
Some points to note are:
|
||||
|
||||
* Each continuation will not begin until the preceding has completed.
|
||||
* If an exception is thrown, the following continuation can handle it in a try-catch block
|
||||
|
||||
|
||||
Input Parameters:
|
||||
|
||||
* Lambda function2: One option which was considered was to follow JavaScript's approach and take two functions, one for
|
||||
success and one for error handling. However this option is not viable in C++ as there is no single base type for
|
||||
exceptions as there is in JavaScript. The lambda function takes a future as its input which carries the exception
|
||||
through. This makes propagating exceptions straightforward. This approach also simplifies the chaining of continuations.
|
||||
* Scheduler: Providing an overload to .then, to take a scheduler reference places great flexibility over the execution
|
||||
of the future in the programmer's hand. As described above, often taking a launch policy is not sufficient for powerful
|
||||
asynchronous operations. The lifetime of the scheduler must outlive the continuation.
|
||||
* Launch policy: if the additional flexibility that the scheduler provides is not required.
|
||||
|
||||
|
||||
Return values: The decision to return a future was based primarily on the ability to chain multiple continuations using
|
||||
.then. This benefit of composability gives the programmer incredible control and flexibility over their code. Returning
|
||||
a future object rather than a shared_future is also a much cheaper operation thereby improving performance. A
|
||||
shared_future object is not necessary to take advantage of the chaining feature. It is also easy to go from a future
|
||||
to a shared_future when needed using future::share().
|
||||
|
||||
|
||||
[endsect]
|
||||
|
||||
|
||||
[include future_ref.qbk]
|
||||
|
||||
[endsect]
|
||||
146
doc/index.html
146
doc/index.html
@@ -1,138 +1,12 @@
|
||||
<!-- Copyright (c) 2002-2003 Beman Dawes, William E. Kempf.
|
||||
Subject to the Boost Software License, Version 1.0.
|
||||
(See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
|
||||
-->
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content=
|
||||
"text/html; charset=iso-8859-1">
|
||||
<meta name="keywords" content="threads, BTL, thread library, C++">
|
||||
|
||||
<title>Boost.Threads, Index</title>
|
||||
</head>
|
||||
|
||||
<body bgcolor="#FFFFFF" link="#0000FF" vlink="#800080">
|
||||
<table summary="header" border="0" cellpadding="7" cellspacing="0"
|
||||
width="100%">
|
||||
<tr>
|
||||
<td valign="top" width="300">
|
||||
<h3><img src="../../../c++boost.gif" alt="C++ Boost" width=
|
||||
"277" height="86"></h3>
|
||||
</td>
|
||||
|
||||
<td valign="top">
|
||||
<h1 align="center">Boost.Threads</h1>
|
||||
|
||||
<h2 align="center">Documentation Map</h2>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<hr>
|
||||
|
||||
<h2>Contents</h2>
|
||||
|
||||
<ul>
|
||||
<li><a href="overview.html">Overview</a></li>
|
||||
|
||||
<li>
|
||||
<a href="mutex_concept.html">Mutex Concepts</a>
|
||||
|
||||
<ul>
|
||||
<li><a href="mutex_concept.html#Mutex">Mutex</a></li>
|
||||
|
||||
<li><a href="mutex_concept.html#TryMutex">TryMutex</a></li>
|
||||
|
||||
<li><a href="mutex_concept.html#TimedMutex">
|
||||
TimedMutex</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
|
||||
<li>
|
||||
Mutex Classes
|
||||
|
||||
<ul>
|
||||
<li><a href="mutex.html">mutex / try_mutex /
|
||||
timed_mutex</a></li>
|
||||
|
||||
<li><a href="recursive_mutex.html">recursive_mutex /
|
||||
recursive_try_mutex / recursive_timed_mutex</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
|
||||
<li>
|
||||
<a href="lock_concept.html">Lock Concepts</a>
|
||||
|
||||
<ul>
|
||||
<li><a href="lock_concept.html#Lock">Lock</a></li>
|
||||
|
||||
<li><a href="lock_concept.html#ScopedLock">
|
||||
ScopedLock</a></li>
|
||||
|
||||
<li><a href="lock_concept.html#ScopedTryLock">
|
||||
ScopedTryLock</a></li>
|
||||
|
||||
<li><a href="lock_concept.html#ScopedTimedLock">
|
||||
ScopedTimedLock</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
|
||||
<li>
|
||||
Lock Classes
|
||||
|
||||
<ul>
|
||||
<li><a href="scoped_lock.html">scoped_lock</a></li>
|
||||
|
||||
<li><a href="scoped_try_lock.html">scoped_try_lock</a></li>
|
||||
|
||||
<li><a href="scoped_timed_lock.html">
|
||||
scoped_timed_lock</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
|
||||
<li>Class <a href="condition.html">condition</a></li>
|
||||
|
||||
<li>Class <a href="thread_specific_ptr.html">
|
||||
thread_specific_ptr</a></li>
|
||||
|
||||
<li>Class <a href="thread.html">thread</a></li>
|
||||
|
||||
<li>Class <a href="thread_group.html">thread_group</a></li>
|
||||
|
||||
<li>Class <a href="xtime.html">xtime</a></li>
|
||||
|
||||
<li>Class <a href="lock_error.html">lock_error</a></li>
|
||||
|
||||
<li>Class <a href="thread_resource_error.html">
|
||||
thread_resource_error</a></li>
|
||||
|
||||
<li>Routine <a href="call_once.html">call_once</a></li>
|
||||
|
||||
<li><a href="config.html">Configuration Information</a></li>
|
||||
|
||||
<li><a href="introduction.html">Introduction to design</a></li>
|
||||
|
||||
<li><a href="rationale.html">Rationale for design
|
||||
decisions</a></li>
|
||||
|
||||
<li><a href="definitions.html">Definitions</a></li>
|
||||
|
||||
<li><a href="faq.html">Frequently Asked Questions</a></li>
|
||||
|
||||
<li><a href="bibliography.html">Bibliography</a></li>
|
||||
|
||||
<li><a href="acknowledgements.html">Acknowledgements</a></li>
|
||||
</ul>
|
||||
<hr>
|
||||
|
||||
<p>Revised
|
||||
<!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan -->05 November, 2001<!--webbot bot="Timestamp" endspan i-checksum="39359" --></p>
|
||||
|
||||
<p>© <i>Copyright <a href="mailto:williamkempf@hotmail.com">
|
||||
William E. Kempf</a> 2001</i></p>
|
||||
|
||||
<p>Permission to use, copy, modify, distribute and sell this software
|
||||
and its documentation for any purpose is hereby granted without fee,
|
||||
provided that the above copyright notice appear in all copies and that
|
||||
both that copyright notice and this permission notice appear in
|
||||
supporting documentation. William E. Kempf makes no representations
|
||||
about the suitability of this software for any purpose. It is provided
|
||||
"as is" without express or implied warranty.</p>
|
||||
</body>
|
||||
<head>
|
||||
<meta http-equiv="refresh" content="0; URL=../../../doc/html/thread.html">
|
||||
</head>
|
||||
<body>
|
||||
Automatic redirection failed, please go to <a href="../../../doc/html/thread.html">../../../doc/html/thread.html</a>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
|
||||
450
doc/internal_locking.qbk
Normal file
450
doc/internal_locking.qbk
Normal file
@@ -0,0 +1,450 @@
|
||||
[/
|
||||
/ Copyright (c) 2008 Vicente J. Botet Escriba
|
||||
/
|
||||
/ Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
/ file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
/]
|
||||
|
||||
|
||||
[section Internal Locking]
|
||||
[note This tutorial is an adaptation of chapter Concurrency of the Object-Oriented Programming in the BETA Programming Language and of the paper of Andrei Alexandrescu "Multithreading and the C++ Type System" to the Boost library.]
|
||||
[section Concurrent threads of execution]
|
||||
|
||||
Consider, for example, modeling a bank account class that supports simultaneous deposits and withdrawals from multiple locations (arguably the "Hello, World" of multithreaded programming).
|
||||
|
||||
From here a component is a model of the `Callable` concept.
|
||||
|
||||
On C++11 (Boost) concurrent execution of a component is obtained by means of the `std::thread`(`boost::thread`):
|
||||
|
||||
boost::thread thread1(S);
|
||||
|
||||
where `S` is a model of `Callable`. The meaning of this expression is that execution of `S()` will take place concurrently with the current thread of execution executing the expression.
|
||||
|
||||
The following example includes a bank account of a person (Joe) and two components, one corresponding to a bank agent depositing money in Joe's account, and one representing Joe. Joe will only be withdrawing money from the account:
|
||||
|
||||
class BankAccount;
|
||||
|
||||
BankAccount JoesAccount;
|
||||
|
||||
void bankAgent()
|
||||
{
|
||||
for (int i =10; i>0; --i) {
|
||||
//...
|
||||
JoesAccount.Deposit(500);
|
||||
//...
|
||||
}
|
||||
}
|
||||
|
||||
void Joe() {
|
||||
for (int i =10; i>0; --i) {
|
||||
//...
|
||||
int myPocket = JoesAccount.Withdraw(100);
|
||||
std::cout << myPocket << std::endl;
|
||||
//...
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
//...
|
||||
boost::thread thread1(bankAgent); // start concurrent execution of bankAgent
|
||||
boost::thread thread2(Joe); // start concurrent execution of Joe
|
||||
thread1.join();
|
||||
thread2.join();
|
||||
return 0;
|
||||
}
|
||||
|
||||
From time to time, the `bankAgent` will deposit $500 in `JoesAccount`. Joe will similarly withdraw $100 from his account. These sentences describe that the bankAgent and Joe are executed concurrently.
|
||||
|
||||
The above example works well as long as the bankAgent and Joe doesn't access JoesAccount at the same time. There is, however, no guarantee that this will not happen. We may use a mutex to guarantee exclusive access to each bank.
|
||||
|
||||
class BankAccount {
|
||||
boost::mutex mtx_;
|
||||
int balance_;
|
||||
public:
|
||||
void Deposit(int amount) {
|
||||
mtx_.lock();
|
||||
balance_ += amount;
|
||||
mtx_.unlock();
|
||||
}
|
||||
void Withdraw(int amount) {
|
||||
mtx_.lock();
|
||||
balance_ -= amount;
|
||||
mtx_.unlock();
|
||||
}
|
||||
int GetBalance() {
|
||||
mtx_.lock();
|
||||
int b = balance_;
|
||||
mtx_.unlock();
|
||||
return balance_;
|
||||
}
|
||||
};
|
||||
|
||||
Execution of the Deposit and Withdraw operations will no longer be able to make simultaneous access to balance.
|
||||
|
||||
Mutex is a simple and basic mechanism for obtaining synchronization. In the above example it is relatively easy to be convinced that the synchronization works correctly (in the absence of exception). In a system with several concurrent objects and several shared objects, it may be difficult to describe synchronization by means of mutexes. Programs that make heavy use of mutexes may be difficult to read and write. Instead, we shall introduce a number of generic classes for handling more complicated forms of synchronization and communication.
|
||||
|
||||
With the RAII idiom we can simplify a lot this using the scoped locks. In the code below, guard's constructor locks the passed-in object this, and guard's destructor unlocks this.
|
||||
|
||||
class BankAccount {
|
||||
boost::mutex mtx_; // explicit mutex declaration
|
||||
int balance_;
|
||||
public:
|
||||
void Deposit(int amount) {
|
||||
boost::lock_guard<boost::mutex> guard(mtx_);
|
||||
balance_ += amount;
|
||||
}
|
||||
void Withdraw(int amount) {
|
||||
boost::lock_guard<boost::mutex> guard(mtx_);
|
||||
balance_ -= amount;
|
||||
}
|
||||
int GetBalance() {
|
||||
boost::lock_guard<boost::mutex> guard(mtx_);
|
||||
return balance_;
|
||||
}
|
||||
};
|
||||
|
||||
The object-level locking idiom doesn't cover the entire richness of a threading model. For example, the model above is quite deadlock-prone when you try to coordinate multi-object transactions. Nonetheless, object-level locking is useful in many cases, and in combination with other mechanisms can provide a satisfactory solution to many threaded access problems in object-oriented programs.
|
||||
|
||||
The BankAccount class above uses internal locking. Basically, a class that uses internal locking guarantees that any concurrent calls to its public member functions don't corrupt an instance of that class. This is typically ensured by having each public member function acquire a lock on the object upon entry. This way, for any given object of that class, there can be only one member function call active at any moment, so the operations are nicely serialized.
|
||||
|
||||
This approach is reasonably easy to implement and has an attractive simplicity. Unfortunately, "simple" might sometimes morph into "simplistic."
|
||||
|
||||
Internal locking is insufficient for many real-world synchronization tasks. Imagine that you want to implement an ATM withdrawal transaction with the BankAccount class. The requirements are simple. The ATM transaction consists of two withdrawals-one for the actual money and one for the $2 commission. The two withdrawals must appear in strict sequence; that is, no other transaction can exist between them.
|
||||
|
||||
The obvious implementation is erratic:
|
||||
|
||||
void ATMWithdrawal(BankAccount& acct, int sum) {
|
||||
acct.Withdraw(sum);
|
||||
// preemption possible
|
||||
acct.Withdraw(2);
|
||||
}
|
||||
|
||||
The problem is that between the two calls above, another thread can perform another operation on the account, thus breaking the second design requirement.
|
||||
|
||||
In an attempt to solve this problem, let's lock the account from the outside during the two operations:
|
||||
|
||||
void ATMWithdrawal(BankAccount& acct, int sum) {
|
||||
boost::lock_guard<boost::mutex> guard(acct.mtx_); 1
|
||||
acct.Withdraw(sum);
|
||||
acct.Withdraw(2);
|
||||
}
|
||||
|
||||
Notice that the code above doesn't compiles, the `mtx_` field is private.
|
||||
We have two possibilities:
|
||||
|
||||
* make `mtx_` public which seams odd
|
||||
* make the `BankAccount` lockable by adding the lock/unlock functions
|
||||
|
||||
We can add these functions explicitly
|
||||
|
||||
class BankAccount {
|
||||
boost::mutex mtx_;
|
||||
int balance_;
|
||||
public:
|
||||
void Deposit(int amount) {
|
||||
boost::lock_guard<boost::mutex> guard(mtx_);
|
||||
balance_ += amount;
|
||||
}
|
||||
void Withdraw(int amount) {
|
||||
boost::lock_guard<boost::mutex> guard(mtx_);
|
||||
balance_ -= amount;
|
||||
}
|
||||
void lock() {
|
||||
mtx_.lock();
|
||||
}
|
||||
void unlock() {
|
||||
mtx_.unlock();
|
||||
}
|
||||
};
|
||||
|
||||
or inheriting from a class which add these lockable functions.
|
||||
|
||||
The `basic_lockable_adapter` class helps to define the `BankAccount` class as
|
||||
|
||||
class BankAccount
|
||||
: public basic_lockable_adapter<mutex>
|
||||
{
|
||||
int balance_;
|
||||
public:
|
||||
void Deposit(int amount) {
|
||||
boost::lock_guard<BankAccount> guard(*this);
|
||||
balance_ += amount;
|
||||
}
|
||||
void Withdraw(int amount) {
|
||||
boost::lock_guard<BankAccount> guard(*this);
|
||||
balance_ -= amount;
|
||||
}
|
||||
int GetBalance() {
|
||||
boost::lock_guard<BankAccount> guard(*this);
|
||||
return balance_;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
and the code that doesn't compiles becomes
|
||||
|
||||
void ATMWithdrawal(BankAccount& acct, int sum) {
|
||||
boost::lock_guard<BankAccount> guard(acct);
|
||||
acct.Withdraw(sum);
|
||||
acct.Withdraw(2);
|
||||
}
|
||||
|
||||
Notice that now acct is being locked by Withdraw after it has already been locked by guard. When running such code, one of two things happens.
|
||||
|
||||
* Your mutex implementation might support the so-called recursive mutex semantics. This means that the same thread can lock the same mutex several times successfully. In this case, the implementation works but has a performance overhead due to unnecessary locking. (The locking/unlocking sequence in the two Withdraw calls is not needed but performed anyway-and that costs time.)
|
||||
* Your mutex implementation might not support recursive locking, which means that as soon as you try to acquire it the second time, it blocks-so the ATMWithdrawal function enters the dreaded deadlock.
|
||||
|
||||
As `boost::mutex` is not recursive, we need to use its recursive version `boost::recursive_mutex`.
|
||||
|
||||
class BankAccount
|
||||
: public basic_lockable_adapter<recursive_mutex>
|
||||
{
|
||||
|
||||
// ...
|
||||
};
|
||||
|
||||
|
||||
[endsect]
|
||||
|
||||
[/
|
||||
[section Monitors]
|
||||
|
||||
The use of `mutex` and `lockers`, as in `BankAccount`, is a common way of defining objects shared by two or more concurrent components. The basic_lockable_adapter class was a first step.
|
||||
We shall therefore introduce an abstraction that makes it easier to define such objects.
|
||||
The following class describes a so-called monitor pattern.
|
||||
|
||||
template <
|
||||
typename Lockable=mutex
|
||||
>
|
||||
class basic_monitor : protected basic_lockable_adapter<Lockable> { // behaves like an BasicLockable for the derived classes
|
||||
protected:
|
||||
typedef unspecified synchronizer; // is an strict lock guard
|
||||
};
|
||||
|
||||
[/shared_monitor]
|
||||
[/monitor]
|
||||
|
||||
A basic_monitor object behaves like a `BasicLockable` object but only for the inheriting classes.
|
||||
Protected inheritance from lockable_adapter provide to all the derived classes all BasicLockable operations. In addition has a protected nested class, synchronizer, used when defining the monitor operations to synchronize the access critical regions. The BankAccount may be described using Monitor in the following way:
|
||||
|
||||
class BankAccount : protected basic_monitor<>
|
||||
{
|
||||
protected:
|
||||
int balance_;
|
||||
public:
|
||||
BankAccount() : balance_(0) {}
|
||||
BankAccount(const BankAccount &rhs) {
|
||||
synchronizer _(*rhs.mutex());
|
||||
balance_=rhs.balance_;
|
||||
}
|
||||
|
||||
BankAccount& operator=(BankAccount &rhs)
|
||||
{
|
||||
if(&rhs == this) return *this;
|
||||
|
||||
int balance=0;
|
||||
{
|
||||
synchronizer _(*rhs.mutex());
|
||||
balance=rhs.balance_;
|
||||
}
|
||||
synchronizer _(*this->mutex());
|
||||
balance_=balance;
|
||||
return *this;
|
||||
}
|
||||
|
||||
void Deposit(int amount) {
|
||||
synchronizer _(*this->mutex());
|
||||
balance_ += amount;
|
||||
}
|
||||
int Withdraw(int amount) {
|
||||
synchronizer _(*this->mutex());
|
||||
balance_ -= amount;
|
||||
return amount;
|
||||
}
|
||||
int GetBalance() {
|
||||
synchronizer _(*this->mutex());
|
||||
return balance_;
|
||||
}
|
||||
};
|
||||
|
||||
In the following, a monitor means some sub-class of monitor. A synchronized operation means an operation using the synchronizer guard defined within some monitor. Monitor is one example of a high-level concurrency abstraction that can be defined by means of mutexes.
|
||||
|
||||
|
||||
[section Monitor Conditions]
|
||||
|
||||
It may happen that a component executing an entry operation of a monitor is unable to continue execution due to some condition not being fulfilled. Consider, for instance, a bounded buffer of characters. Such a buffer may be implemented as a monitor with two operations Push and Pull: the Puss operation cannot be executed if the buffer is full, and the Pull operation cannot be executed if the buffer is empty. A sketch of such a buffer monitor may look as
|
||||
follows:
|
||||
|
||||
class sync_buffer {
|
||||
boost::mutex mtx_; 1
|
||||
public:
|
||||
...
|
||||
bool full() { return in_==out_; }
|
||||
bool empty() { return in_==(out_%size)+1; }
|
||||
void push(T& v) {
|
||||
// wait if buffer is full
|
||||
data_[in_]=v;
|
||||
in_ = (in_% size)+1;
|
||||
}
|
||||
T pull() {
|
||||
// wait if buffer is empty
|
||||
out_ = (out_% size)+1;
|
||||
return data_[out_];
|
||||
}
|
||||
};
|
||||
|
||||
The meaning of a wait is that the calling component is delayed until the condition becomes true. We can do that using Boost.Thread condition variables like:
|
||||
|
||||
template <typename T, unsigned size>
|
||||
class sync_buffer
|
||||
{
|
||||
typedef boost::mutex mutex_type;
|
||||
typedef boost::condition_variable condition_type;
|
||||
typedef boost::unique_lock<mutex_type> unique_lock_type;
|
||||
mutex_type mtx_;
|
||||
condition_type not_full_;
|
||||
condition_type not_empty_;
|
||||
|
||||
T data_[size+1];
|
||||
unsigned in_, out_;
|
||||
|
||||
public:
|
||||
sync_buffer():in_(0), out_(0) {}
|
||||
|
||||
bool full() { return out_==(in_+1)%(size+1); }
|
||||
bool empty() { return out_==in_; }
|
||||
|
||||
unsigned get_in() {return in_;}
|
||||
unsigned get_out() {return out_;}
|
||||
void push(T v) {
|
||||
unique_lock_type guard(mtx_); 1
|
||||
while (full()) { 2
|
||||
not_full_.wait(guard);
|
||||
}
|
||||
data_[in_]=v;
|
||||
in_ = (in_+1)% (size+1);
|
||||
not_empty_.notify_one(); 3
|
||||
}
|
||||
|
||||
T pull() {
|
||||
unique_lock_type guard(mtx_); 4
|
||||
while (empty()) { 5
|
||||
not_empty_.wait(guard);
|
||||
}
|
||||
unsigned idx = out_;
|
||||
out_ = (out_+1)% (size+1);
|
||||
not_full_.notify_one(); 6
|
||||
return data_[idx];
|
||||
}
|
||||
};
|
||||
|
||||
The Monitor class replace the nested synchronizer unique_lock with the class `condition_unique_lock` for this purpose:
|
||||
|
||||
template <
|
||||
typename Lockable,
|
||||
class Condition=condition_safe<typename best_condition<Lockable>::type >
|
||||
, typename ScopeTag=typename scope_tag<Lockable>::type
|
||||
>
|
||||
class condition_unique_lock
|
||||
: protected unique_lock<Lockable,ScopeTag>
|
||||
{
|
||||
BOOST_CONCEPT_ASSERT((LockableConcept<Lockable>));
|
||||
public:
|
||||
typedef Lockable lockable_type;
|
||||
typedef Condition condition;
|
||||
|
||||
explicit condition_unique_lock(lockable_type& obj); 1
|
||||
condition_unique_lock(lockable_type& obj, condition &cond); 2
|
||||
template <typename Predicate>
|
||||
condition_unique_lock(lockable_type& obj, condition &cond, Predicate pred); 3
|
||||
~condition_unique_lock() 4
|
||||
|
||||
typedef bool (condition_unique_lock::*bool_type)() const; 5
|
||||
operator bool_type() const; 6
|
||||
bool operator!() const { return false; } 7
|
||||
bool owns_lock() const { return true; } 8
|
||||
bool is_locking(lockable_type* l) const 9
|
||||
|
||||
void relock_on(condition & cond);
|
||||
template<typename Clock, typename Duration>
|
||||
void relock_until(condition & cond, chrono::time_point<Clock, Duration> const& abs_time);
|
||||
template<typename duration_type>
|
||||
void relock_on_for(condition & cond, duration_type const& rel_time);
|
||||
|
||||
template<typename Predicate>
|
||||
void relock_when(condition &cond, Predicate pred);
|
||||
template<typename Predicate>
|
||||
template<typename Clock, typename Duration>
|
||||
void relock_when_until(condition &cond, Predicate pred,
|
||||
chrono::time_point<Clock, Duration> const& abs_time);
|
||||
template<typename Predicate, typename duration_type>
|
||||
void relock_when_for(condition &cond, Predicate pred,
|
||||
duration_type const& rel_time);
|
||||
|
||||
10
|
||||
};
|
||||
|
||||
|
||||
We may now give the complete version of the buffer class. The content of the buffer is: `data_[out_+1], data_[out_+2], ... data_R[in_-1]` where all the indexes are modulo size. The buffer is full if `in_=out_` and it is empty if `in_=(out_+1)%size`.
|
||||
|
||||
template <typename T, unsigned size>
|
||||
class sync_buffer : protected basic_monitor<>
|
||||
{
|
||||
condition not_full_;
|
||||
condition not_empty_;
|
||||
|
||||
T data_[size+1];
|
||||
unsigned in_, out_;
|
||||
|
||||
struct not_full {
|
||||
explicit not_full(sync_buffer &b):that_(b){};
|
||||
bool operator()() const { return !that_.full(); }
|
||||
sync_buffer &that_;
|
||||
};
|
||||
struct not_empty {
|
||||
explicit not_empty(sync_buffer &b):that_(b){};
|
||||
bool operator()() const { return !that_.empty(); }
|
||||
sync_buffer &that_;
|
||||
};
|
||||
public:
|
||||
BOOST_COPY_CONSTRUCTOR_DELETE(sync_buffer) 1
|
||||
BOOST_COPY_ASSIGNEMENT_DELETE(sync_buffer) 2
|
||||
sync_buffer():in_(0), out_(0) {}
|
||||
|
||||
bool full() { return out_==(in_+1)%(size+1); }
|
||||
bool empty() { return out_==in_; }
|
||||
|
||||
unsigned get_in() {return in_;}
|
||||
unsigned get_out() {return out_;}
|
||||
|
||||
void push(T v) {
|
||||
synchronizer _(*this->mutex(), not_full_, not_full(*this)); 3
|
||||
data_[in_]=v;
|
||||
in_ = (in_+1)% (size+1);
|
||||
not_empty_.notify_one(); 4
|
||||
}
|
||||
|
||||
T pull() {
|
||||
synchronizer _(*this->mutex(), not_empty_, not_empty(*this)); 5
|
||||
unsigned idx = out_;
|
||||
out_ = (out_+1)% (size+1);
|
||||
not_full_.notify_one(); 6
|
||||
return data_[idx];
|
||||
}
|
||||
};
|
||||
|
||||
Monitors and conditions are useful for describing simple cases of shared objects (by simple we mean a limited use of conditions). If the conditions for delaying a calling component become complicated, the monitor may similarly become difficult to program and read.
|
||||
|
||||
[endsect] [/Monitor Conditions]
|
||||
|
||||
[endsect] [/Monitors]
|
||||
]
|
||||
|
||||
[section Synchronized variables]
|
||||
[/include synchronized_value.qbk]
|
||||
[endsect] [/Synchronized variables]
|
||||
|
||||
|
||||
[endsect] [/Internal Locking]
|
||||
|
||||
|
||||
@@ -1,194 +0,0 @@
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content=
|
||||
"text/html; charset=iso-8859-1">
|
||||
<meta name="keywords" content="threads, BTL, thread library, C++">
|
||||
|
||||
<title>Boost.Threads, Introduction</title>
|
||||
</head>
|
||||
|
||||
<body bgcolor="#ffffff" link="#0000ff" vlink="#800080">
|
||||
<table summary="header" border="0" cellpadding="7" cellspacing="0"
|
||||
width="100%">
|
||||
<tr>
|
||||
<td valign="top" width="300">
|
||||
<h3><img height="86" alt="C++ Boost" src=
|
||||
"../../../c++boost.gif" width="277"></h3>
|
||||
</td>
|
||||
|
||||
<td valign="top">
|
||||
<h1 align="center">Boost.Threads</h1>
|
||||
|
||||
<h2 align="center">Introduction</h2>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<hr>
|
||||
|
||||
<h3>Motivation</h3>
|
||||
|
||||
<p>With client/server and three-tier architectures becoming common
|
||||
place in today's world, it's becoming increasingly important
|
||||
for programs to be able to handle parallel processing. Modern day
|
||||
operating systems usually provide some support for this through native
|
||||
thread APIs. Unfortunately, writing portable code that makes use of
|
||||
parallel processing in C++ is made very difficult by a lack of a
|
||||
standard interface for these native APIs. Further, these APIs are
|
||||
almost universally C APIs and fail to take advantage of C++'s
|
||||
strengths, or to address C++'s issues.</p>
|
||||
|
||||
<p>The <b>Boost.Threads</b> library is an attempt to define a portable
|
||||
interface for writing parallel processes in C++.</p>
|
||||
|
||||
<h3>Goals</h3>
|
||||
|
||||
<p>The <b>Boost.Threads</b> library has several goals that should help
|
||||
to set it apart from other solutions. These goals are listed in order
|
||||
of precedence with full descriptions below.</p>
|
||||
|
||||
<ul>
|
||||
<li>
|
||||
<b>Portability</b>
|
||||
|
||||
<p><b>Boost.Threads</b> was designed to be highly portable. The
|
||||
goal is for the interface to be easily implemented on any
|
||||
platform that supports threads, and possibly even on platforms
|
||||
without native thread support.</p>
|
||||
</li>
|
||||
|
||||
<li>
|
||||
<b>Safety</b>
|
||||
|
||||
<p><b>Boost.Threads</b> was designed to be as safe as possible.
|
||||
Writing <a href="definitions.html#Thread-safe">thread-safe</a>
|
||||
code is very difficult and successful libraries must strive to
|
||||
insulate the programmer from dangerous constructs as much as
|
||||
possible. This is accomplished in several ways:</p>
|
||||
|
||||
<ul>
|
||||
<li>
|
||||
<p align="left">C++ language features are used make
|
||||
correct usage easy (if possible, the default) and
|
||||
error-prone impossible or at least more difficult. For
|
||||
example, see the <a href="mutex_concept.html">Mutex</a>
|
||||
and <a href="lock_concept.html">Lock</a> designs, and
|
||||
how note how they interact.</p>
|
||||
</li>
|
||||
|
||||
<li>
|
||||
<p align="left">Certain traditional concurrent
|
||||
programming features are considered so error-prone that
|
||||
they are not provided at all. For example, see the <a
|
||||
href="rationale.html#Events">Events Not Provided</a>
|
||||
rationale.</p>
|
||||
</li>
|
||||
|
||||
<li>
|
||||
<p align="left">Dangerous features, or features which
|
||||
may be misused, are identified as such in the
|
||||
documentation to make users aware of potential
|
||||
pitfalls.</p>
|
||||
</li>
|
||||
</ul>
|
||||
</li>
|
||||
|
||||
<li>
|
||||
<b>Flexibility</b>
|
||||
|
||||
<p><b>Boost.Threads</b> was designed to be flexible. This goal
|
||||
is often at odds with <i>safety</i>. When functionality might
|
||||
be compromised by the desire to keep the interface safe, <b>
|
||||
Boost.Threads</b> has been designed to provide the
|
||||
functionality, but to make it's use prohibitive for general
|
||||
use.</p>
|
||||
</li>
|
||||
|
||||
<li>
|
||||
<b>Efficiency</b>
|
||||
|
||||
<p><b>Boost.Threads</b> was designed to be as efficient as
|
||||
possible. When building a library on top of another library
|
||||
there is always a danger that the result will be so much slower
|
||||
than the "native" API that programmers are inclined
|
||||
to ignore the higher level API. <b>Boost.Threads</b> was
|
||||
designed to minimize the chances of this occurring. The
|
||||
interfaces have been crafted to allow an implementation the
|
||||
greatest chance of being as efficient as possible. This goal is
|
||||
often at odds with the goal for <i>safety</i>. Every effort was
|
||||
made to ensure efficient implementations, but when in conflict
|
||||
<i>safety</i> has always taken precedence.</p>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<h3>Iterative Phases</h3>
|
||||
|
||||
<p>Another goal of <b>Boost.Threads</b> was to take a dynamic,
|
||||
iterative approach in its development. The computing industry is still
|
||||
exploring the concepts of parallel programming. Most thread libraries
|
||||
supply only simple primitive concepts for thread synchronization. These
|
||||
concepts are very simple, but they are very difficult to use safely or
|
||||
to provide formal proofs for constructs built on top of them. Until
|
||||
recently, these primitives were "state of the art" and the
|
||||
only concepts available to programmers. Recently there has been a lot
|
||||
of research in other concepts, such as in "Communicating
|
||||
Sequential Processes." <b>Boost.Threads</b> was designed in
|
||||
iterative steps, providing the building blocks necessary for the next
|
||||
step, and giving the researcher the tools necessary to explore new
|
||||
concepts in a portable manner.</p>
|
||||
|
||||
<p>Given the goal of following a dynamic, iterative approach <b>
|
||||
Boost.Threads</b> shall go through several growth cycles. Each phase in
|
||||
its development shall be roughly documented here.</p>
|
||||
|
||||
<h4>Phase 1, Synchronization Primitives</h4>
|
||||
|
||||
<p>Boost is all about providing high quality libraries with
|
||||
implementations for many platforms. Unfortunately, there's a big
|
||||
problem faced by developers wishing to supply such high quality
|
||||
libraries, namely thread-safety. The C++ standard doesn't address
|
||||
threads at all, but real world programs often make use of native
|
||||
threading support. A portable library that doesn't address the
|
||||
issue of thread-safety is there for not much help to a programmer who
|
||||
wants to use the library in his multi-threaded application. So
|
||||
there's a very great need for portable primitives that will allow
|
||||
the library developer to create <a href="definitions.html#Thread-safe">
|
||||
thread-safe</a> implementations. This need far out weighs the need for
|
||||
portable methods to create and manage threads.</p>
|
||||
|
||||
<p>Because of this need, the first phase of <b>Boost.Threads</b>
|
||||
focuses solely on providing portable primitive concepts for thread
|
||||
synchronization. Types provided in this phase include the <a href="mutex.html">
|
||||
mutex/try_mutex/timed_mutex</a>, <a href="recursive_mutex.html">
|
||||
recursive_mutex/recursive_try_mutex/recursive_timed_mutex</a>, <a href=
|
||||
"scoped_lock.html">scoped_lock</a>, <a href="scoped_try_lock.html">
|
||||
scoped_try_lock</a>, <a href="scoped_timed_lock.html">
|
||||
scoped_timed_lock</a> and <a href="lock_error.html">lock_error</a>.
|
||||
These are considered the "core" synchronization primitives,
|
||||
though there are others that will be added in later phases.</p>
|
||||
|
||||
<h4>Phase 2, Thread Management and Thread Specific Storage</h4>
|
||||
|
||||
<p>This phase addresses the creation and management of threads and
|
||||
provides a mechanism for thread specific storage (data associated with
|
||||
a thread instance). Thread management is a tricky issue in C++, so this
|
||||
phase addresses only the basic needs of multi-threaded program. Later
|
||||
phases are likely to add additional functionality in this area. This
|
||||
phase of <b>Boost.Threads</b> adds the <a href="thread.html">thread</a>
|
||||
and <a href="thread_specific_ptr.html">thread_specific_ptr</a> types.
|
||||
With these additions the <b>Boost.Threads</b> library can be considered
|
||||
minimal but complete.</p>
|
||||
|
||||
<h4>The Next Phase</h4>
|
||||
|
||||
<p>The next phase will address more advanced synchronization concepts,
|
||||
such as read/write mutexes and barriers.</p>
|
||||
<hr>
|
||||
|
||||
<p>Revised
|
||||
<!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan -->05 November, 2001<!--webbot bot="Timestamp" endspan i-checksum="39359" --></p>
|
||||
|
||||
<p><i>© Copyright <a href="mailto:williamkempf@hotmail.com">
|
||||
William E. Kempf</a> 2001 all rights reserved.</i></p>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -1,300 +0,0 @@
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content=
|
||||
"text/html; charset=iso-8859-1">
|
||||
<meta name="keywords" content="threads, BTL, thread library, C++">
|
||||
|
||||
<title>Boost.Threads, Lock Concept</title>
|
||||
</head>
|
||||
|
||||
<body bgcolor="#FFFFFF" link="#0000FF" vlink="#800080">
|
||||
<table summary="header" border="0" cellpadding="7" cellspacing="0"
|
||||
width="100%">
|
||||
<tr>
|
||||
<td valign="top" width="300">
|
||||
<h3><img src="../../../c++boost.gif" alt="C++ Boost" width=
|
||||
"277" height="86"></h3>
|
||||
</td>
|
||||
|
||||
<td valign="top">
|
||||
<h1 align="center">Boost.Threads</h1>
|
||||
|
||||
<h2 align="center">Lock Concepts</h2>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<hr>
|
||||
|
||||
<p><a href="#Introduction">Introduction</a><br>
|
||||
<a href="#Requirements">Concept Requirements</a><br>
|
||||
<a href="#Lock">Lock Concept</a><br>
|
||||
<a href="#ScopedLock">ScopedLock Concept</a><br>
|
||||
<a href="#ScopedTryLock">ScopedTryLock Concept</a><br>
|
||||
<a href="#ScopedTimedLock">ScopedTimedLock Concept</a><br>
|
||||
<a href="#Models">Models</a></p>
|
||||
|
||||
<h2><a name="Introduction">Introduction</a></h2>
|
||||
|
||||
<p>The lock concepts provide exception safe means for locking and
|
||||
unlocking a <a href="mutex_concept.html">mutex model</a>. In other
|
||||
words they are an implementation of the <i>Scoped Locking</i> <a href=
|
||||
"bibliography.html#Schmidt 00">[Schmidt 00]</a> pattern. The <a href=
|
||||
"#ScopedLock">ScopedLock</a> concept, with <a href="#ScopedTryLock">
|
||||
ScopedTryLock</a> and <a href="#ScopedTimedLock">ScopedTimedLock</a>
|
||||
refinements, formalize the requirements.</p>
|
||||
|
||||
<p>Lock models are constructed with a reference to a <a href=
|
||||
"mutex_concept.html">mutex model</a> and typically acquire ownership of
|
||||
the <a href="mutex_concept.html">mutex model</a> by setting its state
|
||||
to locked. They also ensure ownership is relinquished in the
|
||||
destructor. Lock models also expose functions to query the lock status
|
||||
and to manually lock and unlock the <a href="mutex_concept.html">mutex
|
||||
model</a>.</p>
|
||||
|
||||
<p>Instances of lock models are meant to be short lived, expected to be
|
||||
used at block scope only. The lock models are not <a href=
|
||||
"definitions.html#Thread-safe">thread-safe</a>. Lock models must
|
||||
maintain state to indicate whether or not they've been locked and
|
||||
this state is not protected by any synchronization concepts. For this
|
||||
reason an instance of a lock model should never be shared between
|
||||
multiple threads.</p>
|
||||
|
||||
<h2>Concept <a name="Requirements">Requirements</a></h2>
|
||||
|
||||
<p>[For documentation purposes, portions of the concept requirements
|
||||
are repeated in the documentation for specific lock classes. Those
|
||||
copies need to be kept in sync with the requirements here.]</p>
|
||||
|
||||
<h3><a name="Lock">Lock</a> Concept</h3>
|
||||
|
||||
<p>For a <a href="#ScopedLock">ScopedLock</a>, <a href=
|
||||
"#ScopedTryLock">ScopedTryLock</a>, or <a href="#ScopedTimedLock">
|
||||
ScopedTimedLock</a> type <code>L</code> and an object <code>lk</code>
|
||||
and const object <code>clk</code> of that type, the following
|
||||
expressions must be well-formed and have the indicated effects.</p>
|
||||
|
||||
<p>The Lock concept is used as a base for the <a href="#ScopedLock">
|
||||
ScopedLock</a>, <a href="#ScopedTryLock">ScopedTryLock</a>, and <a
|
||||
href="#ScopedTimedLock">ScopedTimedLock</a> refinements. The associated
|
||||
mutex type is as specified for each of those refinements
|
||||
respectively.</p>
|
||||
|
||||
<table summary="Lock expressions" border="1" cellpadding="5">
|
||||
<tr>
|
||||
<td><b>Expression</b></td>
|
||||
|
||||
<td><b>Effects</b></td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td valign="top"><code>(&lk)->~L();</code></td>
|
||||
|
||||
<td><code>if (locked()) unlock();</code></td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td valign="top"><code>(&clk)->operator const
|
||||
void*()</code></td>
|
||||
|
||||
<td>Returns type void*, non-zero if if the associated mutex has
|
||||
been locked by <code>clk</code>, otherwise 0.</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td valign="top"><code>clk.locked()</code></td>
|
||||
|
||||
<td>Returns a <code>bool</code>, <code>(&clk)->operator
|
||||
const void*() != 0</code></td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td valign="top"><code>lk.lock()</code></td>
|
||||
|
||||
<td>Throws lock_error if locked(). If the associated mutex is
|
||||
already locked by some other thread, places the current thread
|
||||
in the <a href="definitions.html#State">Blocked</a> state until
|
||||
the associated mutex is unlocked, after which the current
|
||||
thread is placed in the <a href="definitions.html#State">
|
||||
Ready</a> state, eventually to be returned to the <a href=
|
||||
"definitions.html#State">Running</a> state.<br>
|
||||
Postcondition: locked()</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td valign="top"><code>lk.unlock()</code></td>
|
||||
|
||||
<td>If !locked(), throws lock_error, otherwise unlocks the
|
||||
associated mutex.<br>
|
||||
Postcondition: !locked()</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<h3><a name="ScopedLock">ScopedLock</a> Concept</h3>
|
||||
|
||||
<p>A ScopedLock must meet the <a href="#Lock">Lock</a> requirements.
|
||||
For a ScopedLock type <code>L</code> and an object <code>lk</code> of
|
||||
that type, and an object <code>m</code> of a type meeting the <a href=
|
||||
"mutex_concept.html#Mutex">Mutex</a> requirements, and an object <code>
|
||||
b</code> of type <code>bool</code>, the following expressions must be
|
||||
well-formed and have the indicated effects.</p>
|
||||
|
||||
<table summary="ScopedLock expressions" border="1" cellpadding="5">
|
||||
<tr>
|
||||
<td><b>Expression</b></td>
|
||||
|
||||
<td><b>Effects</b></td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td valign="top"><code>L lk(m);</code></td>
|
||||
|
||||
<td>Constructs an object <code>lk</code>, and associates mutex
|
||||
<code>m</code> with it, then calls <code>lock()</code></td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td valign="top"><code>L lk(m,b);</code></td>
|
||||
|
||||
<td>Constructs an object <code>lk</code>, and associates mutex
|
||||
<code>m</code> with it, then if <code>b</code>, calls <code>
|
||||
lock()</code></td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<h3><a name="ScopedTryLock">ScopedTryLock</a> Concept</h3>
|
||||
|
||||
<p>A ScopedTryLock must meet the <a href="#Lock">Lock</a> requirements.
|
||||
For a ScopedTryLock type <code>L</code> and an object <code>lk</code>
|
||||
of that type, and an object <code>m</code> of a type meeting the <a
|
||||
href="mutex_concept.html#TryMutex">TryMutex</a> requirements, and an
|
||||
object <code>b</code> of type <code>bool</code>, the following
|
||||
expressions must be well-formed and have the indicated effects.</p>
|
||||
|
||||
<table summary="ScopedTryLock expressions" border="1" cellpadding="5">
|
||||
<tr>
|
||||
<td><b>Expression</b></td>
|
||||
|
||||
<td><b>Effects</b></td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td valign="top"><code>L lk(m);</code></td>
|
||||
|
||||
<td>Constructs an object <code>lk</code>, and associates mutex
|
||||
<code>m</code> with it, then calls <code>try_lock()</code></td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td valign="top"><code>L lk(m,b);</code></td>
|
||||
|
||||
<td>Constructs an object <code>lk</code>, and associates mutex
|
||||
<code>m</code> with it, then if <code>b</code>, calls <code>
|
||||
lock()</code></td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td valign="top"><code>lk.try_lock()</code></td>
|
||||
|
||||
<td>If locked(), throws <code>lock_error</code>. Makes a
|
||||
non-blocking attempt to lock the associated mutex, returning
|
||||
<code>true</code> if the lock attempt is successful, otherwise
|
||||
<code>false</code>.</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<h3><a name="ScopedTimedLock">ScopedTimedLock</a> Concept</h3>
|
||||
|
||||
<p>A ScopedTimedLock must meet the <a href="#Lock">Lock</a>
|
||||
requirements. For a ScopedTimedLock type <code>L</code> and an object
|
||||
<code>lk</code> of that type, and an object <code>m</code> of a type
|
||||
meeting the <a href="mutex_concept.html#TimedMutex">TimedMutex</a>
|
||||
requirements, and an object <code>b</code> of type <code>bool</code>,
|
||||
and an object <code>t</code> of type <code><a href="xtime.html">
|
||||
xtime</a></code>, the following expressions must be well-formed and
|
||||
have the indicated effects.</p>
|
||||
|
||||
<table summary="ScopedTimedLock expressions" border="1" cellpadding=
|
||||
"5">
|
||||
<tr>
|
||||
<td><b>Expression</b></td>
|
||||
|
||||
<td><b>Effects</b></td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td valign="top"><code>L lk(m,t);</code></td>
|
||||
|
||||
<td>Constructs an object <code>lk</code>, and associates mutex
|
||||
<code>m</code> with it, then calls <code>
|
||||
timed_lock(t)</code></td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td valign="top"><code>L lk(m,b);</code></td>
|
||||
|
||||
<td>Constructs an object <code>lk</code>, and associates mutex
|
||||
<code>m</code> with it, then if <code>b</code>, calls <code>
|
||||
lock()</code></td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td valign="top"><code>lk.timed_lock(t)</code></td>
|
||||
|
||||
<td>If locked(), throws lock_error. Makes a blocking attempt to
|
||||
lock the associated mutex, and returns <code>true</code> if
|
||||
successful within the specified time <code>t</code>, otherwise
|
||||
<code>false</code>.</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<h2><a name="Models">Models</a></h2>
|
||||
|
||||
<p><b>Boost.Threads</b> currently supplies three classes which model
|
||||
lock concepts.</p>
|
||||
|
||||
<p>These classes are normally accessed via typedefs of the same name
|
||||
supplied by a <a href="mutex_concept.html">mutex model</a>.</p>
|
||||
|
||||
<table summary="Lock concept classes" border="1" cellpadding="5">
|
||||
<tr>
|
||||
<td><b>Concept</b></td>
|
||||
|
||||
<td><b>Refines</b></td>
|
||||
|
||||
<td><b>Classes Modeling the Concept</b></td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td><a href="#ScopedLock">ScopedLock</a></td>
|
||||
|
||||
<td> </td>
|
||||
|
||||
<td><a href="scoped_lock.html">scoped_lock</a></td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td><a href="#ScopedTryLock">ScopedTryLock</a></td>
|
||||
|
||||
<td><a href="#ScopedLock">ScopedLock</a></td>
|
||||
|
||||
<td><a href="scoped_try_lock.html">scoped_try_lock</a> </td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td><a href="#ScopedTimedLock">ScopedTimedLock</a></td>
|
||||
|
||||
<td><a href="#ScopedLock">ScopedLock</a></td>
|
||||
|
||||
<td><a href="scoped_timed_lock.html">scoped_timed_lock</a></td>
|
||||
</tr>
|
||||
</table>
|
||||
<hr>
|
||||
|
||||
<p>Revised
|
||||
<!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan -->05 November, 2001<!--webbot bot="Timestamp" endspan i-checksum="39359" --></p>
|
||||
|
||||
<p><i>© Copyright <a href="mailto:williamkempf@hotmail.com">
|
||||
William E. Kempf</a> 2001 all rights reserved.</i></p>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -1,110 +0,0 @@
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content=
|
||||
"text/html; charset=iso-8859-1">
|
||||
<meta name="keywords" content="threads, BTL, thread library, C++">
|
||||
<link rel="stylesheet" type="text/css" href="styles.css">
|
||||
|
||||
<title>Boost.Threads, lock_error</title>
|
||||
</head>
|
||||
|
||||
<body bgcolor="#FFFFFF" link="#0000FF" vlink="#800080">
|
||||
<table summary="header" border="0" cellpadding="7" cellspacing="0"
|
||||
width="100%">
|
||||
<tr>
|
||||
<td valign="top" width="300">
|
||||
<h3><img src="../../../c++boost.gif" alt="C++ Boost" width=
|
||||
"277" height="86"></h3>
|
||||
</td>
|
||||
|
||||
<td valign="top">
|
||||
<h1 align="center">Boost.Threads</h1>
|
||||
|
||||
<h2 align="center">lock_error</h2>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<hr>
|
||||
|
||||
<p><a href="#Introduction">Introduction</a><br>
|
||||
<a href="#Header">Header</a><br>
|
||||
<a href="#Synopsis">Synopsis</a><br>
|
||||
<a href="#Members">Members</a><br>
|
||||
<a href="#Example">Example</a></p>
|
||||
|
||||
<h2><a name="Introduction">Introduction</a></h2>
|
||||
|
||||
<p>The <tt>lock_error</tt> class defines an exception type thrown to
|
||||
indicate a locking related error has been detected. Examples of such
|
||||
errors include a lock operation which can be determined to result in a
|
||||
deadlock, or unlock operations attempted by a thread that does not own
|
||||
the lock.</p>
|
||||
|
||||
<h2><a name="Header">Header</a></h2>
|
||||
<pre>
|
||||
#include <a href=
|
||||
"../../../boost/thread/thread.hpp"><boost/thread/thread.hpp></a>
|
||||
</pre>
|
||||
|
||||
<h2><a name="Synopsis">Synopsis</a></h2>
|
||||
<pre>
|
||||
namespace boost
|
||||
|
||||
class lock_error : public std::runtime_error
|
||||
{
|
||||
public:
|
||||
lock_error();
|
||||
};
|
||||
}
|
||||
</pre>
|
||||
|
||||
<h2><a name="Members">Members</a></h2>
|
||||
<hr>
|
||||
|
||||
<h3>Constructor</h3>
|
||||
<pre>
|
||||
lock_error();
|
||||
</pre>
|
||||
|
||||
<p>Constructs a <tt>lock_error</tt> object.</p>
|
||||
<hr>
|
||||
|
||||
<h2><a name="Example">Example</a> Usage</h2>
|
||||
<pre>
|
||||
#include <a href=
|
||||
"../../../boost/thread/mutex.hpp"><boost/thread/mutex.hpp></a>
|
||||
#include <a href=
|
||||
"../../../boost/thread/thread.hpp"><boost/thread/thread.hpp></a>
|
||||
#include <iostream>
|
||||
|
||||
int main(int, char*[])
|
||||
{
|
||||
boost::mutex mutex;
|
||||
boost::mutex::scoped_lock scoped_lock(mutex);
|
||||
try
|
||||
{
|
||||
boost::mutex::scoped_lock deadlock(mutex);
|
||||
std::cout << "lock succeeded" << std::endl;
|
||||
}
|
||||
catch (boost::lock_error& err)
|
||||
{
|
||||
std::cout << err.what() << " - deadlock occurred." << std::endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
</pre>
|
||||
|
||||
<p>The output is:</p>
|
||||
<pre>
|
||||
thread lock error - deadlock occurred.
|
||||
</pre>
|
||||
<hr>
|
||||
|
||||
<p>Revised
|
||||
<!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan -->05 November, 2001<!--webbot bot="Timestamp" endspan i-checksum="39359" --></p>
|
||||
|
||||
<p><i>© Copyright <a href="mailto:williamkempf@hotmail.com">
|
||||
William E. Kempf</a> 2001 all rights reserved.</i></p>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
359
doc/mutex.html
359
doc/mutex.html
@@ -1,359 +0,0 @@
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content=
|
||||
"text/html; charset=iso-8859-1">
|
||||
<meta name="keywords" content="threads, BTL, thread library, C++">
|
||||
<link rel="stylesheet" type="text/css" href="styles.css">
|
||||
|
||||
<title>Boost.Threads, mutex</title>
|
||||
</head>
|
||||
|
||||
<body bgcolor="#ffffff" link="#0000ff" vlink="#800080">
|
||||
<table summary="header" border="0" cellpadding="7" cellspacing="0"
|
||||
width="100%">
|
||||
<tr>
|
||||
<td valign="top" width="300">
|
||||
<h3><img alt="C++ Boost" src="../../../c++boost.gif" width=
|
||||
"277" height="86"></h3>
|
||||
</td>
|
||||
|
||||
<td valign="top">
|
||||
<h1 align="center">Boost.Threads</h1>
|
||||
|
||||
<h2 align="center">mutex<br>
|
||||
try_mutex<br>
|
||||
timed_mutex</h2>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<hr>
|
||||
|
||||
<p><a href="#Introduction">Introduction</a><br>
|
||||
<a href="#Header">Header</a><br>
|
||||
<a href="#mutex Synopsis">Class mutex Synopsis</a><br>
|
||||
<a href="#mutex Members">Class mutex Members</a><br>
|
||||
<a href="#try_mutex Synopsis">Class try_mutex Synopsis</a><br>
|
||||
<a href="#try_mutex Members">Class try_mutex Members</a><br>
|
||||
<a href="#timed_mutex Synopsis">Class timed_mutex Synopsis</a><br>
|
||||
<a href="#timed_mutex Members">Class timed_mutex Members</a><br>
|
||||
<a href="#Example">Example</a></p>
|
||||
|
||||
<h2><a name="Introduction">Introduction</a></h2>
|
||||
|
||||
<p>The <tt><a href="#mutex Synopsis">mutex</a></tt>, <tt><a href=
|
||||
"#try_mutex Synopsis">try_mutex</a></tt> and <tt><a href=
|
||||
"#timed_mutex Synopsis">timed_mutex</a></tt> classes define full
|
||||
featured models of the <a href="mutex_concept.html#Mutex">Mutex</a>, <a
|
||||
href="mutex_concept.html#TryMutex">TryMutex</a>, and <a href=
|
||||
"mutex_concept.html#TimedMutex">TimedMutex</a> concepts. These types
|
||||
should be used to non-recursively synchronize access to shared
|
||||
resources. For recursive locking mechanics, see <a href=
|
||||
"recursive_mutex.html">recursive mutexes</a>.</p>
|
||||
|
||||
<p>Each class supplies one or more typedefs for lock types which model
|
||||
matching lock concepts. For the best possible performance you should
|
||||
use the mutex class that supports the minimum set of lock types that
|
||||
you need.</p>
|
||||
|
||||
<table summary="lock types" border="1" cellpadding="5">
|
||||
<tr>
|
||||
<td><b>Mutex Class</b></td>
|
||||
|
||||
<td><b>Lock name</b></td>
|
||||
|
||||
<td><b>Implementation defined Lock Type</b></td>
|
||||
|
||||
<td><b>Lock Concept</b></td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td valign="top"><a href="#mutex Synopsis"><code>
|
||||
mutex</code></a></td>
|
||||
|
||||
<td valign="middle"><code>scoped_lock</code></td>
|
||||
|
||||
<td valign="middle"><code><a href="scoped_lock.html">
|
||||
boost::</a></code><a href=
|
||||
"scoped_lock.html"><code>detail::thread::scoped_lock<mutex></code></a></td>
|
||||
|
||||
<td valign="middle"><a href="lock_concept.html#ScopedLock">
|
||||
ScopedLock</a></td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td valign="top"><tt><a href="#try_mutex Synopsis">
|
||||
try_mutex</a></tt> </td>
|
||||
|
||||
<td valign="middle"><code>scoped_lock<br>
|
||||
scoped_try_lock</code></td>
|
||||
|
||||
<td valign="middle"><code><a href="scoped_lock.html">
|
||||
boost::</a></code><a href=
|
||||
"scoped_lock.html"><code>detail::thread::scoped_lock<try_mutex><br>
|
||||
|
||||
</code></a> <code><a href="scoped_try_lock.html">
|
||||
boost::detail::thread::scoped_try_lock<try_mutex></a></code></td>
|
||||
|
||||
<td valign="middle"><a href="lock_concept.html#ScopedLock">
|
||||
ScopedLock</a><br>
|
||||
<a href="lock_concept.html#ScopedTryLock">
|
||||
ScopedTryLock</a></td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td valign="top"><code><a href="#timed_mutex Synopsis">
|
||||
timed_mutex</a></code> </td>
|
||||
|
||||
<td valign="middle"><code>scoped_lock<br>
|
||||
scoped_try_lock<br>
|
||||
scoped_timed_lock</code></td>
|
||||
|
||||
<td valign="middle"><code><a href="scoped_lock.html">
|
||||
boost::</a></code><a href=
|
||||
"scoped_lock.html"><code>detail::thread::scoped_lock<timed_mutex></code></a><br>
|
||||
|
||||
<code><a href="scoped_try_lock.html">boost::</a></code><a
|
||||
href=
|
||||
"scoped_try_lock.html"><code>detail::thread::scoped_try_lock<timed_mutex></code></a><br>
|
||||
|
||||
<code><a href="scoped_timed_lock.html">boost::</a></code><a
|
||||
href=
|
||||
"scoped_timed_lock.html"><code>detail::thread::scoped_timed_lock<timed_mutex></code></a></td>
|
||||
|
||||
<td valign="middle"><a href="lock_concept.html#ScopedLock">
|
||||
ScopedLock</a><br>
|
||||
<a href="lock_concept.html#ScopedTryLock">
|
||||
ScopedTryLock</a><br>
|
||||
<a href="lock_concept.html#ScopedTimedLock">
|
||||
ScopedTimedLock</a></td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<p>The <tt>mutex</tt>, <tt>try_mutex</tt> and <tt>timed_mutex</tt>
|
||||
classes use an <tt>Unspecified</tt> <a href=
|
||||
"mutex_concept.html#LockingStrategies">locking strategy</a>, so
|
||||
attempts to recursively lock them or attempts to unlock them by threads
|
||||
that don't own a lock on them result in <b>undefined behavior</b>.
|
||||
This strategy allows implementations to be as efficient as possible on
|
||||
any given platform. It is, however, recommended that implementations
|
||||
include debugging support to detect misuse when <tt>NDEBUG</tt> is not
|
||||
defined.</p>
|
||||
|
||||
<p>Like all the <b>Boost.Threads</b> <a href="mutex_concept.html">mutex
|
||||
models</a>, the <tt>mutex</tt>, <tt>try_mutex</tt> and <tt>
|
||||
timed_mutex</tt> leave the <a href=
|
||||
"mutex_concept.html#SchedulingPolicies">scheduling policy</a> as <tt>
|
||||
Unspecified</tt>. Programmers should assume that threads waiting for a
|
||||
lock on objects of these types acquire the lock in a random order, even
|
||||
though the specific behavior for a given platform may be different.</p>
|
||||
|
||||
<h2><a name="Header">Header</a></h2>
|
||||
<pre>
|
||||
#include <a href=
|
||||
"../../../boost/thread/mutex.hpp"><boost/thread/mutex.hpp></a>
|
||||
</pre>
|
||||
|
||||
<h2>Class <a name="mutex Synopsis">mutex Synopsis</a></h2>
|
||||
<pre>
|
||||
namespace boost
|
||||
{
|
||||
class mutex : private <a href=
|
||||
"../../utility/utility.htm">boost::noncopyable</a> // Exposition only.
|
||||
// Class mutex meets the <a href=
|
||||
"overview.html#NonCopyable">NonCopyable</a> requirement.
|
||||
{
|
||||
public:
|
||||
typedef <i>[implementation defined; see <a href=
|
||||
"#Introduction">Introduction</a>]</i> scoped_lock;
|
||||
|
||||
mutex();
|
||||
~mutex();
|
||||
};
|
||||
}
|
||||
</pre>
|
||||
|
||||
<h2>Class <a name="mutex Members">mutex Members</a></h2>
|
||||
<hr>
|
||||
|
||||
<h3>Constructor</h3>
|
||||
<pre>
|
||||
mutex();
|
||||
</pre>
|
||||
|
||||
<p><b>Postconditions:</b> <code>*this</code> is in the unlocked
|
||||
state.</p>
|
||||
<hr>
|
||||
|
||||
<h3>Destructor</h3>
|
||||
<pre>
|
||||
~mutex();
|
||||
</pre>
|
||||
|
||||
<p><b>Requires:</b> <code>*this</code> is in the unlocked state.</p>
|
||||
|
||||
<p><b>Effects:</b> Destroys <code>*this</code>.</p>
|
||||
|
||||
<p><b>Dangers:</b> Destruction of a locked mutex is a serious
|
||||
programming error resulting in undefined behavior such as a program
|
||||
crash.</p>
|
||||
<hr>
|
||||
|
||||
<h2>Class <a name="try_mutex Synopsis">try_mutex Synopsis</a></h2>
|
||||
<pre>
|
||||
namespace boost
|
||||
{
|
||||
class try_mutex : private boost::noncopyable // Exposition only.
|
||||
// Class try_mutex meets the <a href=
|
||||
"overview.html#NonCopyable">NonCopyable</a> requirement.
|
||||
{
|
||||
public:
|
||||
typedef <i>[implementation defined; see <a href=
|
||||
"#Introduction">Introduction</a>]</i> scoped_lock;
|
||||
typedef <i>[implementation defined; see <a href=
|
||||
"#Introduction">Introduction</a>]</i> scoped_try_lock;
|
||||
|
||||
try_mutex();
|
||||
~try_mutex();
|
||||
};
|
||||
}
|
||||
</pre>
|
||||
|
||||
<h2>Class <a name="try_mutex Members">try_mutex Members</a></h2>
|
||||
<hr>
|
||||
|
||||
<h3>Constructor</h3>
|
||||
<pre>
|
||||
try_mutex();
|
||||
</pre>
|
||||
|
||||
<p><b>Postconditions:</b> <code>*this</code> is in the unlocked
|
||||
state.</p>
|
||||
<hr>
|
||||
|
||||
<h3>Destructor</h3>
|
||||
<pre>
|
||||
~try_mutex();
|
||||
</pre>
|
||||
|
||||
<p><b>Requires:</b> <code>*this</code> is in the unlocked state.</p>
|
||||
|
||||
<p><b>Effects:</b> Destroys <code>*this</code>.</p>
|
||||
|
||||
<p><b>Dangers:</b> Destruction of a locked mutex is a serious
|
||||
programming error resulting in undefined behavior such as a program
|
||||
crash.</p>
|
||||
<hr>
|
||||
|
||||
<h2>Class <a name="timed_mutex Synopsis">timed_mutex Synopsis</a></h2>
|
||||
<pre>
|
||||
namespace boost
|
||||
{
|
||||
class timed_mutex : private boost::noncopyable // Exposition only.
|
||||
// Class timed_mutex meets the <a href=
|
||||
"overview.html#NonCopyable">NonCopyable</a> requirement.
|
||||
{
|
||||
public:
|
||||
typedef <i>[implementation defined; see <a href=
|
||||
"#Introduction">Introduction</a>]</i> scoped_lock;
|
||||
typedef <i>[implementation defined; see <a href=
|
||||
"#Introduction">Introduction</a>]</i> scoped_try_lock;
|
||||
typedef <i>[implementation defined; see <a href=
|
||||
"#Introduction">Introduction</a>]</i> scoped_timed_lock;
|
||||
|
||||
timed_mutex();
|
||||
~timed_mutex();
|
||||
};
|
||||
}
|
||||
</pre>
|
||||
|
||||
<h2>Class <a name="timed_mutex Members">timed_mutex Members</a></h2>
|
||||
<hr>
|
||||
|
||||
<h3>Constructor</h3>
|
||||
<pre>
|
||||
timed_mutex();
|
||||
</pre>
|
||||
|
||||
<p><b>Postconditions:</b> <code>*this</code> is in the unlocked
|
||||
state.</p>
|
||||
<hr>
|
||||
|
||||
<h3>Destructor</h3>
|
||||
<pre>
|
||||
~timed_mutex();
|
||||
</pre>
|
||||
|
||||
<p><b>Requires:</b> <code>*this</code> is in the unlocked state.</p>
|
||||
|
||||
<p><b>Effects:</b> Destroys <code>*this</code>.</p>
|
||||
|
||||
<p><b>Dangers:</b> Destruction of a locked mutex is a serious
|
||||
programming error resulting in undefined behavior such as a program
|
||||
crash.</p>
|
||||
<hr>
|
||||
|
||||
<h2><a name="Example">Example</a> Usage</h2>
|
||||
<pre>
|
||||
#include <a href=
|
||||
"../../../boost/thread/mutex.hpp"><boost/thread/mutex.hpp></a>
|
||||
#include <a href=
|
||||
"../../../boost/thread/thread.hpp"><boost/thread/thread.hpp></a>
|
||||
#include <iostream>
|
||||
|
||||
boost::mutex io_mutex; // The iostreams are not guaranteed to be <a href=
|
||||
"definitions.html#Thread-safe">thread-safe</a>!
|
||||
|
||||
class counter
|
||||
{
|
||||
public:
|
||||
counter() : count(0) { }
|
||||
|
||||
int increment() {
|
||||
boost::mutex::scoped_lock scoped_lock(mutex);
|
||||
return ++count;
|
||||
}
|
||||
|
||||
private:
|
||||
boost::mutex mutex;
|
||||
int count;
|
||||
};
|
||||
|
||||
counter c;
|
||||
|
||||
void change_count(void*)
|
||||
{
|
||||
int i = c.increment();
|
||||
boost::mutex::scoped_lock scoped_lock(io_mutex);
|
||||
std::cout << "count == " << i << std::endl;
|
||||
}
|
||||
|
||||
int main(int, char*[])
|
||||
{
|
||||
const int num_threads = 4;
|
||||
boost::thread_group thrds;
|
||||
for (int i=0; i < num_threads; ++i)
|
||||
thrds.create_thread(&change_count, 0);
|
||||
|
||||
thrds.join_all();
|
||||
|
||||
return 0;
|
||||
}
|
||||
</pre>
|
||||
|
||||
<p>The output is:</p>
|
||||
<pre>
|
||||
count == 1
|
||||
count == 2
|
||||
count == 3
|
||||
count == 4
|
||||
</pre>
|
||||
<hr>
|
||||
|
||||
<p>Revised
|
||||
<!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan -->05 November, 2001<!--webbot bot="Timestamp" endspan i-checksum="39359" --></p>
|
||||
|
||||
<p><i>© Copyright <a href="mailto:williamkempf@hotmail.com">
|
||||
William E. Kempf</a> 2001 all rights reserved.</i></p>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -1,337 +0,0 @@
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content=
|
||||
"text/html; charset=iso-8859-1">
|
||||
<meta name="keywords" content="threads, BTL, thread library, C++">
|
||||
|
||||
<title>Boost.Threads, Mutex Concept</title>
|
||||
</head>
|
||||
|
||||
<body bgcolor="#ffffff" link="#0000ff" vlink="#800080">
|
||||
<table summary="header" border="0" cellpadding="7" cellspacing="0"
|
||||
width="100%">
|
||||
<tr>
|
||||
<td valign="top" width="300">
|
||||
<h3><img height="86" alt="C++ Boost" src=
|
||||
"../../../c++boost.gif" width="277"></h3>
|
||||
</td>
|
||||
|
||||
<td valign="top">
|
||||
<h1 align="center">Boost.Threads</h1>
|
||||
|
||||
<h2 align="center">Mutex Concepts</h2>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<hr>
|
||||
|
||||
<p><a href="#Introduction">Introduction</a><br>
|
||||
<a href="#LockingStrategies">Locking Strategies</a><br>
|
||||
<a href="#Recursive">Recursive</a><br>
|
||||
<a href="#CheckedStrategy">Checked</a><br>
|
||||
<a href="#UncheckedStrategy">Unchecked</a><br>
|
||||
<a href="#UnspecifiedStrategy">
|
||||
Unspecified</a><br>
|
||||
<a href="#SchedulingPolicies">Scheduling Policies</a><br>
|
||||
<a href="#FIFO">FIFO</a><br>
|
||||
<a href="#Priority Driven">Priority
|
||||
Driven</a><br>
|
||||
<a href="#UndefinedScheduling">
|
||||
Undefined</a><br>
|
||||
<a href="#UnspecifiedScheduling">
|
||||
Unspecified</a><br>
|
||||
<a href="#Requirements">Concept Requirements</a><br>
|
||||
<a href="#Mutex">Mutex Concept</a><br>
|
||||
<a href="#TryMutex">TryMutex Concept</a><br>
|
||||
<a href="#TimedMutex">TimedMutex
|
||||
Concept</a><br>
|
||||
<a href="#Models">Models</a></p>
|
||||
|
||||
<h2><a name="Introduction">Introduction</a></h2>
|
||||
|
||||
<p>A mutex (short for mutual-exclusion) concept serializes access to a
|
||||
resource shared between multiple threads. The <a href="#Mutex">
|
||||
Mutex</a> concept, with <a href="#TryMutex">TryMutex</a> and <a href=
|
||||
"#TimedMutex">TimedMutex</a> refinements, formalize the requirements. A
|
||||
model that implements Mutex and its refinements has two states: <b>
|
||||
locked</b> and <b>unlocked</b>. Before using a shared resource, a
|
||||
thread locks a Boost.Threads mutex model object, insuring <a href=
|
||||
"definitions.html#Thread-safe">thread-safe</a> access to the shared
|
||||
resource. When use of the shared resource is complete, the thread
|
||||
unlocks the mutex model object, allowing another thread to acquire the
|
||||
lock and use the shared resource.</p>
|
||||
|
||||
<p>Traditional C thread APIs, like Pthreads or the Windows thread APIs,
|
||||
expose functions to lock and unlock a mutex model. This is dangerous
|
||||
since it's easy to forget to unlock a locked mutex. When the flow
|
||||
of control is complex, with multiple return points, the likelihood of
|
||||
forgetting to unlock a mutex model would become even greater. When
|
||||
exceptions are thrown, it becomes nearly impossible to ensure that the
|
||||
mutex is unlocked properly when using these traditional API's. The
|
||||
result is <a href="definitions.html#Deadlock">deadlock</a>.</p>
|
||||
|
||||
<p>Many C++ threading libraries use a pattern known as <i>Scoped
|
||||
Locking</i> <a href="bibliography.html#Schmidt 00">[Schmidt 00]</a> to
|
||||
free the programmer from the need to explicitly lock and unlock
|
||||
mutexes. With this pattern, a <a href="lock_concept.html">lock
|
||||
concept</a> is employed where the lock model's constructor locks
|
||||
the associated mutex model and the destructor automatically does the
|
||||
unlocking. The <b>Boost.Threads</b> library takes this pattern to the
|
||||
extreme in that lock concepts are the only way to lock and unlock a
|
||||
mutex model: lock and unlock functions are not exposed by any <b>
|
||||
Boost.Threads</b> mutex models. This helps to ensure safe usage
|
||||
patterns, especially when code throws exceptions.</p>
|
||||
|
||||
<h2><a name="LockingStrategies">Locking Strategies</a></h2>
|
||||
|
||||
<p>Every mutex model follows one of several locking strategies. These
|
||||
strategies define the semantics for the locking operation when the
|
||||
calling thread already owns a lock on the mutex model.</p>
|
||||
|
||||
<h3><a name="Recursive">Recursive</a></h3>
|
||||
|
||||
<p>With a recursive locking strategy when a thread attempts to acquire
|
||||
a lock on the mutex model for which it already owns a lock, the
|
||||
operation is successful. Note the distinction between a thread, which
|
||||
may have multiple locks outstanding on a recursive mutex, and a lock
|
||||
object, which even for a recursive mutex cannot have its lock()
|
||||
function called multiple times without first calling unlock().</p>
|
||||
|
||||
<p>Internally a lock count is maintained and the owning thread must
|
||||
unlock the mutex model the same number of times that it's locked it
|
||||
before the mutex model's state returns to unlocked. Since mutex
|
||||
models in <b>Boost.Threads</b> expose locking functionality only
|
||||
through lock concepts, a thread will always unlock a mutex model the
|
||||
same number of times that it locked it. This helps to eliminate a whole
|
||||
set of errors typically found in traditional C style thread APIs.</p>
|
||||
|
||||
<p>Classes <a href="recursive_mutex.html">recursive_mutex</a>, <a href=
|
||||
"recursive_mutex.html">recursive_try_mutex</a> and <a href=
|
||||
"recursive_mutex.html">recursive_timed_mutex</a> use this locking
|
||||
strategy.</p>
|
||||
|
||||
<h3><a name="CheckedStrategy">Checked</a></h3>
|
||||
|
||||
<p>With a checked locking strategy when a thread attempts to acquire a
|
||||
lock on the mutex model for which the thread already owns a lock, the
|
||||
operation will fail with some sort of error indication. Further,
|
||||
attempts by a thread to unlock a mutex that was not locked by the
|
||||
thread will also return some sort of error indication. In <b>
|
||||
Boost.Threads</b>, an exception of type <a href="lock_error.html">
|
||||
lock_error</a> would be thrown in these cases.</p>
|
||||
|
||||
<p><b>Boost.Threads</b> does not currently provide any mutex models
|
||||
that use this strategy.</p>
|
||||
|
||||
<h3><a name="UncheckedStrategy">Unchecked</a></h3>
|
||||
|
||||
<p>With an unchecked locking strategy when a thread attempts to acquire
|
||||
a lock on the mutex model for which the thread already owns a lock the
|
||||
operation will <a href="definitions.html#Deadlock">deadlock</a>. In
|
||||
general this locking strategy is less safe than a checked or recursive
|
||||
strategy, but it's also a faster strategy and so is employed by
|
||||
many libraries.</p>
|
||||
|
||||
<p><b>Boost.Threads</b> does not currently provide any mutex models
|
||||
that use this strategy.</p>
|
||||
|
||||
<h3><a name="UnspecifiedStrategy">Unspecified</a></h3>
|
||||
|
||||
<p>With an unspecified locking strategy, when a thread attempts to
|
||||
acquire a lock on a mutex model for which the thread already owns a
|
||||
lock the operation results in <b>undefined behavior</b>. When a mutex
|
||||
model has an unspecified locking strategy the programmer must assume
|
||||
that the mutex model instead uses an unchecked strategy.</p>
|
||||
|
||||
<p>In general a mutex model with an unspecified locking strategy is
|
||||
unsafe, and it requires programmer discipline to use the mutex model
|
||||
properly. However, this strategy allows an implementation to be as fast
|
||||
as possible with no restrictions on its implementation. This is
|
||||
especially true for portable implementations that wrap the native
|
||||
threading support of a platform. For this reason, the classes <a href=
|
||||
"mutex.html">mutex</a>, <a href="mutex.html">try_mutex</a> and <a href=
|
||||
"mutex.html">timed_mutex</a> use this locking strategy despite the lack
|
||||
of safety.</p>
|
||||
|
||||
<h2><a name="SchedulingPolicies">Scheduling Policies</a></h2>
|
||||
|
||||
<p>Every mutex model follows one of several scheduling policies. These
|
||||
policies define the semantics when the mutex model is unlocked and
|
||||
there is more than one thread waiting to acquire a lock. In other
|
||||
words, the policy defines which waiting thread shall acquire the
|
||||
lock.</p>
|
||||
|
||||
<h3><a name="FIFO">FIFO</a></h3>
|
||||
|
||||
<p>With a FIFO scheduling policy, threads waiting for the lock will
|
||||
acquire it in a first come first serve order (or First In First Out).
|
||||
This can help prevent a high priority thread from starving lower
|
||||
priority threads that are also waiting on the mutex lock.</p>
|
||||
|
||||
<h3><a name="Priority Driven">Priority Driven</a></h3>
|
||||
|
||||
<p>With a Priority Driven scheduling policy, the thread with the
|
||||
highest priority acquires the lock. Note that this means that
|
||||
low-priority threads may never acquire the lock if the mutex model has
|
||||
high contention and there is always at least one high-priority thread
|
||||
waiting. This is known as thread starvation. When multiple threads of
|
||||
the same priority are waiting on the mutex lock one of the other
|
||||
scheduling priorities will determine which thread shall acquire the
|
||||
lock.</p>
|
||||
|
||||
<h3><a name="UndefinedScheduling">Undefined</a></h3>
|
||||
|
||||
<p>Threads acquire the lock in no particular order. Users should assume
|
||||
that low-priority threads may wait indefinitely, and that threads of
|
||||
the same priority acquire the lock in essentially random order.</p>
|
||||
|
||||
<h3><a name="UnspecifiedScheduling">Unspecified</a></h3>
|
||||
|
||||
<p>The mutex model does not specify which scheduling policy is used.
|
||||
The programmer must assume that an undefined scheduling policy is used.
|
||||
In order to ensure portability, all <b>Boost.Threads</b> mutex models
|
||||
use an unspecified scheduling policy.</p>
|
||||
|
||||
<h2>Concept <a name="Requirements">Requirements</a></h2>
|
||||
|
||||
<h3><a name="Mutex">Mutex</a> Concept</h3>
|
||||
|
||||
<p>A Mutex object has two states: locked and unlocked. Mutex object
|
||||
state can only be determined by an object meeting the <a href=
|
||||
"lock_concept.html#ScopedLock">ScopedLock</a> requirements and
|
||||
constructed for the Mutex object.</p>
|
||||
|
||||
<p>A Mutex is <a href="../../utility/utility.htm#Class noncopyable">
|
||||
noncopyable</a>.</p>
|
||||
|
||||
<p>For a Mutex type M and an object m of that type, the following
|
||||
expressions must be well-formed and have the indicated effects.</p>
|
||||
|
||||
<table summary="Mutex expressions" border="1" cellpadding="5">
|
||||
<tr>
|
||||
<td><b>Expression</b></td>
|
||||
|
||||
<td><b>Effects</b></td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td><code>M m;</code></td>
|
||||
|
||||
<td>Constructs a mutex object m. Post-condition: m is
|
||||
unlocked.</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td><code>(&m)->~M();</code></td>
|
||||
|
||||
<td>Precondition: m is unlocked. Destroys a mutex object
|
||||
m.</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td><code>M::scoped_lock</code></td>
|
||||
|
||||
<td>A type meeting the <a href="lock_concept.html#ScopedLock">
|
||||
ScopedLock</a> requirements.</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<h3><a name="TryMutex">TryMutex</a> Concept</h3>
|
||||
|
||||
<p>A TryMutex must meet the <a href="#Mutex">Mutex</a> requirements. In
|
||||
addition, for a TryMutex type M and an object m of that type, the
|
||||
following expressions must be well-formed and have the indicated
|
||||
effects.</p>
|
||||
|
||||
<table summary="TryMutex expressions" border="1" cellpadding="5">
|
||||
<tr>
|
||||
<td><b>Expression</b></td>
|
||||
|
||||
<td><b>Effects</b></td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td><code>M::scoped_try_lock</code></td>
|
||||
|
||||
<td>A type meeting the <a href=
|
||||
"lock_concept.html#ScopedTryLock">ScopedTryLock</a>
|
||||
requirements.</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<h3><a name="TimedMutex">TimedMutex</a> Concept</h3>
|
||||
|
||||
<p>A TimedMutex must meet the <a href="#TryMutex">TryMutex</a>
|
||||
requirements. In addition, for a TimedMutex type M and an object m of
|
||||
that type, the following expressions must be well-formed and have the
|
||||
indicated effects.</p>
|
||||
|
||||
<table summary="TimedMutex expressions" border="1" cellpadding="5">
|
||||
<tr>
|
||||
<td><b>Expression</b></td>
|
||||
|
||||
<td><b>Effects</b></td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td><code>M::scoped_timed_lock</code></td>
|
||||
|
||||
<td>A type meeting the <a href=
|
||||
"lock_concept.html#ScopedTimedLock">ScopedTimedLock</a>
|
||||
requirements.</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<h2><a name="Models">Models</a></h2>
|
||||
|
||||
<p><b>Boost.Threads</b> currently supplies six classes which model
|
||||
mutex concepts.</p>
|
||||
|
||||
<table summary="Mutex concept classes" border="1" cellpadding="5">
|
||||
<tr>
|
||||
<td><b>Concept</b></td>
|
||||
|
||||
<td><b>Refines</b></td>
|
||||
|
||||
<td><b>Classes Modeling the Concept</b></td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td valign="top"><a href="#Mutex">Mutex</a></td>
|
||||
|
||||
<td valign="top"> </td>
|
||||
|
||||
<td><a href="mutex.html">mutex</a><br>
|
||||
<a href="recursive_mutex.html">recursive_mutex</a></td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td valign="top"><a href="#TryMutex">TryMutex</a></td>
|
||||
|
||||
<td valign="top"><a href="#Mutex">Mutex</a></td>
|
||||
|
||||
<td><a href="mutex.html">try_mutex<br>
|
||||
</a> <a href="recursive_mutex.html">recursive_try_mutex</a>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td valign="top"><a href="#TimedMutex">TimedMutex</a></td>
|
||||
|
||||
<td valign="top"><a href="#TryMutex">TryMutex</a></td>
|
||||
|
||||
<td><a href="mutex.html">timed_mutex<br>
|
||||
</a> <a href="recursive_mutex.html">
|
||||
recursive_timed_mutex</a></td>
|
||||
</tr>
|
||||
</table>
|
||||
<hr>
|
||||
|
||||
<p>Revised
|
||||
<!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan -->05 November, 2001<!--webbot bot="Timestamp" endspan i-checksum="39359" --></p>
|
||||
|
||||
<p><i>© Copyright <a href="mailto:williamkempf@hotmail.com">
|
||||
William E. Kempf</a> 2001 all rights reserved.</i></p>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
2698
doc/mutex_concepts.qbk
Normal file
2698
doc/mutex_concepts.qbk
Normal file
File diff suppressed because it is too large
Load Diff
241
doc/mutexes.qbk
Normal file
241
doc/mutexes.qbk
Normal file
@@ -0,0 +1,241 @@
|
||||
[/
|
||||
(C) Copyright 2007-11 Anthony Williams
|
||||
(C) Copyright 2011-12 Vicente J. Botet Escriba
|
||||
Distributed under the Boost Software License, Version 1.0.
|
||||
(See accompanying file LICENSE_1_0.txt or copy at
|
||||
http://www.boost.org/LICENSE_1_0.txt).
|
||||
]
|
||||
|
||||
[section:mutex_types Mutex Types]
|
||||
|
||||
[section:mutex Class `mutex`]
|
||||
|
||||
#include <boost/thread/mutex.hpp>
|
||||
|
||||
class mutex:
|
||||
boost::noncopyable
|
||||
{
|
||||
public:
|
||||
mutex();
|
||||
~mutex();
|
||||
|
||||
void lock();
|
||||
bool try_lock();
|
||||
void unlock();
|
||||
|
||||
typedef platform-specific-type native_handle_type;
|
||||
native_handle_type native_handle();
|
||||
|
||||
typedef unique_lock<mutex> scoped_lock;
|
||||
typedef unspecified-type scoped_try_lock;
|
||||
};
|
||||
|
||||
__mutex__ implements the __lockable_concept__ to provide an exclusive-ownership mutex. At most one thread can own the lock on a given
|
||||
instance of __mutex__ at any time. Multiple concurrent calls to __lock_ref__, __try_lock_ref__ and __unlock_ref__ shall be permitted.
|
||||
|
||||
[section:nativehandle Member function `native_handle()`]
|
||||
|
||||
typedef platform-specific-type native_handle_type;
|
||||
native_handle_type native_handle();
|
||||
|
||||
[variablelist
|
||||
|
||||
[[Effects:] [Returns an instance of `native_handle_type` that can be used with platform-specific APIs to manipulate the underlying
|
||||
implementation. If no such instance exists, `native_handle()` and `native_handle_type` are not present.]]
|
||||
|
||||
[[Throws:] [Nothing.]]
|
||||
|
||||
]
|
||||
|
||||
[endsect]
|
||||
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:try_mutex Typedef `try_mutex`]
|
||||
|
||||
#include <boost/thread/mutex.hpp>
|
||||
|
||||
typedef mutex try_mutex;
|
||||
|
||||
__try_mutex__ is a `typedef` to __mutex__, provided for backwards compatibility with previous releases of boost.
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:timed_mutex Class `timed_mutex`]
|
||||
|
||||
#include <boost/thread/mutex.hpp>
|
||||
|
||||
class timed_mutex:
|
||||
boost::noncopyable
|
||||
{
|
||||
public:
|
||||
timed_mutex();
|
||||
~timed_mutex();
|
||||
|
||||
void lock();
|
||||
void unlock();
|
||||
bool try_lock();
|
||||
|
||||
template <class Rep, class Period>
|
||||
bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
|
||||
template <class Clock, class Duration>
|
||||
bool try_lock_until(const chrono::time_point<Clock, Duration>& t);
|
||||
|
||||
typedef platform-specific-type native_handle_type;
|
||||
native_handle_type native_handle();
|
||||
|
||||
typedef unique_lock<timed_mutex> scoped_timed_lock;
|
||||
typedef unspecified-type scoped_try_lock;
|
||||
typedef scoped_timed_lock scoped_lock;
|
||||
|
||||
#if defined BOOST_THREAD_PROVIDES_DATE_TIME || defined BOOST_THREAD_DONT_USE_CHRONO
|
||||
bool timed_lock(system_time const & abs_time);
|
||||
template<typename TimeDuration>
|
||||
bool timed_lock(TimeDuration const & relative_time);
|
||||
#endif
|
||||
|
||||
};
|
||||
|
||||
__timed_mutex__ implements the __timed_lockable_concept__ to provide an exclusive-ownership mutex. At most one thread can own the
|
||||
lock on a given instance of __timed_mutex__ at any time. Multiple concurrent calls to __lock_ref__, __try_lock_ref__,
|
||||
__timed_lock_ref__, __timed_lock_duration_ref__ and __unlock_ref__ shall be permitted.
|
||||
|
||||
[section:nativehandle Member function `native_handle()`]
|
||||
|
||||
typedef platform-specific-type native_handle_type;
|
||||
native_handle_type native_handle();
|
||||
|
||||
[variablelist
|
||||
|
||||
[[Effects:] [Returns an instance of `native_handle_type` that can be used with platform-specific APIs to manipulate the underlying
|
||||
implementation. If no such instance exists, `native_handle()` and `native_handle_type` are not present.]]
|
||||
|
||||
[[Throws:] [Nothing.]]
|
||||
|
||||
]
|
||||
|
||||
[endsect]
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:recursive_mutex Class `recursive_mutex`]
|
||||
|
||||
#include <boost/thread/recursive_mutex.hpp>
|
||||
|
||||
class recursive_mutex:
|
||||
boost::noncopyable
|
||||
{
|
||||
public:
|
||||
recursive_mutex();
|
||||
~recursive_mutex();
|
||||
|
||||
void lock();
|
||||
bool try_lock() noexcept;
|
||||
void unlock();
|
||||
|
||||
typedef platform-specific-type native_handle_type;
|
||||
native_handle_type native_handle();
|
||||
|
||||
typedef unique_lock<recursive_mutex> scoped_lock;
|
||||
typedef unspecified-type scoped_try_lock;
|
||||
};
|
||||
|
||||
__recursive_mutex__ implements the __lockable_concept__ to provide an exclusive-ownership recursive mutex. At most one thread can
|
||||
own the lock on a given instance of __recursive_mutex__ at any time. Multiple concurrent calls to __lock_ref__, __try_lock_ref__ and
|
||||
__unlock_ref__ shall be permitted. A thread that already has exclusive ownership of a given __recursive_mutex__ instance can call
|
||||
__lock_ref__ or __try_lock_ref__ to acquire an additional level of ownership of the mutex. __unlock_ref__ must be called once for
|
||||
each level of ownership acquired by a single thread before ownership can be acquired by another thread.
|
||||
|
||||
[section:nativehandle Member function `native_handle()`]
|
||||
|
||||
typedef platform-specific-type native_handle_type;
|
||||
native_handle_type native_handle();
|
||||
|
||||
[variablelist
|
||||
|
||||
[[Effects:] [Returns an instance of `native_handle_type` that can be used with platform-specific APIs to manipulate the underlying
|
||||
implementation. If no such instance exists, `native_handle()` and `native_handle_type` are not present.]]
|
||||
|
||||
[[Throws:] [Nothing.]]
|
||||
|
||||
]
|
||||
|
||||
[endsect]
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:recursive_try_mutex Typedef `recursive_try_mutex`]
|
||||
|
||||
#include <boost/thread/recursive_mutex.hpp>
|
||||
|
||||
typedef recursive_mutex recursive_try_mutex;
|
||||
|
||||
__recursive_try_mutex__ is a `typedef` to __recursive_mutex__, provided for backwards compatibility with previous releases of boost.
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:recursive_timed_mutex Class `recursive_timed_mutex`]
|
||||
|
||||
#include <boost/thread/recursive_mutex.hpp>
|
||||
|
||||
class recursive_timed_mutex:
|
||||
boost::noncopyable
|
||||
{
|
||||
public:
|
||||
recursive_timed_mutex();
|
||||
~recursive_timed_mutex();
|
||||
|
||||
void lock();
|
||||
bool try_lock() noexcept;
|
||||
void unlock();
|
||||
|
||||
|
||||
template <class Rep, class Period>
|
||||
bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
|
||||
template <class Clock, class Duration>
|
||||
bool try_lock_until(const chrono::time_point<Clock, Duration>& t);
|
||||
|
||||
typedef platform-specific-type native_handle_type;
|
||||
native_handle_type native_handle();
|
||||
|
||||
typedef unique_lock<recursive_timed_mutex> scoped_lock;
|
||||
typedef unspecified-type scoped_try_lock;
|
||||
typedef scoped_lock scoped_timed_lock;
|
||||
|
||||
#if defined BOOST_THREAD_PROVIDES_DATE_TIME || defined BOOST_THREAD_DONT_USE_CHRONO
|
||||
bool timed_lock(system_time const & abs_time);
|
||||
template<typename TimeDuration>
|
||||
bool timed_lock(TimeDuration const & relative_time);
|
||||
#endif
|
||||
|
||||
};
|
||||
|
||||
__recursive_timed_mutex__ implements the __timed_lockable_concept__ to provide an exclusive-ownership recursive mutex. At most one
|
||||
thread can own the lock on a given instance of __recursive_timed_mutex__ at any time. Multiple concurrent calls to __lock_ref__,
|
||||
__try_lock_ref__, __timed_lock_ref__, __timed_lock_duration_ref__ and __unlock_ref__ shall be permitted. A thread that already has
|
||||
exclusive ownership of a given __recursive_timed_mutex__ instance can call __lock_ref__, __timed_lock_ref__,
|
||||
__timed_lock_duration_ref__ or __try_lock_ref__ to acquire an additional level of ownership of the mutex. __unlock_ref__ must be
|
||||
called once for each level of ownership acquired by a single thread before ownership can be acquired by another thread.
|
||||
|
||||
[section:nativehandle Member function `native_handle()`]
|
||||
|
||||
typedef platform-specific-type native_handle_type;
|
||||
native_handle_type native_handle();
|
||||
|
||||
[variablelist
|
||||
|
||||
[[Effects:] [Returns an instance of `native_handle_type` that can be used with platform-specific APIs to manipulate the underlying
|
||||
implementation. If no such instance exists, `native_handle()` and `native_handle_type` are not present.]]
|
||||
|
||||
[[Throws:] [Nothing.]]
|
||||
|
||||
]
|
||||
|
||||
[endsect]
|
||||
|
||||
[endsect]
|
||||
|
||||
[include shared_mutex_ref.qbk]
|
||||
|
||||
[endsect]
|
||||
84
doc/once.qbk
Normal file
84
doc/once.qbk
Normal file
@@ -0,0 +1,84 @@
|
||||
[/
|
||||
(C) Copyright 2007-8 Anthony Williams.
|
||||
Distributed under the Boost Software License, Version 1.0.
|
||||
(See accompanying file LICENSE_1_0.txt or copy at
|
||||
http://www.boost.org/LICENSE_1_0.txt).
|
||||
]
|
||||
|
||||
[section:once One-time Initialization]
|
||||
|
||||
#include <boost/thread/once.hpp>
|
||||
|
||||
namespace boost
|
||||
{
|
||||
struct once_flag;
|
||||
template<typename Callable>
|
||||
void call_once(once_flag& flag,Callable func);
|
||||
|
||||
#if defined BOOST_THREAD_PROVIDES_DEPRECATED_FEATURES_SINCE_V3_0_0
|
||||
void call_once(void (*func)(),once_flag& flag);
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
`boost::call_once` provides a mechanism for ensuring that an initialization routine is run exactly once without data races or deadlocks.
|
||||
|
||||
[section:once_flag Typedef `once_flag`]
|
||||
|
||||
#ifdef BOOST_THREAD_PROVIDES_ONCE_CXX11
|
||||
struct once_flag
|
||||
{
|
||||
constexprr once_flag() noexcept;
|
||||
once_flag(const once_flag&) = delete;
|
||||
once_flag& operator=(const once_flag&) = delete;
|
||||
};
|
||||
#else
|
||||
typedef platform-specific-type once_flag;
|
||||
#define BOOST_ONCE_INIT platform-specific-initializer
|
||||
#endif
|
||||
|
||||
Objects of type `boost::once_flag` shall be initialized with `BOOST_ONCE_INIT` if BOOST_THREAD_PROVIDES_ONCE_CXX11 is not defined
|
||||
|
||||
boost::once_flag f=BOOST_ONCE_INIT;
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:call_once Non-member function `call_once`]
|
||||
|
||||
template<typename Callable>
|
||||
void call_once(once_flag& flag,Callable func);
|
||||
|
||||
[variablelist
|
||||
|
||||
[[Requires:] [`Callable` is `CopyConstructible`. Copying `func` shall have no side effects, and the effect of calling the copy shall
|
||||
be equivalent to calling the original. ]]
|
||||
|
||||
[[Effects:] [Calls to `call_once` on the same `once_flag` object are serialized. If there has been no prior effective `call_once` on
|
||||
the same `once_flag` object, the argument `func` (or a copy thereof) is called as-if by invoking `func()`, and the invocation of
|
||||
`call_once` is effective if and only if `func()` returns without exception. If an exception is thrown, the exception is
|
||||
propagated to the caller. If there has been a prior effective `call_once` on the same `once_flag` object, the `call_once` returns
|
||||
without invoking `func`. ]]
|
||||
|
||||
[[Synchronization:] [The completion of an effective `call_once` invocation on a `once_flag` object, synchronizes with
|
||||
all subsequent `call_once` invocations on the same `once_flag` object. ]]
|
||||
|
||||
[[Throws:] [`thread_resource_error` when the effects cannot be achieved. or any exception propagated from `func`.]]
|
||||
|
||||
[[Note:] [The function passed to `call_once` must not also call
|
||||
`call_once` passing the same `once_flag` object. This may cause
|
||||
deadlock, or invoking the passed function a second time. The
|
||||
alternative is to allow the second call to return immediately, but
|
||||
that assumes the code knows it has been called recursively, and can
|
||||
proceed even though the call to `call_once` didn't actually call the
|
||||
function, in which case it could also avoid calling `call_once`
|
||||
recursively.]]
|
||||
|
||||
]
|
||||
|
||||
void call_once(void (*func)(),once_flag& flag);
|
||||
|
||||
This second overload is provided for backwards compatibility. The effects of `call_once(func,flag)` shall be the same as those of
|
||||
`call_once(flag,func)`.
|
||||
|
||||
[endsect]
|
||||
[endsect]
|
||||
@@ -1,224 +0,0 @@
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content=
|
||||
"text/html; charset=windows-1252">
|
||||
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
|
||||
<meta name="ProgId" content="FrontPage.Editor.Document">
|
||||
|
||||
<title>Boost.Threads Overview</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<table summary="header" border="0" cellpadding="7" cellspacing="0"
|
||||
width="100%">
|
||||
<tr>
|
||||
<td valign="top" width="300">
|
||||
<h3><img height="86" alt="C++ Boost" src=
|
||||
"../../../c++boost.gif" width="277"></h3>
|
||||
</td>
|
||||
|
||||
<td valign="top">
|
||||
<h1 align="center">Boost.Threads</h1>
|
||||
|
||||
<h2 align="center">Overview</h2>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<p><a href="#Introduction">Introduction</a><br>
|
||||
<a href="#Dangers">Dangers</a><br>
|
||||
<a href="#Library">C++ Standard Library usage</a><br>
|
||||
<a href="#Common">Common requirements</a></p>
|
||||
|
||||
<h2><a name="Introduction">Introduction</a></h2>
|
||||
|
||||
<p>Boost.Threads allows C++ programs to execute as multiple,
|
||||
asynchronous, independent, threads-of-execution. Each thread has its
|
||||
own machine state including program instruction counter and registers.
|
||||
Programs which execute as multiple threads are called multi-threaded
|
||||
programs to distinguish them from traditional single-threaded programs.
|
||||
<a href="definitions.html">Definitions</a> gives a more complete
|
||||
description of the multi-threading execution environment.</p>
|
||||
|
||||
<p>Multi-threading provides several advantages:</p>
|
||||
|
||||
<ul>
|
||||
<li>Programs which would otherwise block waiting for some external
|
||||
event can continue to respond if the blocking operation is placed
|
||||
in a separate thread. Multi-threading is usually an absolute
|
||||
requirement for these programs.</li>
|
||||
</ul>
|
||||
|
||||
<ul>
|
||||
<li>Well-designed multi-threaded programs may execute faster than
|
||||
single-threaded programs, particularly on multi-processor hardware.
|
||||
Note, however, that poorly-designed multi-threaded programs are
|
||||
often slower that single-threaded programs.</li>
|
||||
</ul>
|
||||
|
||||
<ul>
|
||||
<li>Some program designs may be easier to formulate using a
|
||||
multi-threaded approach. After all, the real world is
|
||||
asynchronous!</li>
|
||||
</ul>
|
||||
|
||||
<h2><a name="Dangers">Dangers</a></h2>
|
||||
|
||||
<p>Beyond the errors which can occur in single-threaded programs,
|
||||
multi-threaded programs are subject to additional errors:</p>
|
||||
|
||||
<ul>
|
||||
<li><a href="definitions.html#Race condition">Race
|
||||
conditions</a>.</li>
|
||||
|
||||
<li><a href="definitions.html#Deadlock">Deadlock</a> (sometimes
|
||||
called "deadly embrace")</li>
|
||||
|
||||
<li><a href="definitions.html#Priority failure">Priority
|
||||
failures</a> (priority inversion, infinite overtaking, starvation,
|
||||
etc.)</li>
|
||||
</ul>
|
||||
|
||||
<p>Every multi-threaded program must be designed carefully to avoid
|
||||
race conditions and deadlock. These aren't rare or exotic failures
|
||||
- they are virtually guaranteed to occur unless multi-threaded code is
|
||||
designed to avoid them. Priority failures are somewhat less common, but
|
||||
are none-the-less serious.</p>
|
||||
|
||||
<p>The <a href="introduction.html">Boost.Threads design</a> attempts to
|
||||
minimize these errors, but they will still occur unless the programmer
|
||||
proactively designs to avoid them.</p>
|
||||
|
||||
<h3>Testing and debugging considerations</h3>
|
||||
|
||||
<p>Multi-threaded programs are non-deterministic. In other words, the
|
||||
same program with the same input data may follow different execution
|
||||
paths each time it is invoked. That can make testing and debugging a
|
||||
nightmare:</p>
|
||||
|
||||
<ul>
|
||||
<li>Failures are often not repeatable.</li>
|
||||
|
||||
<li>Probe effect causes debuggers to produce very different results
|
||||
from non-debug uses.</li>
|
||||
|
||||
<li>Debuggers require special support to show thread state.</li>
|
||||
|
||||
<li>Tests on a single processor system may give no indication of
|
||||
serious errors which would appear on multiprocessor systems, and
|
||||
visa versa. Thus test cases should include a varying number of
|
||||
processors.</li>
|
||||
|
||||
<li>For programs which create a varying number of threads according
|
||||
to workload, tests which don't span the full range of
|
||||
possibilities may miss serious errors.</li>
|
||||
</ul>
|
||||
|
||||
<h3>Getting a head start</h3>
|
||||
|
||||
<p>Although it might appear that multi-threaded programs are inherently
|
||||
unreliable, many reliable multi-threaded programs do exist.
|
||||
Multi-threading techniques are known which lead to reliable
|
||||
programs.</p>
|
||||
|
||||
<p>Design patterns for reliable multi-threaded programs, including the
|
||||
important <i>monitor</i> pattern, are presented in <cite>
|
||||
Pattern-Oriented Software Architecture Volume 2 - Patterns for
|
||||
Concurrent and Networked Objects</cite> [<a href=
|
||||
"bibliography.html#Schmidt-00">Schmidt 00</a>]. Many important
|
||||
multi-threading programming considerations (independent of threading
|
||||
library) are discussed in <cite>Programming with POSIX Threads</cite>
|
||||
[<a href="bibliography.html#Butenhof-97">Butenhof 97</a>].</p>
|
||||
|
||||
<p>Doing some reading before attempting multi-threaded designs will give
|
||||
you a head start toward reliable multi-threaded programs.</p>
|
||||
|
||||
<h2><a name="Library">C++ Standard Library usage in multi-threaded
|
||||
programs</a></h2>
|
||||
|
||||
<h3>Runtime libraries</h3>
|
||||
|
||||
<p><b>Warning:</b> Multi-threaded programs such as those using <b>
|
||||
Boost.Threads</b> must link to <a href="definitions.html#Thread-safe">
|
||||
thread-safe</a> versions of all runtime libraries used by the program,
|
||||
including the runtime library for the C++ Standard Library. Otherwise
|
||||
<a href="definitions.html#Race condition">race conditions</a> will
|
||||
occur when multiple threads simultaneously execute runtime library
|
||||
functions for <i>new</i>, <i>delete</i>, or other language features
|
||||
which imply shared state.</p>
|
||||
|
||||
<h3>Potentially non-thread-safe functions</h3>
|
||||
|
||||
<p>Certain C++ Standard Library functions inherited from C are
|
||||
particular problems because they hold internal state between calls:</p>
|
||||
|
||||
<ul>
|
||||
<li>rand</li>
|
||||
|
||||
<li>strtok</li>
|
||||
|
||||
<li>asctime</li>
|
||||
|
||||
<li>ctime</li>
|
||||
|
||||
<li>gmtime</li>
|
||||
|
||||
<li>localtime</li>
|
||||
</ul>
|
||||
|
||||
<p>It is possible to write thread-safe implementations of these by
|
||||
using <a href="thread_specific_ptr.html">thread-specific storage</a>,
|
||||
and several C++ compiler vendors do just that. The technique is
|
||||
well-know and is explained in [<a href=
|
||||
"bibliography.html#Butenhof-97">Buttenhof-97</a>].</p>
|
||||
|
||||
<p>But at least one vendor (HP-UX) does not provide thread-safe
|
||||
implementations of the above functions in their otherwise thread-safe
|
||||
runtime library. Instead they provide replacement functions with
|
||||
different names and arguments.</p>
|
||||
|
||||
<p><b>Recommendation:</b> For the most portable, yet thread-safe code,
|
||||
use Boost replacements for the problem functions. See the <a href=
|
||||
"../../random/index.html">Boost Random Number Library</a> and <a href=
|
||||
"../../tokenizer/index.htm">Boost Tokenizer Library</a>.</p>
|
||||
|
||||
<h2><a name="Common">Common</a> requirements for all Boost.Threads
|
||||
components</h2>
|
||||
|
||||
<h3>Exceptions</h3>
|
||||
|
||||
<p><b>Boost.Threads</b> destructors never throw exceptions. Unless
|
||||
otherwise specified, other <b>Boost.Threads</b> functions that do not
|
||||
have an exception-specification may throw implementation-defined
|
||||
exceptions.</p>
|
||||
|
||||
<p>In particular, <b>Boost.Threads</b> reports failure to allocate
|
||||
storage by throwing an exception of type std::bad_alloc, or a class
|
||||
derived from std::bad_alloc, failure to obtain thread resources other
|
||||
than memory by throwing an exception of type <a href=
|
||||
"thread_resource_error.html">boost::thread_resource_error</a>, and
|
||||
certain lock related failures by throwing an exception of type <a href=
|
||||
"lock_error.html">boost::lock_error</a></p>
|
||||
|
||||
<p><b>Rationale:</b> Follows the C++ Standard Library practice of
|
||||
allowing all functions except destructors or other specified functions
|
||||
to throw exceptions on errors.</p>
|
||||
|
||||
<h3><a name="NonCopyable">NonCopyable</a> requirement</h3>
|
||||
|
||||
<p><b>Boost.Threads</b> classes documented as meeting the NonCopyable
|
||||
requirement disallow copy construction and copy assignment. For the
|
||||
sake of exposition, the synopsis of such classes show private
|
||||
derivation from <a href="../../utility/utility.htm">
|
||||
boost::noncopyable</a>. Users should not depend on this derivation,
|
||||
however, as implementations are free to meet the NonCopyable
|
||||
requirement in other ways.</p>
|
||||
<hr>
|
||||
|
||||
<p>Revised
|
||||
<!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan -->05 November, 2001<!--webbot bot="Timestamp" endspan i-checksum="39359" --></p>
|
||||
|
||||
<p>© Copyright 2001 Beman Dawes</p>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
74
doc/overview.qbk
Normal file
74
doc/overview.qbk
Normal file
@@ -0,0 +1,74 @@
|
||||
[/
|
||||
(C) Copyright 2007-12 Anthony Williams.
|
||||
(C) Copyright 20012 Vicente J. Botet Escriba.
|
||||
Distributed under the Boost Software License, Version 1.0.
|
||||
(See accompanying file LICENSE_1_0.txt or copy at
|
||||
http://www.boost.org/LICENSE_1_0.txt).
|
||||
]
|
||||
|
||||
[section:overview Overview]
|
||||
|
||||
__boost_thread__ enables the use of multiple threads of execution with shared data in portable C++ code. It provides classes and
|
||||
functions for managing the threads themselves, along with others for synchronizing data between the threads or providing separate
|
||||
copies of data specific to individual threads.
|
||||
|
||||
The __boost_thread__ library was originally written and designed by William E. Kempf (version 1).
|
||||
|
||||
Anthony Williams version (version 2) was a major rewrite designed to
|
||||
closely follow the proposals presented to the C++ Standards Committee, in particular
|
||||
[@http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2497.html N2497],
|
||||
[@http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2320.html N2320],
|
||||
[@http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2184.html N2184],
|
||||
[@http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2139.html N2139], and
|
||||
[@http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2094.html N2094]
|
||||
|
||||
Vicente J. Botet Escriba started (version 3) the adaptation to comply with the accepted Thread C++11 library (Make use of Boost.Chrono and Boost.Move) and the [@http://home.roadrunner.com/~hinnant/bloomington/shared_mutex.html Shared Locking] Howard Hinnant proposal except for the upward conversions.
|
||||
Some minor non-standard features have been added also as thread attributes, reverse_lock, shared_lock_guard.
|
||||
|
||||
In order to use the classes and functions described here, you can
|
||||
either include the specific headers specified by the descriptions of
|
||||
each class or function, or include the master thread library header:
|
||||
|
||||
#include <boost/thread.hpp>
|
||||
|
||||
which includes all the other headers in turn.
|
||||
|
||||
[endsect]
|
||||
|
||||
|
||||
[section:build Using and building the library]
|
||||
|
||||
Boost.Thread is configured following the conventions used to build [@http://www.boost.org/doc/libs/1_48_0/libs/config/doc/html/boost_config/boost_macro_reference.html#boost_config.boost_macro_reference.macros_for_libraries_with_separate_source_code libraries with separate source code]. Boost.Thread will import/export the code only if the user has specifically asked for it, by defining either BOOST_ALL_DYN_LINK if they want all boost libraries to be dynamically linked, or BOOST_THREAD_DYN_LINK if they want just this one to be dynamically liked.
|
||||
|
||||
The definition of these macros determines whether BOOST_THREAD_USE_DLL is defined. If BOOST_THREAD_USE_DLL is not defined, the library will define BOOST_THREAD_USE_DLL or BOOST_THREAD_USE_LIB depending on whether the platform. On non windows platforms BOOST_THREAD_USE_LIB is defined if is not defined. In windows platforms, BOOST_THREAD_USE_LIB is defined if BOOST_THREAD_USE_DLL and the compiler supports auto-tss cleanup with Boost.Threads (for the time been Msvc and Intel)
|
||||
|
||||
The source code compiled when building the library defines a macros BOOST_THREAD_SOURCE that is used to import or export it. The user must not define this macro in any case.
|
||||
|
||||
Boost.Thread depends on some non header-only libraries.
|
||||
|
||||
* Boost.System: This dependency is mandatory and you will need to link with the library.
|
||||
|
||||
* Boost.Chrono: This dependency is optional (see below how to configure) and you will need to link with the library if you use some of the time related interfaces.
|
||||
|
||||
* Boost.DateTime: This dependency is mandatory, but even if Boost.DateTime is a non header-only library Boost.Thread uses only parts that are header-only, so in principle you should not need to link with the library.
|
||||
|
||||
It seems that there are some IDE (as e.g. Visual Studio) that deduce the libraries that a program needs to link to inspecting the sources. Such IDE could force to link to Boost.DateTime and/or Boost.Chrono.
|
||||
|
||||
As the single mandatory dependency is to Boost.System, the following
|
||||
|
||||
bjam toolset=msvc-11.0 --build-type=complete --with-thread
|
||||
|
||||
will install only boost_thread and boost_system.
|
||||
|
||||
Users of such IDE should force the Boost.Chrono and Boost.DateTime build using
|
||||
|
||||
bjam toolset=msvc-11.0 --build-type=complete --with-thread --with-chrono --with-date_time
|
||||
|
||||
|
||||
The following section describes all the macros used to configure Boost.Thread.
|
||||
|
||||
[include configuration.qbk]
|
||||
|
||||
|
||||
[endsect]
|
||||
|
||||
@@ -1,481 +0,0 @@
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content=
|
||||
"text/html; charset=iso-8859-1">
|
||||
<meta name="keywords" content="threads, BTL, thread library, C++">
|
||||
|
||||
<title>Boost.Threads, rationale</title>
|
||||
</head>
|
||||
|
||||
<body bgcolor="#ffffff" link="#0000ff" vlink="#800080">
|
||||
<table summary="header" border="0" cellpadding="7" cellspacing="0"
|
||||
width="100%">
|
||||
<tr>
|
||||
<td valign="top" width="300">
|
||||
<h3><img height="86" alt="C++ Boost" src=
|
||||
"../../../c++boost.gif" width="277"></h3>
|
||||
</td>
|
||||
|
||||
<td valign="top">
|
||||
<h1 align="center">Boost.Threads</h1>
|
||||
|
||||
<h2 align="center">Rationale</h2>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<hr>
|
||||
|
||||
<p>This page explains the rationale behind various design decisions in
|
||||
the <b>Boost.Threads</b> library. Having the rationale documented here
|
||||
should explain how we arrived at the current design as well as prevent
|
||||
future rehashing of discussions and thought processes that have already
|
||||
occurred. It can also give users a lot of insight into the design
|
||||
process required for this library.</p>
|
||||
|
||||
<h2><a name="library">Rationale for the Creation of
|
||||
Boost.Threads</a></h2>
|
||||
|
||||
<p>Processes often have a degree of "potential parallelism"
|
||||
and it can often be more intuitive to design systems with this in mind.
|
||||
Further, these parallel processes can result in more responsive
|
||||
programs. The benefits for multi-threaded programming are quite well
|
||||
known to most modern programmers, yet the C++ language doesn't
|
||||
directly support this concept.</p>
|
||||
|
||||
<p>Many platforms support multi-threaded programming despite the fact
|
||||
that the language doesn't support it. They do this through external
|
||||
libraries, which are, unfortunately, platform specific. POSIX has tried
|
||||
to address this problem through the standardization of a
|
||||
"pthread" library. However, this is a standard only on POSIX
|
||||
platforms, so its portability is limited.</p>
|
||||
|
||||
<p>Another problem with POSIX and other platform specific thread
|
||||
libraries is that they are almost universally C based libraries. This
|
||||
leaves several C++ specific issues unresolved, such as what happens
|
||||
when an exception is thrown in a thread. Further, there are some C++
|
||||
concepts, such as destructors, that can make usage much easier than
|
||||
what's available in a C library.</p>
|
||||
|
||||
<p>What's truly needed is C++ language support for threads.
|
||||
However, the C++ standards committee needs existing practice or a good
|
||||
proposal as a starting point for adding this to the standard.</p>
|
||||
|
||||
<p>The Boost.Threads library was developed to provide a C++ developer
|
||||
with a portable interface for writing multi-threaded programs on
|
||||
numerous platforms. There's a hope that the library can be the
|
||||
basis for a more detailed proposal for the C++ standards committee to
|
||||
consider for inclusion in the next C++ standard.</p>
|
||||
|
||||
<h2><a name="primitives">Rationale for the Low Level Primitives
|
||||
Supported in Boost.Threads</a></h2>
|
||||
|
||||
<p>The Boost.Threads library supplies a set of low level primitives for
|
||||
writing multi-threaded programs, such as mutexes and condition variables.
|
||||
In fact, the first release of Boost.Threads supports only these low level
|
||||
primitives. However, computer science research has shown that use of these
|
||||
primitives is difficult since there's no way to mathematically prove
|
||||
that a usage pattern is correct, meaning it doesn't result in race
|
||||
conditions or deadlocks. There are several algebras (such as CSP, CCS and
|
||||
Join calculus) that have been developed to help write provably correct
|
||||
parallel processes. In order to prove the correctness these processes must
|
||||
be coded using higher level abstractions. So why does Boost.Threads support
|
||||
the lower level concepts?</p>
|
||||
|
||||
<p>The reason is simple: the higher level concepts need to be
|
||||
implemented using at least some of the lower level concepts. So having
|
||||
portable lower level concepts makes it easier to develop the higher
|
||||
level concepts and will allow researchers to experiment with various
|
||||
techniques.</p>
|
||||
|
||||
<p>Beyond this theoretical application of higher level concepts,
|
||||
however, the fact remains that many multi-threaded programs are written
|
||||
using only the lower level concepts, so they are useful in and of
|
||||
themselves, even if it's hard to prove that their usage is correct.
|
||||
Since many users will be familiar with these lower level concepts but
|
||||
be unfamiliar with any of the higher level concepts there's also an
|
||||
argument for accessibility.</p>
|
||||
|
||||
<h2><a name="lock_objects">Rationale for the Lock Design</a></h2>
|
||||
|
||||
<p>Programmers who are used to multi-threaded programming issues will
|
||||
quickly note that the Boost.Thread's design for mutex lock concepts
|
||||
is not <a href="definitions.html#Thread-safe">thread-safe</a> (this is
|
||||
clearly documented as well). At first this may seem like a serious
|
||||
design flaw. Why have a multi-threading primitive that's not
|
||||
thread-safe itself?</p>
|
||||
|
||||
<p>A lock object is not a synchronization primitive. A lock
|
||||
object's sole responsibility is to ensure that a mutex is both
|
||||
locked and unlocked in a manner that won't result in the common
|
||||
error of locking a mutex and then forgetting to unlock it. This means
|
||||
that instances of a lock object are only going to be created, at least
|
||||
in theory, within block scope and won't be shared between threads.
|
||||
Only the mutex objects will be created outside of block scope and/or
|
||||
shared between threads. Though it's possible to create a lock
|
||||
object outside of block scope and to share it between threads to do so
|
||||
would not be a typical usage. Nor are there any cases when such usage
|
||||
would be required.</p>
|
||||
|
||||
<p>Lock objects must maintain some state information. In order to allow
|
||||
a program to determine if a try_lock or timed_lock was successful the
|
||||
lock object must retain state indicating the success or failure of the
|
||||
call made in its constructor. If a lock object were to have such state
|
||||
and remain thread-safe it would need to synchronize access to the state
|
||||
information which would result in roughly doubling the time of most
|
||||
operations. Worse, since checking the state can occur only by a call
|
||||
after construction we'd have a race condition if the lock object
|
||||
were shared between threads.</p>
|
||||
|
||||
<p>So, to avoid the overhead of synchronizing access to the state
|
||||
information and to avoid the race condition the Boost.Threads library
|
||||
simply does nothing to make lock objects thread-safe. Instead, sharing
|
||||
a lock object between threads results in undefined behavior. Since the
|
||||
only proper usage of lock objects is within block scope this isn't
|
||||
a problem, and so long as the lock object is properly used there's
|
||||
no danger of any multi-threading issues.</p>
|
||||
|
||||
<h2><a name="thread">Rationale for Non-copyable Thread Type</a></h2>
|
||||
|
||||
<p>Programmers who are used to C libraries for multi-threaded
|
||||
programming are likely to wonder why Boost.Threads uses a non-copyable
|
||||
design for <a href="thread.html">boost::thread</a>. After all, the C
|
||||
thread types are copyable, and you often have a need for copying them
|
||||
within user code. However, careful comparison of C designs to C++
|
||||
designs shows a flaw in this logic.</p>
|
||||
|
||||
<p>All C types are copyable. It is, in fact, not possible to make a
|
||||
non-copyable type in C. For this reason types that represent system
|
||||
resources in C are often designed to behave very similarly to a pointer
|
||||
to dynamic memory. There's an API for acquiring the resource and an
|
||||
API for releasing the resources. For memory we have pointers as the
|
||||
type and alloc/free for the acquisition and release APIs. For files we
|
||||
have FILE* as the type and fopen/fclose for the acquisition and release
|
||||
APIs. You can freely copy instances of the types but must manually
|
||||
manage the lifetime of the actual resource through the acquisition and
|
||||
release APIs.</p>
|
||||
|
||||
<p>C++ designs recognize that the acquisition and release APIs are
|
||||
error prone and try to eliminate possible errors by acquiring the
|
||||
resource in the constructor and releasing it in the destructor. The
|
||||
best example of such a design is the std::iostream set of classes which
|
||||
can represent the same resource as the FILE* type in C. A file is
|
||||
opened in the std::fstream's constructor and closed in its
|
||||
destructor. However, if an iostream were copyable it could lead to a
|
||||
file being closed twice, an obvious error, so the std::iostream types
|
||||
are noncopyable by design. This is the same design used by
|
||||
boost::thread, which is a simple and easy to understand design
|
||||
that's consistent with other C++ standard types.</p>
|
||||
|
||||
<p>During the design of boost::thread it was pointed out that it would
|
||||
be possible to allow it to be a copyable type if some form of
|
||||
"reference management" were used, such as ref-counting or
|
||||
ref-lists, and many argued for a boost::thread_ref design instead. The
|
||||
reasoning was that copying "thread" objects was a typical
|
||||
need in the C libraries, and so presumably would be in the C++
|
||||
libraries as well. It was also thought that implementations could
|
||||
provide more efficient reference management then wrappers (such as
|
||||
boost::shared_ptr) around a noncopyable thread concept. Analysis of
|
||||
whether or not these arguments would hold true don't appear to bear
|
||||
them out. To illustrate the analysis we'll first provide
|
||||
pseudo-code illustrating the six typical usage patterns of a thread
|
||||
object.</p>
|
||||
|
||||
<h3>1. Simple creation of a thread.</h3>
|
||||
<pre>
|
||||
void foo()
|
||||
{
|
||||
create_thread(&bar);
|
||||
}
|
||||
</pre>
|
||||
|
||||
<h3>2. Creation of a thread that's later joined.</h3>
|
||||
<pre>
|
||||
void foo()
|
||||
{
|
||||
thread = create_thread(&bar);
|
||||
join(thread);
|
||||
}
|
||||
</pre>
|
||||
|
||||
<h3>3. Simple creation of several threads in a loop.</h3>
|
||||
<pre>
|
||||
void foo()
|
||||
{
|
||||
for (int i=0; i<NUM_THREADS; ++i)
|
||||
create_thread(&bar);
|
||||
}
|
||||
</pre>
|
||||
|
||||
<h3>4. Creation of several threads in a loop which are later
|
||||
joined.</h3>
|
||||
<pre>
|
||||
void foo()
|
||||
{
|
||||
for (int i=0; i<NUM_THREADS; ++i)
|
||||
threads[i] = create_thread(&bar);
|
||||
for (int i=0; i<NUM_THREADS; ++i)
|
||||
threads[i].join();
|
||||
}
|
||||
</pre>
|
||||
|
||||
<h3>5. Creation of a thread whose ownership is passed to another
|
||||
object/method.</h3>
|
||||
<pre>
|
||||
void foo()
|
||||
{
|
||||
thread = create_thread(&bar);
|
||||
manager.owns(thread);
|
||||
}
|
||||
</pre>
|
||||
|
||||
<h3>6. Creation of a thread whose ownership is shared between multiple
|
||||
objects.</h3>
|
||||
<pre>
|
||||
void foo()
|
||||
{
|
||||
thread = create_thread(&bar);
|
||||
manager1.add(thread);
|
||||
manager2.add(thread);
|
||||
}
|
||||
</pre>
|
||||
|
||||
<p>Of these usage patterns there's only one that requires reference
|
||||
management (number 6). Hopefully it's fairly obvious that this
|
||||
usage pattern simply won't occur as often as the other usage
|
||||
patterns. So there really isn't a "typical need" for a
|
||||
thread concept, though there is some need.</p>
|
||||
|
||||
<p>Since the need isn't typical we must use different criteria for
|
||||
deciding on either a thread_ref or thread design. Possible criteria
|
||||
include ease of use and performance. So let's analyze both of these
|
||||
carefully.</p>
|
||||
|
||||
<p>With ease of use we can look at existing experience. The standard
|
||||
C++ objects that represent a system resource, such as std::iostream,
|
||||
are noncopyable, so we know that C++ programmers must at least be
|
||||
experienced with this design. Most C++ developers are also used to
|
||||
smart pointers such as boost::shared_ptr, so we know they can at least
|
||||
adapt to a thread_ref concept with little effort. So existing
|
||||
experience isn't going to lead us to a choice.</p>
|
||||
|
||||
<p>The other thing we can look at is how difficult it is to use both
|
||||
types for the six usage patterns above. If we find it overly difficult
|
||||
to use a concept for any of the usage patterns there would be a good
|
||||
argument for choosing the other design. So we'll code all six usage
|
||||
patterns using both designs.</p>
|
||||
|
||||
<h3>1.</h3>
|
||||
<pre>
|
||||
void foo()
|
||||
{
|
||||
thread thrd(&bar);
|
||||
}
|
||||
|
||||
void foo()
|
||||
{
|
||||
thread_ref thrd = create_thread(&bar);
|
||||
}
|
||||
</pre>
|
||||
|
||||
<h3>2.</h3>
|
||||
<pre>
|
||||
void foo()
|
||||
{
|
||||
thread thrd(&bar);
|
||||
thrd.join();
|
||||
}
|
||||
|
||||
void foo()
|
||||
{
|
||||
thread_ref thrd =
|
||||
create_thread(&bar);thrd->join();
|
||||
}
|
||||
</pre>
|
||||
|
||||
<h3>3.</h3>
|
||||
<pre>
|
||||
void foo()
|
||||
{
|
||||
for (int i=0; i<NUM_THREADS; ++i)
|
||||
thread thrd(&bar);
|
||||
}
|
||||
|
||||
void foo()
|
||||
{
|
||||
for (int i=0; i<NUM_THREADS; ++i)
|
||||
thread_ref thrd = create_thread(&bar);
|
||||
}
|
||||
</pre>
|
||||
|
||||
<h3>4.</h3>
|
||||
<pre>
|
||||
void foo()
|
||||
{
|
||||
std::auto_ptr<thread> threads[NUM_THREADS];
|
||||
for (int i=0; i<NUM_THREADS; ++i)
|
||||
threads[i] = std::auto_ptr<thread>(new thread(&bar));
|
||||
for (int i= 0; i<NUM_THREADS;
|
||||
++i)threads[i]->join();
|
||||
}
|
||||
|
||||
void foo()
|
||||
{
|
||||
thread_ref threads[NUM_THREADS];
|
||||
for (int i=0; i<NUM_THREADS; ++i)
|
||||
threads[i] = create_thread(&bar);
|
||||
for (int i= 0; i<NUM_THREADS;
|
||||
++i)threads[i]->join();
|
||||
}
|
||||
</pre>
|
||||
|
||||
<h3>5.</h3>
|
||||
<pre>
|
||||
void foo()
|
||||
{
|
||||
thread thrd* = new thread(&bar);
|
||||
manager.owns(thread);
|
||||
}
|
||||
|
||||
void foo()
|
||||
{
|
||||
thread_ref thrd = create_thread(&bar);
|
||||
manager.owns(thrd);
|
||||
}
|
||||
</pre>
|
||||
|
||||
<h3>6.</h3>
|
||||
<pre>
|
||||
void foo()
|
||||
{
|
||||
boost::shared_ptr<thread> thrd(new thread(&bar));
|
||||
manager1.add(thrd);
|
||||
manager2.add(thrd);
|
||||
}
|
||||
|
||||
void foo()
|
||||
{
|
||||
thread_ref thrd = create_thread(&bar);
|
||||
manager1.add(thrd);
|
||||
manager2.add(thrd);
|
||||
}
|
||||
</pre>
|
||||
|
||||
<p>This shows the usage patterns being nearly identical in complexity
|
||||
for both designs. The only actual added complexity occurs because of
|
||||
the use of operator new in (4), (5) and (6) and the use of
|
||||
std::auto_ptr and boost::shared_ptr in (4) and (6) respectively.
|
||||
However, that's not really much added complexity, and C++
|
||||
programmers are used to using these idioms any way. Some may dislike
|
||||
the presence of operator new in user code, but this can be eliminated
|
||||
by proper design of higher level concepts, such as the
|
||||
boost::thread_group class that simplifies example (4) down to:</p>
|
||||
<pre>
|
||||
void foo()
|
||||
{
|
||||
thread_group threads;
|
||||
for (int i=0; i<NUM_THREADS; ++i)
|
||||
threads.create_thread(&bar);
|
||||
threads.join_all();
|
||||
}
|
||||
</pre>
|
||||
|
||||
<p>So ease of use is really a wash and not much help in picking a
|
||||
design.</p>
|
||||
|
||||
<p>So what about performance? If you look at the above code examples we
|
||||
can analyze the theoretical impact to performance that both designs
|
||||
have. For (1) we can see that platforms that don't have a
|
||||
ref-counted native thread type (POSIX, for instance) will be impacted
|
||||
by a thread_ref design. Even if the native thread type is ref-counted
|
||||
there may be an impact if more state information has to be maintained
|
||||
for concepts foreign to the native API, such as clean up stacks for
|
||||
Win32 implementations. For (2) the performance impact will be identical
|
||||
to (1). The same for (3). For (4) things get a little more interesting
|
||||
and we find that theoretically at least the thread_ref may perform
|
||||
faster since the thread design requires dynamic memory
|
||||
allocation/deallocation. However, in practice there may be dynamic
|
||||
allocation for the thread_ref design as well, it will just be hidden
|
||||
from the user. As long as the implementation has to do dynamic
|
||||
allocations the thread_ref loses again because of the reference
|
||||
management. For (5) we see the same impact as we do for (4). For (6) we
|
||||
still have a possible impact to the thread design because of dynamic
|
||||
allocation but thread_ref no longer suffers because of it's
|
||||
reference management, and in fact, theoretically at least, the
|
||||
thread_ref may do a better job of managing the references. All of this
|
||||
indicates that thread wins for (1), (2) and (3), with (4) and (5) the
|
||||
winner depends on the implementation and the platform but the thread
|
||||
design probably has a better chance, and with (6) it will again depend
|
||||
on the implementation and platform but this time we favor thread_ref
|
||||
slightly. Given all of this it's a narrow margin, but the thread
|
||||
design prevails.</p>
|
||||
|
||||
<p>Given this analysis, and the fact that noncopyable objects for
|
||||
system resources are the normal designs that C++ programmers are used
|
||||
to dealing with, the Boost.Threads library has gone with a noncopyable
|
||||
design.</p>
|
||||
|
||||
<h2>Rationale for not providing <i><a name="Events">Event</a>
|
||||
Variables</i></h2>
|
||||
|
||||
<p><i>Event variables</i> are simply far too error-prone. <a href=
|
||||
"condition.html">Condition variables</a> are a much safer
|
||||
alternative.</p>
|
||||
|
||||
<p>[Note that Graphical User Interface <i>events</i> are a different
|
||||
concept, and are not what is being discussed here.]</p>
|
||||
|
||||
<p>Event variables were one of the first synchronization primitives.
|
||||
They are still used today, for example, in the native Windows
|
||||
multithreading API.</p>
|
||||
|
||||
<p>Yet both respected computer science researchers and experienced
|
||||
multithreading practitioners believe event variables are so inherently
|
||||
error-prone that they should never be used, and thus should not be part
|
||||
of a multithreading library.</p>
|
||||
|
||||
<p>Per Brinch Hansen <a href="bibliography.html#Brinch-Hansen-73">
|
||||
[Brinch Hansen 73]</a> analyzed event variables in some detail,
|
||||
pointing out [emphasis his] that "<i>event operations force the
|
||||
programmer to be aware of the relative speeds of the sending and
|
||||
receiving processes</i>". His summary:</p>
|
||||
|
||||
<blockquote>
|
||||
<p>We must therefore conclude that event variables of the previous
|
||||
type are impractical for system design. <i>The effect of an
|
||||
interaction between two processes must be independent of the speed
|
||||
at which it is carried out.</i></p>
|
||||
</blockquote>
|
||||
|
||||
<p>Experienced programmers using the Windows platform today report that
|
||||
event variables are a continuing source of errors, even after previous
|
||||
bad experiences caused them to be very careful in their use of event
|
||||
variables. Overt problems can be avoided, for example, by teaming the
|
||||
event variable with a mutex, but that may just convert a <a href=
|
||||
"definitions.html#Race condition">race condition</a> into another
|
||||
problem, such as excessive resource use. One of the most distressing
|
||||
aspects of the experience reports is the claim that many defects are
|
||||
latent. That is, the programs appear to work correctly, but contain
|
||||
hidden timing dependencies which will cause them to fail when
|
||||
environmental factors or usage patterns change, altering relative
|
||||
thread timings.</p>
|
||||
|
||||
<p>The decision to exclude event variables from Boost.Threads has been
|
||||
surprising to some Windows programmers. They have written programs
|
||||
which work using event variables, and wonder what the problem is. It
|
||||
seems similar to the "goto considered harmful" controversy of
|
||||
30 years ago. It isn't that events, like gotos, can't be made
|
||||
to work, but rather that virtually all programs using alternatives will
|
||||
be easier to write, debug, read, maintain, and be less likely to
|
||||
contain latent defects.</p>
|
||||
|
||||
<p>[Rationale provided by Beman Dawes]</p>
|
||||
<hr>
|
||||
|
||||
<p>Revised
|
||||
<!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan -->05 November, 2001<!--webbot bot="Timestamp" endspan i-checksum="39359" --></p>
|
||||
|
||||
<p><i>© Copyright <a href="mailto:williamkempf@hotmail.com">
|
||||
William E. Kempf</a> 2001 all rights reserved.</i></p>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -1,368 +0,0 @@
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content=
|
||||
"text/html; charset=iso-8859-1">
|
||||
<meta name="keywords" content="threads, BTL, thread library, C++">
|
||||
<link rel="stylesheet" type="text/css" href="styles.css">
|
||||
|
||||
<title>Boost.Threads, recursive_mutex</title>
|
||||
</head>
|
||||
|
||||
<body bgcolor="#FFFFFF" link="#0000FF" vlink="#800080">
|
||||
<table summary="header" border="0" cellpadding="7" cellspacing="0" width="100%">
|
||||
<tr>
|
||||
<td valign="top" width="300">
|
||||
<h3><img src="../../../c++boost.gif" alt="C++ Boost" width=
|
||||
"277" height="86"></h3>
|
||||
</td>
|
||||
|
||||
<td valign="top">
|
||||
<h1 align="center">Boost.Threads</h1>
|
||||
|
||||
<h2 align="center">recursive_mutex<br>
|
||||
recursive_try_mutex<br>
|
||||
recursive_timed_mutex</h2>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<hr>
|
||||
|
||||
<p><a href="#Introduction">Introduction</a><br>
|
||||
<a href="#Header">Header</a><br>
|
||||
<a href="#recursive_mutex Synopsis">Class recursive_mutex
|
||||
Synopsis</a><br>
|
||||
<a href="#recursive_mutex Members">Class recursive_mutex
|
||||
Members</a><br>
|
||||
<a href="#recursive_try_mutex Synopsis">Class recursive_try_mutex
|
||||
Synopsis</a><br>
|
||||
<a href="#recursive_try_mutex Members">Class recursive_try_mutex
|
||||
Members</a><br>
|
||||
<a href="#recursive_timed_mutex Synopsis">Class recursive_timed_mutex
|
||||
Synopsis</a><br>
|
||||
<a href="#recursive_timed_mutex Members">Class recursive_timed_mutex
|
||||
Members</a><br>
|
||||
<a href="#Example">Example</a></p>
|
||||
|
||||
<h2><a name="Introduction">Introduction</a></h2>
|
||||
|
||||
<p>The <code>recursive_mutex</code>, <code>recursive_try_mutex</code>
|
||||
and <code>recursive_timed_mutex</code> classes define full featured
|
||||
models of the <a href="mutex_concept.html#Mutex">Mutex</a>, <a href=
|
||||
"mutex_concept.html#TryMutex">TryMutex</a> and <a href=
|
||||
"mutex_concept.html#TimedMutex">TimedMutex</a> concepts with recursive
|
||||
locking semantics. These types should be used to synchronize access to
|
||||
shared resources when recursive locking by a single thread is likely to
|
||||
occur. A good example for this is when a class supplies "internal
|
||||
synchronization" to ensure <a href="definitions.html#Thread-safe">
|
||||
thread-safety</a> and a function of the class may have to call other
|
||||
functions of the class which also attempt to lock the mutex. For
|
||||
recursive locking mechanics, see <a href="mutex.html">mutexes</a>.</p>
|
||||
|
||||
<p>Each class supplies one or more typedefs for lock types which model
|
||||
matching lock concepts. For the best possible performance you should
|
||||
use the mutex class that supports the minimum set of lock types that
|
||||
you need.</p>
|
||||
|
||||
<table summary="lock types" border="1" cellpadding="5">
|
||||
<tr>
|
||||
<td><b>Mutex Class</b></td>
|
||||
|
||||
<td><b>Lock name</b></td>
|
||||
|
||||
<td><b>Implementation defined Lock Type</b></td>
|
||||
|
||||
<td><b>Lock Concept</b></td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td valign="top"><a href="#recursive_mutex Synopsis"><code>
|
||||
recursive_mutex</code></a></td>
|
||||
|
||||
<td valign="middle"><code>scoped_lock</code></td>
|
||||
|
||||
<td valign="middle"><a href="scoped_lock.html"><code>
|
||||
detail::thread::scoped_lock<recursive_mutex></code></a></td>
|
||||
|
||||
<td valign="middle"><a href="lock_concept.html#ScopedLock">
|
||||
ScopedLock</a></td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td valign="top"><code><a href="#recursive_try_mutex Synopsis">
|
||||
recursive_try_mutex</a></code></td>
|
||||
|
||||
<td valign="middle"><code>scoped_lock<br>
|
||||
scoped_try_lock</code></td>
|
||||
|
||||
<td valign="middle"><a href="scoped_lock.html"><code>
|
||||
detail::thread::scoped_lock<recursive_try_mutex><br>
|
||||
</code></a> <code><a href="scoped_try_lock.html">
|
||||
detail::thread::scoped_try_lock<recursive_try_mutex></a></code></td>
|
||||
|
||||
<td valign="middle"><a href="lock_concept.html#ScopedLock">
|
||||
ScopedLock</a><br>
|
||||
<a href="lock_concept.html#ScopedTryLock">
|
||||
ScopedTryLock</a></td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td valign="top"><code><a href=
|
||||
"#recursive_timed_mutex Synopsis">
|
||||
recursive_timed_mutex</a></code> </td>
|
||||
|
||||
<td valign="middle"><code>scoped_lock<br>
|
||||
scoped_try_lock<br>
|
||||
scoped_timed_lock</code></td>
|
||||
|
||||
<td valign="middle"><a href="scoped_lock.html"><code>
|
||||
detail::thread::scoped_lock<recursive_timed_mutex></code></a><br>
|
||||
|
||||
<a href="scoped_try_lock.html"><code>
|
||||
detail::thread::scoped_try_lock<recursive_timed_mutex></code></a><br>
|
||||
|
||||
<a href="scoped_timed_lock.html"><code>
|
||||
detail::thread::scoped_timed_lock<recursive_timed_mutex></code></a></td>
|
||||
|
||||
<td valign="middle"><a href="lock_concept.html#ScopedLock">
|
||||
ScopedLock</a><br>
|
||||
<a href="lock_concept.html#ScopedTryLock">
|
||||
ScopedTryLock</a><br>
|
||||
<a href="lock_concept.html#ScopedTimedLock">
|
||||
ScopedTimedLock</a></td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<p>The <code>recursive_mutex</code>, <code>recursive_try_mutex</code>
|
||||
and <code>recursive_timed_mutex</code> employ a <code>Recursive</code>
|
||||
<a href="mutex_concept.html#LockingStrategies">locking strategy</a>, so
|
||||
attempts to recursively lock them succeed and an internal "lock
|
||||
count" is maintained. Attempts to unlock them by a thread that
|
||||
does not own a lock on them will result in a <a href="lock_error.html">
|
||||
lock_error</a> exception being thrown.</p>
|
||||
|
||||
<p>The <code>recursive_mutex</code>, <code>recursive_try_mutex</code>
|
||||
and <code>recursive_timed_mutex</code> leave the <a href=
|
||||
"mutex_concept.html#SchedulingPolicies">scheduling policy</a> as <code>
|
||||
Unspecified</code>. Programmers should assume that threads waiting for
|
||||
a lock on objects of these types acquire the lock in a random order,
|
||||
even though the specific behavior for a given platform may be
|
||||
different.</p>
|
||||
|
||||
<h2><a name="Header">Header</a></h2>
|
||||
<pre>
|
||||
#include <a href=
|
||||
"../../../boost/thread/recursive_mutex.hpp"><boost/thread/recursive_mutex.hpp></a>
|
||||
</pre>
|
||||
|
||||
<h2>Class <a name="recursive_mutex Synopsis">recursive_mutex
|
||||
Synopsis</a></h2>
|
||||
<hr>
|
||||
<pre>
|
||||
namespace boost
|
||||
{
|
||||
class recursive_mutex : private <a href=
|
||||
"../../utility/utility.htm">boost::noncopyable</a> // Exposition only.
|
||||
// Class recursive_mutex meets the <a href=
|
||||
"overview.html#NonCopyable">NonCopyable</a> requirement.
|
||||
{
|
||||
public:
|
||||
typedef <i>[implementation defined; see <a href=
|
||||
"#Introduction">Introduction</a>]</i> scoped_lock;
|
||||
|
||||
recursive_mutex();
|
||||
~recursive_mutex();
|
||||
};
|
||||
}
|
||||
</pre>
|
||||
|
||||
<h2>Class <a name="recursive_mutex Members">recursive_mutex
|
||||
Members</a></h2>
|
||||
<hr>
|
||||
|
||||
<h3>Constructor</h3>
|
||||
<pre>
|
||||
recursive_mutex();
|
||||
</pre>
|
||||
|
||||
<p><b>Postconditions:</b> <code>*this</code> is in the unlocked
|
||||
state.</p>
|
||||
<hr>
|
||||
|
||||
<h3>Destructor</h3>
|
||||
<pre>
|
||||
~recursive_mutex();
|
||||
</pre>
|
||||
|
||||
<p><b>Requires:</b> <code>*this</code> is in the unlocked state.</p>
|
||||
|
||||
<p><b>Effects:</b> Destroys <code>*this</code>.</p>
|
||||
|
||||
<p><b>Dangers:</b> Destruction of a locked mutex is a serious
|
||||
programming error resulting in undefined behavior such as a program
|
||||
crash..</p>
|
||||
<hr>
|
||||
|
||||
<h2>Class <a name="recursive_try_mutex Synopsis">recursive_try_mutex
|
||||
Synopsis</a></h2>
|
||||
<pre>
|
||||
namespace boost
|
||||
{
|
||||
class recursive_try_mutex : private boost::noncopyable // Exposition only.
|
||||
// Class recursive_try_mutex meets the <a href=
|
||||
"overview.html#NonCopyable">NonCopyable</a> requirement.
|
||||
{
|
||||
public:
|
||||
typedef <i>[implementation defined; see <a href=
|
||||
"#Introduction">Introduction</a>]</i> scoped_lock;
|
||||
typedef <i>[implementation defined; see <a href=
|
||||
"#Introduction">Introduction</a>]</i> scoped_try_lock;
|
||||
|
||||
recursive_try_mutex();
|
||||
~recursive_try_mutex();
|
||||
};
|
||||
}
|
||||
</pre>
|
||||
|
||||
<h2>Class <a name="recursive_try_mutex Members">recursive_try_mutex
|
||||
Members</a></h2>
|
||||
<hr>
|
||||
|
||||
<h3>Constructor</h3>
|
||||
<pre>
|
||||
recursive_try_mutex();
|
||||
</pre>
|
||||
|
||||
<p><b>Postconditions:</b> <code>*this</code> is in the unlocked
|
||||
state.</p>
|
||||
<hr>
|
||||
|
||||
<h3>Destructor</h3>
|
||||
<pre>
|
||||
~recursive_try_mutex();
|
||||
</pre>
|
||||
|
||||
<p><b>Requires:</b> <code>*this</code> is in the unlocked state.</p>
|
||||
|
||||
<p><b>Effects:</b> Destroys <code>*this</code>.</p>
|
||||
|
||||
<p><b>Dangers:</b> Destruction of a locked mutex is a serious
|
||||
programming error resulting in undefined behavior such as a program
|
||||
crash..</p>
|
||||
<hr>
|
||||
|
||||
<h2>Class <a name="recursive_timed_mutex Synopsis">
|
||||
recursive_timed_mutex Synopsis</a></h2>
|
||||
<pre>
|
||||
namespace boost
|
||||
{
|
||||
class recursive_timed_mutex : private boost::noncopyable // Exposition only.
|
||||
// Class recursive_timed_mutex meets the <a href=
|
||||
"overview.html#NonCopyable">NonCopyable</a> requirement.
|
||||
{
|
||||
public:
|
||||
typedef <i>[implementation defined; see <a href=
|
||||
"#Introduction">Introduction</a>]</i> scoped_lock;
|
||||
typedef <i>[implementation defined; see <a href=
|
||||
"#Introduction">Introduction</a>]</i> scoped_try_lock;
|
||||
typedef <i>[implementation defined; see <a href=
|
||||
"#Introduction">Introduction</a>]</i> scoped_timed_lock;
|
||||
|
||||
recursive_timed_mutex();
|
||||
~recursive_timed_mutex();
|
||||
};
|
||||
}
|
||||
</pre>
|
||||
|
||||
<h2>Class <a name="recursive_timed_mutex Members">recursive_timed_mutex
|
||||
Members</a></h2>
|
||||
<hr>
|
||||
|
||||
<h3>Constructor</h3>
|
||||
<pre>
|
||||
recursive_timed_mutex();
|
||||
</pre>
|
||||
|
||||
<p><b>Postconditions:</b> <code>*this</code> is in the unlocked
|
||||
state.</p>
|
||||
<hr>
|
||||
|
||||
<h3>Destructor</h3>
|
||||
<pre>
|
||||
~recursive_timed_mutex();
|
||||
</pre>
|
||||
|
||||
<p><b>Requires:</b> <code>*this</code> is in the unlocked state.</p>
|
||||
|
||||
<p><b>Effects:</b> Destroys <code>*this</code>.</p>
|
||||
|
||||
<p><b>Dangers:</b> Destruction of a locked mutex is a serious
|
||||
programming error resulting in undefined behavior such as a program
|
||||
crash..</p>
|
||||
<hr>
|
||||
|
||||
<h2><a name="Example">Example</a> Usage</h2>
|
||||
<pre>
|
||||
#include <a href=
|
||||
"../../../boost/thread/recursive_mutex.hpp"><boost/thread/recursive_mutex.hpp></a>
|
||||
#include <a href=
|
||||
"../../../boost/thread/thread.hpp"><boost/thread/thread.hpp></a>
|
||||
#include <iostream>
|
||||
|
||||
class counter
|
||||
{
|
||||
public:
|
||||
counter() : count(0) { }
|
||||
|
||||
int add(int val) {
|
||||
boost::recursive_mutex::scoped_lock scoped_lock(mutex);
|
||||
count += val;
|
||||
return count;
|
||||
}
|
||||
int increment() {
|
||||
boost::recursive_mutex::scoped_lock scoped_lock(mutex);
|
||||
return add(1);
|
||||
}
|
||||
|
||||
private:
|
||||
boost::recursive_mutex mutex;
|
||||
int count;
|
||||
};
|
||||
|
||||
counter c;
|
||||
|
||||
void change_count(void*)
|
||||
{
|
||||
std::cout << "count == " << c.increment() << std::endl;
|
||||
}
|
||||
|
||||
int main(int, char*[])
|
||||
{
|
||||
const int num_threads=4;
|
||||
|
||||
boost::thread_group threads;
|
||||
for (int i=0; i < num_threads; ++i)
|
||||
threads.create_thread(&change_count, 0);
|
||||
|
||||
threads.join_all();
|
||||
|
||||
return 0;
|
||||
}
|
||||
</pre>
|
||||
|
||||
<p>The output is:</p>
|
||||
<pre>
|
||||
count == 1
|
||||
count == 2
|
||||
count == 3
|
||||
count == 4
|
||||
</pre>
|
||||
<hr>
|
||||
|
||||
<p>Revised
|
||||
<!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan -->05 November, 2001<!--webbot bot="Timestamp" endspan i-checksum="39359" --></p>
|
||||
|
||||
<p><i>© Copyright <a href="mailto:williamkempf@hotmail.com">
|
||||
William E. Kempf</a> 2001 all rights reserved.</i></p>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -1,134 +0,0 @@
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
||||
<meta name="keywords" content="threads, BTL, thread library, C++">
|
||||
<title>Boost.Threads, atomic_t</title>
|
||||
</head>
|
||||
|
||||
<body bgcolor="#FFFFFF" link="#0000FF" vlink="#800080">
|
||||
|
||||
<table border="0" cellpadding="7" cellspacing="0" width="100%">
|
||||
<tr>
|
||||
<td valign="top" width="300">
|
||||
<h3><img src="../../../c++boost.gif" alt="C++ Boost" width="277" height="86"></h3>
|
||||
</td>
|
||||
<td valign="top">
|
||||
<h1 align="center">Boost.Threads</h1>
|
||||
<h2 align="center">atomic_t</h2>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<hr>
|
||||
|
||||
<h2>Header</h2>
|
||||
|
||||
<p>The <tt>atomic_t</tt> class defines an "atomic integer" type. This class should be used
|
||||
to perform thread safe operations on an integral type with out the overhead of locks. Only
|
||||
a limited set of integer operations are available with an <tt>atomic_t</tt> instance.</p>
|
||||
|
||||
<pre>
|
||||
#include <boost/thread/atomic.hpp>
|
||||
</pre>
|
||||
|
||||
<h2>Public Interface</h2>
|
||||
|
||||
<pre>
|
||||
class atomic_t
|
||||
{
|
||||
public:
|
||||
typedef <b>implementation defined</b> value_type;
|
||||
|
||||
explicit atomic_t(value_type val=0);
|
||||
};
|
||||
|
||||
atomic_t::value_type read(const atomic_t& x);
|
||||
atomic_t::value_type increment(atomic_t& x);
|
||||
atomic_t::value_type decrement(atomic_t& x);
|
||||
atomic_t::value_type swap(atomic_t& x, atomic_t::value_type y);
|
||||
atomic_t::value_type compare_swap(atomic_t& x, atomic_t::value_type y, atomic_t::value_type z);
|
||||
</pre>
|
||||
|
||||
<h3>Constructor</h3>
|
||||
|
||||
<pre>
|
||||
atomic_t(atomic_t::value_type val=0);
|
||||
</pre>
|
||||
|
||||
<p>Constructs an <tt>atomic_t</tt> and sets its value to <tt>val</tt>.</p>
|
||||
|
||||
<h3>read</h3>
|
||||
|
||||
<pre>
|
||||
atomic_t::value_type read(const atomic_t& x);
|
||||
</pre>
|
||||
|
||||
<p>Gets the current value of <tt>x</tt>.</p>
|
||||
|
||||
<h3>increment</h3>
|
||||
|
||||
<pre>
|
||||
atomic_t::value_type increment(atomic_t& x);
|
||||
</pre>
|
||||
|
||||
<p>Increments <tt>x</tt> and returns a value <tt>< 0</tt> if the result is less than 0,
|
||||
<tt>> 0</tt> if the result is greater than 0 and <tt>== 0</tt> if the result is equal to
|
||||
0.</p>
|
||||
|
||||
<h3>decrement</h3>
|
||||
|
||||
<pre>
|
||||
atomic_t::value_type decrement(atomic_t& x);
|
||||
</pre>
|
||||
|
||||
<p>Decrements <tt>x</tt> and returns a value <tt>< 0</tt> if the result is less than 0,
|
||||
<tt>> 0</tt> if the result is greater than 0 and <tt>== 0</tt> if the result is equal to
|
||||
0.</p>
|
||||
|
||||
<h3>swap</h3>
|
||||
|
||||
<pre>
|
||||
atomic_t::value_type swap(atomic_t& x, atomic_t::value_type y);
|
||||
</pre>
|
||||
|
||||
<p>Assigns the value of <tt>y</tt> to <tt>x</tt> and returns the value of <tt>x</tt> prior
|
||||
to the swap.</p>
|
||||
|
||||
<h3>compare_swap</h3>
|
||||
|
||||
<pre>
|
||||
atomic_t::value_type compare_swap(atomic_t& x, atomic_t::value_type y, atomic_t::value_type z);
|
||||
</pre>
|
||||
|
||||
<p>Compares the value of <tt>z</tt> to the value of <tt>x</tt> and if equal sets the value of
|
||||
<tt>x</tt> to the value of <tt>y</tt> and returns the value of <tt>x</tt> prior to the swap.</p>
|
||||
|
||||
<h2>Example Usage</h2>
|
||||
|
||||
<pre>
|
||||
#include <boost/thread/atomic.hpp>
|
||||
#include <boost/test/test_tools.hpp>
|
||||
|
||||
int test_main(int, char*[])
|
||||
{
|
||||
boost::atomic_t a;
|
||||
BOOST_TEST_VERIFY(boost::read(a) == 0);
|
||||
BOOST_TEST_VERIFY(boost::increment(a) > 0);
|
||||
BOOST_TEST_VERIFY(boost::decrement(a) == 0);
|
||||
BOOST_TEST_VERIFY(boost::swap(a, 1) == 0);
|
||||
BOOST_TEST_VERIFY(boost::swap(a, 2, 0) == 1);
|
||||
BOOST_TEST_VERIFY(boost::read(a) == 1);
|
||||
}
|
||||
</pre>
|
||||
|
||||
<hr>
|
||||
|
||||
<p>Revised <!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan -->05 November, 2001<!--webbot bot="Timestamp" endspan i-checksum="39359" -->
|
||||
</p>
|
||||
|
||||
<p><i>© Copyright <a href="mailto:williamkempf@hotmail.com">William E. Kempf</a>
|
||||
2001 all rights reserved.</i></p>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
@@ -1,227 +0,0 @@
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content=
|
||||
"text/html; charset=iso-8859-1">
|
||||
<meta name="keywords" content="threads, BTL, thread library, C++">
|
||||
<link rel="stylesheet" type="text/css" href="styles.css">
|
||||
|
||||
<title>Boost.Threads, scoped_lock</title>
|
||||
</head>
|
||||
|
||||
<body bgcolor="#FFFFFF" link="#0000FF" vlink="#800080">
|
||||
<table summary="header" border="0" cellpadding="7" cellspacing="0"
|
||||
width="100%">
|
||||
<tr>
|
||||
<td valign="top" width="300">
|
||||
<h3><img src="../../../c++boost.gif" alt="C++ Boost" width=
|
||||
"277" height="86"></h3>
|
||||
</td>
|
||||
|
||||
<td valign="top">
|
||||
<h1 align="center">Boost.Threads</h1>
|
||||
|
||||
<h2 align="center">scoped_lock</h2>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<hr>
|
||||
|
||||
<p><a href="#Introduction">Introduction</a><br>
|
||||
<a href="#Header">Header</a><br>
|
||||
<a href="#Synopsis">Synopsis</a><br>
|
||||
<a href="#Members">Members</a><br>
|
||||
<a href="#Example">Example</a></p>
|
||||
|
||||
<h2><a name="Introduction">Introduction</a></h2>
|
||||
|
||||
<p>This class template defines a generic lock type which meets the <a
|
||||
href="lock_concept.html#ScopedLock">ScopedLock</a> requirements. The <a
|
||||
href="mutex.html">mutex</a>, <a href="mutex.html">try_mutex</a>, <a
|
||||
href="mutex.html">timed_mutex</a>, <a href="recursive_mutex.html">
|
||||
recursive_mutex</a>, <a href="recursive_mutex.html">
|
||||
recursive_try_mutex</a> and <a href="recursive_mutex.html">
|
||||
recursive_timed_mutex</a> classes all use this template to define their
|
||||
<code>scoped_lock</code> types.</p>
|
||||
|
||||
<p>Like all the <b>Boost.Threads</b> <a href="lock_concept.html">lock
|
||||
models</a>, <code>scoped_lock</code> objects are meant to be
|
||||
short-lived. Objects of the class are not <a href=
|
||||
"definitions.html#thread-safe">thread-safe</a>, and so should not be
|
||||
shared between threads.</p>
|
||||
|
||||
<p>Class <code>scoped_lock</code> follows the "resource
|
||||
acquisition is initialization" idiom <a href=
|
||||
"bibliography.html#Stroustrup-00">[Stroustrup 00 14.4.1]</a> and is a
|
||||
realization of the "Scoped Locking Pattern" <a href=
|
||||
"bibliography.html#Schmidt-00">[Schmidt-00]</a>. Thus the usage is to
|
||||
let the constructor do the locking, and then let the destructor do the
|
||||
unlocking automatically at the end of the enclosing scope. The lock()
|
||||
and unlock() members are usually not explicitly called, but are
|
||||
provided to allow for complex overlapping locks of multiple
|
||||
mutexes.</p>
|
||||
|
||||
<p>The type used to instantiate the class must meet the <a href=
|
||||
"mutex_concept.html#Mutex">Mutex</a> requirements.</p>
|
||||
|
||||
<p>Although this class is an implementation detail, it is publicly
|
||||
documented here because of its importance.</p>
|
||||
|
||||
<h2><a name="Header">Header</a></h2>
|
||||
<pre>
|
||||
#include <a href=
|
||||
"../../../boost/thread/detail/lock.hpp"><boost/thread/detail/lock.hpp></a>
|
||||
<i>This header is usually not included directly by programmers
|
||||
because it is supplied by <a href=
|
||||
"../../../boost/thread/mutex.hpp"><boost/thread/mutex.hpp></a> or
|
||||
<a href=
|
||||
"../../../boost/thread/recursive_mutex.hpp"><boost/thread/recursive_mutex.hpp></a></i>
|
||||
</pre>
|
||||
|
||||
<h2><a name="Synopsis">Synopsis</a></h2>
|
||||
<pre>
|
||||
namespace boost { namespace detail { namespace thread {
|
||||
template <typename Mutex>
|
||||
class scoped_lock : private <a href=
|
||||
"../../utility/utility.htm#Class noncopyable">boost::noncopyable</a> // Exposition only.
|
||||
// Class scoped_lock meets the <a href=
|
||||
"overview.html#NonCopyable">NonCopyable</a> requirement.
|
||||
{
|
||||
public:
|
||||
typedef Mutex mutex_type;
|
||||
|
||||
explicit scoped_lock(Mutex& mx, bool initially_locked=true);
|
||||
~scoped_lock();
|
||||
|
||||
void lock();
|
||||
void unlock();
|
||||
|
||||
operator const void*() const;
|
||||
bool locked() const;
|
||||
};
|
||||
} // namespace thread
|
||||
} // namespace detail
|
||||
} // namespace boost
|
||||
</pre>
|
||||
|
||||
<h2><a name="Members">Members</a></h2>
|
||||
<hr>
|
||||
|
||||
<h3>Constructor</h3>
|
||||
<pre>
|
||||
explicit scoped_lock(Mutex& mx, bool initially_locked=true);
|
||||
</pre>
|
||||
|
||||
<p><b>Effects:</b> Associates mutex <code>mx</code> with <code>
|
||||
*this</code>. If <code>initially_locked</code> is <code>true,</code>
|
||||
calls <code>lock()</code>.</p>
|
||||
<hr>
|
||||
|
||||
<h3>Destructor</h3>
|
||||
<pre>
|
||||
~scoped_lock();
|
||||
</pre>
|
||||
|
||||
<p><b>Effects:</b> If <code>locked()</code>, calls <code>
|
||||
unlock()</code>. Destroys <code>*this</code>.</p>
|
||||
<hr>
|
||||
|
||||
<h3>lock</h3>
|
||||
<pre>
|
||||
void lock();
|
||||
</pre>
|
||||
|
||||
<p><b>Effects:</b> If the associated mutex is already locked by another
|
||||
lock in the current thread, the effects depend on the locking strategy
|
||||
of the associated mutex, as shown in the following table:</p>
|
||||
|
||||
<table summary="lock effects" border="1" cellpadding="5">
|
||||
<tr>
|
||||
<td><i><a href="mutex_concept.html#LockingStrategies">Locking
|
||||
Strategy</a><br>
|
||||
of associated mutex</i></td>
|
||||
|
||||
<td><i>Effect if associated mutex is already locked by the
|
||||
current thread</i></td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td>Recursive</td>
|
||||
|
||||
<td>As if an additional lock were added to the mutex.</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td>Checked</td>
|
||||
|
||||
<td>Throws <a href="lock_error.html">lock_error</a>.</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td>Unchecked</td>
|
||||
|
||||
<td>Undefined behavior [<a href="bibliography.html#ISO">ISO</a>
|
||||
1.3.12] (but typically, <a href="definitions.html#Deadlock">
|
||||
deadlock</a>.)</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<p>If the associated mutex is already locked by some other thread,
|
||||
places the current thread in the <a href="definitions.html#State">
|
||||
Blocked</a> state until the associated mutex is unlocked, after which
|
||||
the current thread is placed in the <a href="definitions.html#State">
|
||||
Ready</a> state, eventually to be returned to the <a href=
|
||||
"definitions.html#State">Running</a> state.</p>
|
||||
|
||||
<p><b>Postcondition:</b> locked()</p>
|
||||
|
||||
<p><b>Throws:</b> <a href="lock_error.html">lock_error</a> if <code>
|
||||
locked()</code> or as indicated in <b>Effects</b>.</p>
|
||||
<hr>
|
||||
|
||||
<h3>unlock</h3>
|
||||
<pre>
|
||||
void unlock();
|
||||
</pre>
|
||||
|
||||
<p><b>Effects:</b> Unlocks the associated mutex.</p>
|
||||
|
||||
<p><b>Throws:</b> <a href="lock_error.html">lock_error</a> if <code>
|
||||
!locked()</code>.</p>
|
||||
<hr>
|
||||
|
||||
<h3>const void* Conversion</h3>
|
||||
<pre>
|
||||
operator const void*() const;
|
||||
</pre>
|
||||
|
||||
<p><b>Returns:</b> If the associated mutex is currently locked, a value
|
||||
convertible to <code>true</code>, else a value convertible to <code>
|
||||
false</code>.</p>
|
||||
|
||||
<p><b>Rationale:</b> A <code>const void*</code> conversion is
|
||||
considered safer than a conversion to <code>bool</code>.</p>
|
||||
<hr>
|
||||
|
||||
<h3>locked</h3>
|
||||
<pre>
|
||||
bool locked() const;
|
||||
</pre>
|
||||
|
||||
<p><b>Returns:</b> <code>this->operator const void*() !=
|
||||
0</code>.</p>
|
||||
<hr>
|
||||
|
||||
<h2><a name="Example">Example</a> Usage</h2>
|
||||
|
||||
<p>See the example given in the documentation for the <a href=
|
||||
"mutex.html">mutex</a> class.</p>
|
||||
<hr>
|
||||
|
||||
<p>Revised
|
||||
<!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan -->05 November, 2001<!--webbot bot="Timestamp" endspan i-checksum="39359" --></p>
|
||||
|
||||
<p><i>© Copyright <a href="mailto:williamkempf@hotmail.com">
|
||||
William E. Kempf</a> 2001 all rights reserved.</i></p>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
463
doc/scoped_thread.qbk
Normal file
463
doc/scoped_thread.qbk
Normal file
@@ -0,0 +1,463 @@
|
||||
[/
|
||||
(C) Copyright 2008-9 Anthony Williams.
|
||||
(C) Copyright 12 Vicente J. Botet Escriba.
|
||||
Distributed under the Boost Software License, Version 1.0.
|
||||
(See accompanying file LICENSE_1_0.txt or copy at
|
||||
http://www.boost.org/LICENSE_1_0.txt).
|
||||
]
|
||||
|
||||
[section:ScopedThreads Scoped Threads]
|
||||
|
||||
[heading Synopsis]
|
||||
|
||||
//#include <boost/thread/scoped_thread.hpp>
|
||||
|
||||
struct detach;
|
||||
struct join_if_joinable;
|
||||
struct interrupt_and_join_if_joinable;
|
||||
template <class CallableThread = join_if_joinable>
|
||||
class strict_scoped_thread;
|
||||
template <class CallableThread = join_if_joinable>
|
||||
class scoped_thread;
|
||||
void swap(scoped_thread& lhs,scoped_thread& rhs) noexcept;
|
||||
|
||||
[section:motovation Motivation]
|
||||
Based on the scoped_thread class defined in C++ Concurrency in Action Boost.Thread defines a thread wrapper class that instead of calling terminate if the thread is joinable on destruction, call a specific action given as template parameter.
|
||||
|
||||
While the scoped_thread class defined in C++ Concurrency in Action is closer to strict_scoped_thread class that doesn't allows any change in the wrapped thread, Boost.Thread provides a class scoped_thread that provides the same non-deprecated interface than __thread.
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:tutorial Tutorial]
|
||||
|
||||
Scoped Threads are wrappers around a thread that allows the user to state what to do at destruction time. One of the common uses is to join the thread at destruction time so this is the default behavior. This is the single difference respect to a thread. While thread call std::terminate() on the destructor is the thread is joinable, strict_scoped_thread<> or scoped_thread<> join the thread if joinable.
|
||||
|
||||
The difference between strict_scoped_thread and scoped_thread is that the strict_scoped_thread hides completely the owned thread and so the user can do nothing with the owned thread other than the specific action given as parameter, while scoped_thread provide the same interface than __thread and forwards all the operations.
|
||||
|
||||
boost::strict_scoped_thread<> t1((boost::thread(F)));
|
||||
boost::strict_scoped_thread<> t2((boost::thread(F)));
|
||||
t2.interrupt();
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:thread_functors Free Thread Functors]
|
||||
|
||||
//#include <boost/thread/scoped_thread.hpp>
|
||||
|
||||
struct detach;
|
||||
struct join_if_joinable;
|
||||
struct interrupt_and_join_if_joinable;
|
||||
|
||||
|
||||
[section:detach Functor `detach`]
|
||||
|
||||
struct detach
|
||||
{
|
||||
void operator()(thread& t)
|
||||
{
|
||||
t.detach();
|
||||
}
|
||||
};
|
||||
[endsect]
|
||||
[section:join_if_joinable Functor `join_if_joinable`]
|
||||
|
||||
struct join_if_joinable
|
||||
{
|
||||
void operator()(thread& t)
|
||||
{
|
||||
if (t.joinable())
|
||||
{
|
||||
t.join();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:interrupt_and_join_if_joinable Functor `interrupt_and_join_if_joinable`]
|
||||
|
||||
struct interrupt_and_join_if_joinable
|
||||
{
|
||||
void operator()(thread& t)
|
||||
{
|
||||
t.interrupt();
|
||||
if (t.joinable())
|
||||
{
|
||||
t.join();
|
||||
}
|
||||
}
|
||||
};
|
||||
[endsect]
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:strict_scoped_thread Class `strict_scoped_thread`]
|
||||
|
||||
// #include <boost/thread/scoped_thread.hpp>
|
||||
|
||||
template <class CallableThread = join_if_joinable>
|
||||
class strict_scoped_thread
|
||||
{
|
||||
thread t_; // for exposition purposes only
|
||||
public:
|
||||
|
||||
strict_scoped_thread(strict_scoped_thread const&) = delete;
|
||||
strict_scoped_thread& operator=(strict_scoped_thread const&) = delete;
|
||||
|
||||
explicit strict_scoped_thread(thread&& t) noexcept;
|
||||
|
||||
~strict_scoped_thread();
|
||||
|
||||
};
|
||||
|
||||
|
||||
RAI __thread wrapper adding a specific destroyer allowing to master what can be done at destruction time.
|
||||
|
||||
CallableThread: A callable `void(thread&)`.
|
||||
|
||||
The default is a `join_if_joinable`.
|
||||
|
||||
|
||||
`std/boost::thread` destructor terminates the program if the __thread is not joinable.
|
||||
This wrapper can be used to join the thread before destroying it seems a natural need.
|
||||
|
||||
[heading Example]
|
||||
|
||||
boost::strict_scoped_thread<> t((boost::thread(F)));
|
||||
|
||||
[section:default_constructor Default Constructor]
|
||||
|
||||
explicit strict_scoped_thread(thread&& t) noexcept;
|
||||
|
||||
[variablelist
|
||||
|
||||
[[Effects:] [move the thread to own `t_`]]
|
||||
|
||||
[[Throws:] [Nothing]]
|
||||
|
||||
]
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:destructor Destructor]
|
||||
|
||||
~strict_scoped_thread();
|
||||
|
||||
[variablelist
|
||||
|
||||
[[Effects:] [Equivalent to `CallableThread()(t_)`. ]]
|
||||
|
||||
[[Throws:] [Nothing: The `CallableThread()(t_)` should not throw when joining the thread as the scoped variable is on a scope outside the thread function.]]
|
||||
|
||||
|
||||
]
|
||||
|
||||
[endsect]
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:scoped_thread Class `scoped_thread`]
|
||||
|
||||
#include <boost/thread/scoped_thread.hpp>
|
||||
|
||||
class scoped_thread
|
||||
{
|
||||
thread t_; // for exposition purposes only
|
||||
public:
|
||||
scoped_thread() noexcept;
|
||||
scoped_thread(const scoped_thread&) = delete;
|
||||
scoped_thread& operator=(const scoped_thread&) = delete;
|
||||
|
||||
explicit scoped_thread(thread&& th) noexcept;
|
||||
|
||||
~scoped_thread();
|
||||
|
||||
// move support
|
||||
scoped_thread(scoped_thread && x) noexcept;
|
||||
scoped_thread& operator=(scoped_thread && x) noexcept;
|
||||
|
||||
void swap(scoped_thread& x) noexcept;
|
||||
|
||||
typedef thread::id id;
|
||||
|
||||
id get_id() const noexcept;
|
||||
|
||||
bool joinable() const noexcept;
|
||||
void join();
|
||||
#ifdef BOOST_THREAD_USES_CHRONO
|
||||
template <class Rep, class Period>
|
||||
bool try_join_for(const chrono::duration<Rep, Period>& rel_time);
|
||||
template <class Clock, class Duration>
|
||||
bool try_join_until(const chrono::time_point<Clock, Duration>& t);
|
||||
#endif
|
||||
|
||||
void detach();
|
||||
|
||||
static unsigned hardware_concurrency() noexcept;
|
||||
|
||||
typedef thread::native_handle_type native_handle_type;
|
||||
native_handle_type native_handle();
|
||||
|
||||
#if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
|
||||
void interrupt();
|
||||
bool interruption_requested() const noexcept;
|
||||
#endif
|
||||
|
||||
|
||||
};
|
||||
|
||||
void swap(scoped_thread& lhs,scoped_thread& rhs) noexcept;
|
||||
|
||||
|
||||
RAI __thread wrapper adding a specific destroyer allowing to master what can be done at destruction time.
|
||||
|
||||
CallableThread: A callable void(thread&).
|
||||
The default is join_if_joinable.
|
||||
|
||||
thread std::thread destructor terminates the program if the thread is not joinable.
|
||||
Having a wrapper that can join the thread before destroying it seems a natural need.
|
||||
|
||||
Remark: `scoped_thread` is not a __thread as __thread is not designed to be derived from as a polymorphic type.
|
||||
|
||||
Anyway `scoped_thread` can be used in most of the contexts a __thread could be used as it has the
|
||||
same non-deprecated interface with the exception of the construction.
|
||||
|
||||
[heading Example]
|
||||
|
||||
boost::scoped_thread<> t((boost::thread(F)));
|
||||
t.interrupt();
|
||||
|
||||
|
||||
[section:default_constructor Default Constructor]
|
||||
|
||||
scoped_thread() noexcept;
|
||||
|
||||
[variablelist
|
||||
|
||||
[[Effects:] [Constructs a scoped_thread instance that wraps to __not_a_thread__.]]
|
||||
|
||||
[[Postconditions:] [`this->get_id()==thread::id()`]]
|
||||
|
||||
[[Throws:] [Nothing]]
|
||||
|
||||
]
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:move_constructor Move Constructor]
|
||||
|
||||
scoped_thread(scoped_thread&& other) noexcept;
|
||||
|
||||
[variablelist
|
||||
|
||||
[[Effects:] [Transfers ownership of the scoped_thread managed by `other` (if any) to the newly constructed scoped_thread instance.]]
|
||||
|
||||
[[Postconditions:] [`other.get_id()==thread::id()` and `get_id()` returns the value of `other.get_id()` prior to the construction]]
|
||||
|
||||
[[Throws:] [Nothing]]
|
||||
|
||||
]
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:move_assignment Move assignment operator]
|
||||
|
||||
scoped_thread& operator=(scoped_thread&& other) noexcept;
|
||||
|
||||
[variablelist
|
||||
|
||||
[[Effects:] [Transfers ownership of the scoped_thread managed by `other` (if
|
||||
any) to `*this`.
|
||||
|
||||
- if defined `BOOST_THREAD_DONT_PROVIDE_THREAD_MOVE_ASSIGN_CALLS_TERMINATE_IF_JOINABLE`: If there was a `scoped_thread` previously associated with `*this` then that `scoped_thread` is detached, DEPRECATED
|
||||
|
||||
- if defined `BOOST_THREAD_PROVIDES_THREAD_MOVE_ASSIGN_CALLS_TERMINATE_IF_JOINABLE`: If the `scoped_thread` is joinable calls to std::terminate.
|
||||
]]
|
||||
|
||||
[[Postconditions:] [`other->get_id()==thread::id()` and `get_id()` returns the value of `other.get_id()` prior to the assignment.]]
|
||||
|
||||
[[Throws:] [Nothing]]
|
||||
|
||||
]
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:thread_constructor Move Constructor from a __thread]
|
||||
|
||||
scoped_thread(thread&& t);
|
||||
|
||||
[variablelist
|
||||
|
||||
[[Effects:] [move the thread to own `t_`.]]
|
||||
|
||||
[[Postconditions:] [`*this.t_` refers to the newly created thread of execution and `this->get_id()!=thread::id()`.]]
|
||||
|
||||
[[Throws:] [Nothing]]
|
||||
|
||||
]
|
||||
|
||||
[endsect]
|
||||
|
||||
|
||||
[section:destructor Destructor]
|
||||
|
||||
~scoped_thread();
|
||||
|
||||
[variablelist
|
||||
|
||||
[[Effects:] [Equivalent to `CallableThread()(t_)`. ]]
|
||||
|
||||
[[Throws:] [Nothing: The `CallableThread()(t_)` should not throw when joining the thread as the scoped variable is on a scope outside the thread function.]]
|
||||
|
||||
]
|
||||
|
||||
[endsect]
|
||||
|
||||
|
||||
[section:joinable Member function `joinable()`]
|
||||
|
||||
bool joinable() const noexcept;
|
||||
|
||||
[variablelist
|
||||
|
||||
[[Returns:] [Equivalent to return t_.joinable().]]
|
||||
|
||||
[[Throws:] [Nothing]]
|
||||
|
||||
]
|
||||
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:join Member function `join()`]
|
||||
|
||||
void join();
|
||||
|
||||
[variablelist
|
||||
|
||||
[[Effects:] [Equivalent to t_.join().]]
|
||||
|
||||
]
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:try_join_for Member function `try_join_for()`]
|
||||
|
||||
template <class Rep, class Period>
|
||||
bool try_join_for(const chrono::duration<Rep, Period>& rel_time);
|
||||
|
||||
[variablelist
|
||||
|
||||
[[Effects:] [Equivalent to return `t_.try_join_for(rel_time)`.]]
|
||||
|
||||
]
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:try_join_until Member function `try_join_until()`]
|
||||
|
||||
template <class Clock, class Duration>
|
||||
bool try_join_until(const chrono::time_point<Clock, Duration>& abs_time);
|
||||
|
||||
[variablelist
|
||||
|
||||
[[Effects:] [Equivalent to return `t_.try_join_until(abs_time)`.]]
|
||||
|
||||
]
|
||||
|
||||
[endsect]
|
||||
|
||||
|
||||
|
||||
[section:detach Member function `detach()`]
|
||||
|
||||
void detach();
|
||||
|
||||
[variablelist
|
||||
|
||||
[[Effects:] [Equivalent to `t_.detach()`.]]
|
||||
|
||||
]
|
||||
|
||||
[endsect]
|
||||
|
||||
|
||||
[section:get_id Member function `get_id()`]
|
||||
|
||||
thread::id get_id() const noexcept;
|
||||
|
||||
[variablelist
|
||||
|
||||
[[Effects:] [Equivalent to return `t_.get_id()`.]]
|
||||
|
||||
]
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:interrupt Member function `interrupt()`]
|
||||
|
||||
void interrupt();
|
||||
|
||||
[variablelist
|
||||
|
||||
[[Effects:] [Equivalent to `t_.interrupt()`.]]
|
||||
|
||||
]
|
||||
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:hardware_concurrency Static member function `hardware_concurrency()`]
|
||||
|
||||
unsigned hardware_concurrency() noexecpt;
|
||||
|
||||
[variablelist
|
||||
|
||||
[[Effects:] [Equivalent to return `thread::hardware_concurrency()`.]]
|
||||
|
||||
]
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:nativehandle Member function `native_handle()`]
|
||||
|
||||
typedef thread::native_handle_type native_handle_type;
|
||||
native_handle_type native_handle();
|
||||
|
||||
[variablelist
|
||||
|
||||
[[Effects:] [Equivalent to return `t_.native_handle()`.]]
|
||||
|
||||
]
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:swap Member function `swap()`]
|
||||
|
||||
void swap(scoped_thread& other) noexcept;
|
||||
|
||||
[variablelist
|
||||
|
||||
[[Effects:] [Equivalent `t_.swap(other.t_)`.]]
|
||||
|
||||
]
|
||||
|
||||
[endsect]
|
||||
|
||||
|
||||
|
||||
[endsect]
|
||||
[section:non_member_swap Non-member function `swap(scoped_thread&,scoped_thread&)`]
|
||||
|
||||
#include <boost/thread/scoped_thread.hpp>
|
||||
|
||||
void swap(scoped_thread& lhs,scoped_thread& rhs) noexcept;
|
||||
|
||||
[variablelist
|
||||
|
||||
[[Effects:] [`lhs.swap(rhs)`.]]
|
||||
|
||||
]
|
||||
|
||||
[endsect]
|
||||
[endsect]
|
||||
@@ -1,267 +0,0 @@
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content=
|
||||
"text/html; charset=iso-8859-1">
|
||||
<meta name="keywords" content="threads, BTL, thread library, C++">
|
||||
<link rel="stylesheet" type="text/css" href="styles.css">
|
||||
|
||||
<title>Boost.Threads, scoped_timed_lock</title>
|
||||
</head>
|
||||
|
||||
<body bgcolor="#FFFFFF" link="#0000FF" vlink="#800080">
|
||||
<table summary="header" border="0" cellpadding="7" cellspacing="0"
|
||||
width="100%">
|
||||
<tr>
|
||||
<td valign="top" width="300">
|
||||
<h3><img src="../../../c++boost.gif" alt="C++ Boost" width=
|
||||
"277" height="86"></h3>
|
||||
</td>
|
||||
|
||||
<td valign="top">
|
||||
<h1 align="center">Boost.Threads</h1>
|
||||
|
||||
<h2 align="center">scoped_timed_lock</h2>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<hr>
|
||||
|
||||
<p><a href="#Introduction">Introduction</a><br>
|
||||
<a href="#Header">Header</a><br>
|
||||
<a href="#Synopsis">Synopsis</a><br>
|
||||
<a href="#Members">Members</a><br>
|
||||
<a href="#Example">Example</a></p>
|
||||
|
||||
<h2><a name="Introduction">Introduction</a></h2>
|
||||
|
||||
<p>This class template defines a generic lock type which meets the <a
|
||||
href="lock_concept.html#ScopedTimedLock">ScopedTimedLock</a>
|
||||
requirements. The <a href="mutex.html">timed_mutex</a> and <a href=
|
||||
"recursive_mutex.html">recursive_timed_mutex</a> classes use this
|
||||
template to define their <code>scoped_timed_lock</code> types.</p>
|
||||
|
||||
<p>Like all the <b>Boost.Threads</b> <a href="lock_concept.html">lock
|
||||
models</a>, <code>scoped_timed_lock</code> objects are meant to be
|
||||
short-lived. Objects of the class are not <a href=
|
||||
"definitions.html#thread-safe">thread-safe</a>, and so should not be
|
||||
shared between threads.</p>
|
||||
|
||||
<p>Class <code>scoped_timed_lock</code> follows the "resource
|
||||
acquisition is initialization" idiom <a href=
|
||||
"bibliography.html#Stroustrup-00">[Stroustrup 00 14.4.1]</a> and is a
|
||||
realization of the "Scoped Locking Pattern" <a href=
|
||||
"bibliography.html#Schmidt-00">[Schmidt-00]</a>. Thus the usage is to
|
||||
let the constructor do the locking, and then let the destructor do the
|
||||
unlocking automatically at the end of the enclosing scope. The lock()
|
||||
and unlock() members are usually not explicitly called, but are
|
||||
provided to allow for complex overlapping locks of multiple
|
||||
mutexes.</p>
|
||||
|
||||
<p>The type used to instantiate the class must meet the <a href=
|
||||
"mutex_concept.html#TimedMutex">TimedMutex</a> requirements.</p>
|
||||
|
||||
<p>Although this class is an implementation detail, it is publicly
|
||||
documented here because of its importance.</p>
|
||||
|
||||
<h2><a name="Header">Header</a></h2>
|
||||
<pre>
|
||||
#include <a href=
|
||||
"../../../boost/thread/detail/lock.hpp"><boost/thread/detail/lock.hpp></a>
|
||||
<i>This header is usually not included directly by programmers
|
||||
because it is supplied by <a href=
|
||||
"../../../boost/thread/mutex.hpp"><boost/thread/mutex.hpp></a> or
|
||||
<a href=
|
||||
"../../../boost/thread/recursive_mutex.hpp"><boost/thread/recursive_mutex.hpp></a></i>
|
||||
</pre>
|
||||
|
||||
<h2><a name="Synopsis">Synopsis</a></h2>
|
||||
<pre>
|
||||
namespace boost { namespace detail { namespace thread {
|
||||
template <typename TimedMutex>
|
||||
class scoped_timed_lock : private <a href=
|
||||
"../../utility/utility.htm#Class noncopyable">boost::noncopyable</a> // Exposition only.
|
||||
// Class scoped_timed_lock meets the <a href=
|
||||
"overview.html#NonCopyable">NonCopyable</a> requirement.
|
||||
{
|
||||
public:
|
||||
typedef TimedMutex mutex_type;
|
||||
|
||||
scoped_timed_lock(TimedMutex& mx, const boost::xtime& xt);
|
||||
scoped_timed_lock(TimedMutex& mx, bool initially_locked);
|
||||
~scoped_timed_lock();
|
||||
|
||||
void lock();
|
||||
bool timed_lock(const xtime& xt);
|
||||
void unlock();
|
||||
|
||||
operator const void*() const;
|
||||
};
|
||||
} // namespace thread
|
||||
} // namesapce detail
|
||||
} // namespace boost
|
||||
</pre>
|
||||
|
||||
<h2><a name="Members">Members</a></h2>
|
||||
<hr>
|
||||
|
||||
<h3>Constructor</h3>
|
||||
<pre>
|
||||
scoped_timed_lock(TimedMutex& mx, const <a href=
|
||||
"xtime.html">xtime</a>& xt);
|
||||
</pre>
|
||||
|
||||
<p><b>Effects:</b> Associates mutex <code>mx</code> with <code>
|
||||
*this</code>. Calls <code>timed_lock</code>( <code>xt</code>)</p>
|
||||
<hr>
|
||||
<pre>
|
||||
scoped_timed_lock(TimedMutex& mx, bool initially_locked);
|
||||
</pre>
|
||||
|
||||
<p><b>Effects:</b> Associates mutex <code>mx</code> with <code>
|
||||
*this</code>. If <code>initially_locked</code> is <code>true</code>,
|
||||
calls <code>lock()</code>.</p>
|
||||
<hr>
|
||||
|
||||
<h3>Destructor</h3>
|
||||
<pre>
|
||||
~scoped_timed_lock();
|
||||
</pre>
|
||||
|
||||
<p><b>Effects:</b> If <code>locked()</code>, calls <code>
|
||||
unlock()</code>. Destroys <code>*this</code>.</p>
|
||||
<hr>
|
||||
|
||||
<h3>lock</h3>
|
||||
<pre>
|
||||
void lock();
|
||||
</pre>
|
||||
|
||||
<p><b>Effects:</b> If the associated mutex is already locked by another
|
||||
lock in the current thread, the effects depend on the locking strategy
|
||||
of the associated mutex, as shown in the following table:</p>
|
||||
|
||||
<table summary="lock effects" border="1" cellpadding="5">
|
||||
<tr>
|
||||
<td><i><a href="mutex_concept.html#LockingStrategies">Locking
|
||||
Strategy</a><br>
|
||||
of associated mutex</i></td>
|
||||
|
||||
<td><i>Effect if associated mutex is already locked by the
|
||||
current thread</i></td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td>Recursive</td>
|
||||
|
||||
<td>As if an additional lock were added to the mutex.</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td>Checked</td>
|
||||
|
||||
<td>Throws <a href="lock_error.html">lock_error</a>.</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td>Unchecked</td>
|
||||
|
||||
<td>Undefined behavior [<a href="bibliography.html#ISO">ISO</a>
|
||||
1.3.12] (but typically, <a href="definitions.html#Deadlock">
|
||||
deadlock</a>.)</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<p>If the associated mutex is already locked by some other thread,
|
||||
places the current thread in the <a href="definitions.html#State">
|
||||
Blocked</a> state until the associated mutex is unlocked, after which
|
||||
the current thread is placed in the <a href="definitions.html#State">
|
||||
Ready</a> state, eventually to be returned to the <a href=
|
||||
"definitions.html#State">Running</a> state. Places the associated mutex
|
||||
in the locked state.</p>
|
||||
|
||||
<p><b>Throws:</b> <a href="lock_error.html">lock_error</a> if <code>
|
||||
locked()</code> or as indicated in <b>Effects</b>.</p>
|
||||
<hr>
|
||||
|
||||
<h3>timed_lock</h3>
|
||||
<pre>
|
||||
bool timed_lock(const <a href="xtime.html">xtime</a>& xt);
|
||||
</pre>
|
||||
|
||||
<p><b>Effects:</b> Same as <code>lock()</code>, except that if xt is
|
||||
reached, places the current thread in the <a href=
|
||||
"definitions.html#State">Ready</a> state without further ado.</p>
|
||||
|
||||
<p><b>Returns:</b> <code>locked()</code>.</p>
|
||||
|
||||
<p><b>Throws:</b> <a href="lock_error.html">lock_error</a> if <code>
|
||||
locked()</code> or as indicated in <b>Effects</b>.</p>
|
||||
<hr>
|
||||
|
||||
<h3>unlock</h3>
|
||||
<pre>
|
||||
void unlock();
|
||||
</pre>
|
||||
|
||||
<p><b>Effects:</b> Unlocks the associated mutex.</p>
|
||||
|
||||
<p><b>Throws:</b> <a href="lock_error.html">lock_error</a> if <code>
|
||||
!locked()</code>.</p>
|
||||
<hr>
|
||||
|
||||
<h3>const void* Conversion</h3>
|
||||
<pre>
|
||||
operator const void*() const;
|
||||
</pre>
|
||||
|
||||
<p><b>Returns:</b> If the associated mutex is currently locked, a value
|
||||
convertible to <code>true</code>, else a value convertible to <code>
|
||||
false</code>.</p>
|
||||
|
||||
<p><b>Rationale:</b> A <code>const void*</code> conversion is
|
||||
considered safer than a conversion to <code>bool</code>.</p>
|
||||
<hr>
|
||||
|
||||
<h3>locked</h3>
|
||||
<pre>
|
||||
bool locked() const;
|
||||
</pre>
|
||||
|
||||
<p><b>Returns:</b> <code>this->operator const void*() !=
|
||||
0</code>.</p>
|
||||
<hr>
|
||||
|
||||
<h2><a name="Example">Example</a> Usage</h2>
|
||||
<pre>
|
||||
#include <boost/thread/mutex.hpp>
|
||||
#include <iostream>
|
||||
|
||||
int main(int, char*[])
|
||||
{
|
||||
boost::timed_mutex mutex;
|
||||
boost::xtime xt;
|
||||
boost::get_xtime(&xt, boost::TIME_UTC);
|
||||
xt.sec += 1;
|
||||
boost::mutex::scoped_timed_lock scope_timed_lock(mutex, xt);
|
||||
if (scope_timed_lock.locked())
|
||||
std::cout << "locked" << std::endl;
|
||||
else
|
||||
std::cout << "unlocked" << std::endl;
|
||||
return 0;
|
||||
}
|
||||
</pre>
|
||||
|
||||
<p>The output is:</p>
|
||||
<pre>
|
||||
locked
|
||||
</pre>
|
||||
<hr>
|
||||
|
||||
<p>Revised
|
||||
<!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%B %d, %Y" startspan -->November 05, 2001<!--webbot bot="Timestamp" endspan i-checksum="39585" --></p>
|
||||
|
||||
<p><i>© Copyright <a href="mailto:williamkempf@hotmail.com">
|
||||
William E. Kempf</a> 2001 all rights reserved.</i></p>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -1,305 +0,0 @@
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content=
|
||||
"text/html; charset=iso-8859-1">
|
||||
<meta name="keywords" content="threads, BTL, thread library, C++">
|
||||
<link rel="stylesheet" type="text/css" href="styles.css">
|
||||
|
||||
<title>Boost.Threads, scoped_try_lock</title>
|
||||
</head>
|
||||
|
||||
<body bgcolor="#FFFFFF" link="#0000FF" vlink="#800080">
|
||||
<table summary="header" border="0" cellpadding="7" cellspacing="0"
|
||||
width="100%">
|
||||
<tr>
|
||||
<td valign="top" width="300">
|
||||
<h3><img src="../../../c++boost.gif" alt="C++ Boost" width=
|
||||
"277" height="86"></h3>
|
||||
</td>
|
||||
|
||||
<td valign="top">
|
||||
<h1 align="center">Boost.Threads</h1>
|
||||
|
||||
<h2 align="center">scoped_try_lock</h2>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<hr>
|
||||
|
||||
<p><a href="#Introduction">Introduction</a><br>
|
||||
<a href="#Header">Header</a><br>
|
||||
<a href="#Synopsis">Synopsis</a><br>
|
||||
<a href="#Members">Members</a><br>
|
||||
<a href="#Example">Example</a></p>
|
||||
|
||||
<h2><a name="Introduction">Introduction</a></h2>
|
||||
|
||||
<p>This class template defines a generic lock type which meets the <a
|
||||
href="lock_concept.html#ScopedTryLock">ScopedTryLock</a> requirements.
|
||||
The <a href="mutex.html">try_mutex</a>, <a href="mutex.html">
|
||||
timed_mutex</a>, <a href="recursive_mutex.html">recursive_try_mutex</a>
|
||||
and <a href="recursive_mutex.html">recursive_timed_mutex</a> classes
|
||||
use this template to define their <code>scoped_try_lock</code>
|
||||
types.</p>
|
||||
|
||||
<p>Like all the <b>Boost.Threads</b> <a href="lock_concept.html">lock
|
||||
models</a>, <code>scoped_try_lock</code> objects are meant to be
|
||||
short-lived. Objects of the class are not <a href=
|
||||
"definitions.html#thread-safe">thread-safe</a>, and so should not be
|
||||
shared between threads.</p>
|
||||
|
||||
<p>Class <code>scoped_try_lock</code> follows the "resource
|
||||
acquisition is initialization" idiom <a href=
|
||||
"bibliography.html#Stroustrup-00">[Stroustrup 00 14.4.1]</a> and is a
|
||||
realization of the "Scoped Locking Pattern" <a href=
|
||||
"bibliography.html#Schmidt-00">[Schmidt-00]</a>. Thus the usage is to
|
||||
let the constructor do the locking, and then let the destructor do the
|
||||
unlocking automatically at the end of the enclosing scope. The lock()
|
||||
and unlock() members are usually not explicitly called, but are
|
||||
provided to allow for complex overlapping locks of multiple
|
||||
mutexes.</p>
|
||||
|
||||
<p>Although this class is an implementation detail, it is publicly
|
||||
documented here because of its importance.</p>
|
||||
|
||||
<p>The type used to instantiate the class must meet the <a href=
|
||||
"mutex_concept.html#TryMutex">TryMutex</a> requirements.</p>
|
||||
|
||||
<h2><a name="Header">Header</a></h2>
|
||||
<pre>
|
||||
#include <a href=
|
||||
"../../../boost/thread/detail/lock.hpp"><boost/thread/detail/lock.hpp></a>
|
||||
<i>This header is usually not included directly by programmers
|
||||
because it is supplied by <a href=
|
||||
"../../../boost/thread/mutex.hpp"><boost/thread/mutex.hpp></a> or
|
||||
<a href=
|
||||
"../../../boost/thread/recursive_mutex.hpp"><boost/thread/recursive_mutex.hpp></a></i>
|
||||
</pre>
|
||||
|
||||
<h2><a name="Synopsis">Synopsis</a></h2>
|
||||
<pre>
|
||||
namespace boost { namespace detail { namespace thread {
|
||||
template <typename TryMutex>
|
||||
class scoped_try_lock : private <a href=
|
||||
"../../utility/utility.htm#Class noncopyable">boost::noncopyable</a> // Exposition only.
|
||||
// Class scoped_try_lock meets the <a href=
|
||||
"overview.html#NonCopyable">NonCopyable</a> requirement.
|
||||
{
|
||||
public:
|
||||
typedef TryMutex mutex_type;
|
||||
|
||||
explicit scoped_try_lock(TryMutex& mx);
|
||||
scoped_try_lock(TryMutex& mx, bool initially_locked);
|
||||
~scoped_try_lock();
|
||||
|
||||
void lock();
|
||||
bool try_lock();
|
||||
void unlock();
|
||||
|
||||
operator const void*() const;
|
||||
};
|
||||
} // namespace thread
|
||||
} // namespace detail
|
||||
} // namespace boost
|
||||
</pre>
|
||||
|
||||
<h2><a name="Members">Members</a></h2>
|
||||
<hr>
|
||||
|
||||
<h3>Constructors</h3>
|
||||
<pre>
|
||||
explicit scoped_try_lock(TryMutex& mx);
|
||||
</pre>
|
||||
|
||||
<p><b>Effects:</b> Associates mutex <code>mx</code> with <code>
|
||||
*this</code>. Calls <code>try_lock()</code>.</p>
|
||||
<hr>
|
||||
<pre>
|
||||
scoped_try_lock(TryMutex& mx, bool initially_locked);
|
||||
</pre>
|
||||
|
||||
<p><b>Effects:</b> Associates mutex <code>mx</code> with <code>
|
||||
*this</code>. If <code>initially_locked</code> is <code>true,</code>
|
||||
calls <code>lock()</code>.</p>
|
||||
<hr>
|
||||
|
||||
<h3>Destructor</h3>
|
||||
<pre>
|
||||
~scoped_try_lock();
|
||||
</pre>
|
||||
|
||||
<p><b>Effects:</b> If <code>locked()</code>, calls <code>
|
||||
unlock()</code>. Destroys <code>*this</code>.</p>
|
||||
<hr>
|
||||
|
||||
<h3>lock</h3>
|
||||
<pre>
|
||||
void lock();
|
||||
</pre>
|
||||
|
||||
<p><b>Effects:</b> If the associated mutex is already locked by another
|
||||
lock in the current thread, the effects depend on the locking strategy
|
||||
of the associated mutex, as shown in the following table:</p>
|
||||
|
||||
<table summary="lock effects" border="1" cellpadding="5" height="147">
|
||||
<tr>
|
||||
<td height="34"><i><a href=
|
||||
"mutex_concept.html#LockingStrategies">Locking Strategy</a><br>
|
||||
of associated mutex</i></td>
|
||||
|
||||
<td height="34"><i>Effect if associated mutex is already locked
|
||||
by the current thread</i></td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td height="19">Recursive</td>
|
||||
|
||||
<td height="19">As if an additional lock were added to the
|
||||
mutex.</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td height="19">Checked</td>
|
||||
|
||||
<td height="19">Throws <a href="lock_error.html">
|
||||
lock_error</a>.</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td height="19">Unchecked</td>
|
||||
|
||||
<td height="19">Undefined behavior [<a href=
|
||||
"bibliography.html#ISO">ISO</a> 1.3.12] (but typically, <a
|
||||
href="definitions.html#Deadlock">deadlock</a>.)</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<p>If the associated mutex is already locked by some other thread,
|
||||
places the current thread in the <a href="definitions.html#State">
|
||||
Blocked</a> state until the associated mutex is unlocked, after which
|
||||
the current thread is placed in the <a href="definitions.html#State">
|
||||
Ready</a> state, eventually to be returned to the <a href=
|
||||
"definitions.html#State">Running</a> state. Places the associated mutex
|
||||
in the locked state.</p>
|
||||
|
||||
<p><b>Throws:</b> <a href="lock_error.html">lock_error</a> if <code>
|
||||
locked()</code> or as indicated in <b>Effects</b>.</p>
|
||||
<hr>
|
||||
|
||||
<h3>try_lock</h3>
|
||||
<pre>
|
||||
bool try_lock();
|
||||
</pre>
|
||||
|
||||
<p><b>Effects:</b> If the associated mutex is already locked by another
|
||||
lock in the current thread, the effects depend on the locking strategy
|
||||
of the associated mutex, as shown in the following table:</p>
|
||||
|
||||
<table summary="try_lock effects" border="1" cellpadding="5" height=
|
||||
"147">
|
||||
<tr>
|
||||
<td height="34"><i><a href=
|
||||
"mutex_concept.html#LockingStrategies">Locking Strategy</a><br>
|
||||
of associated mutex</i></td>
|
||||
|
||||
<td height="34"><i>Effect if associated mutex is already locked
|
||||
by the current thread</i></td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td height="19">Recursive</td>
|
||||
|
||||
<td height="19">As if an additional lock were added to the
|
||||
mutex.</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td height="19">Checked</td>
|
||||
|
||||
<td height="19">Throws <a href="lock_error.html">
|
||||
lock_error</a>.</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td height="19">Unspecified</td>
|
||||
|
||||
<td height="19">Undefined behavior [<a href=
|
||||
"bibliography.html#ISO">ISO</a> 1.3.12] (but typically, <a
|
||||
href="definitions.html#Deadlock">deadlock</a>.)</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<p>If the associated mutex is not already locked by some other thread,
|
||||
locks the associated mutex and returns true, else returns false.</p>
|
||||
|
||||
<p><b>Returns:</b> See effects.</p>
|
||||
|
||||
<p><b>Throws:</b> <a href="lock_error.html">lock_error</a> if <code>
|
||||
locked()</code> or as indicated in <b>Effects</b>.</p>
|
||||
<hr>
|
||||
|
||||
<h3>unlock</h3>
|
||||
<pre>
|
||||
void unlock();
|
||||
</pre>
|
||||
|
||||
<p><b>Effects:</b> Unlocks the associated mutex.</p>
|
||||
|
||||
<p><b>Throws:</b> <a href="lock_error.html">lock_error</a> if <code>
|
||||
!locked()</code>.</p>
|
||||
<hr>
|
||||
|
||||
<h3>const void* Conversion</h3>
|
||||
<pre>
|
||||
operator const void*() const;
|
||||
</pre>
|
||||
|
||||
<p><b>Returns:</b> If the associated mutex is currently locked, a value
|
||||
convertible to <code>true</code>, else a value convertible to <code>
|
||||
false</code>.</p>
|
||||
|
||||
<p><b>Rationale:</b> A <code>const void*</code> conversion is
|
||||
considered safer than a conversion to <code>bool</code>.</p>
|
||||
<hr>
|
||||
|
||||
<h3>locked</h3>
|
||||
<pre>
|
||||
bool locked() const;
|
||||
</pre>
|
||||
|
||||
<p><b>Returns:</b> <code>this->operator const void*() !=
|
||||
0</code>.</p>
|
||||
<hr>
|
||||
|
||||
<h2><a name="Example">Example</a> Usage</h2>
|
||||
<pre>
|
||||
#include <a href=
|
||||
"../../../boost/thread/mutex.hpp"><boost/thread/mutex.hpp></a>
|
||||
#include <iostream>
|
||||
|
||||
int main(int, char*[])
|
||||
{
|
||||
boost::mutex mutex;
|
||||
boost::mutex::try_lock lock(mutex);
|
||||
if (lock)
|
||||
std::cout << "locked" << std::endl;
|
||||
else
|
||||
std::cout << "unlocked" << std::endl;
|
||||
return 0;
|
||||
}
|
||||
</pre>
|
||||
|
||||
<p>The output is:</p>
|
||||
<pre>
|
||||
locked
|
||||
</pre>
|
||||
<hr>
|
||||
|
||||
<p>Revised
|
||||
<!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan -->05 November, 2001<!--webbot bot="Timestamp" endspan i-checksum="39359" --></p>
|
||||
|
||||
<p><i>© Copyright <a href="mailto:williamkempf@hotmail.com">
|
||||
William E. Kempf</a> 2001 all rights reserved.</i></p>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
228
doc/shared_mutex_ref.qbk
Normal file
228
doc/shared_mutex_ref.qbk
Normal file
@@ -0,0 +1,228 @@
|
||||
[/
|
||||
(C) Copyright 2007-8 Anthony Williams.
|
||||
Distributed under the Boost Software License, Version 1.0.
|
||||
(See accompanying file LICENSE_1_0.txt or copy at
|
||||
http://www.boost.org/LICENSE_1_0.txt).
|
||||
]
|
||||
|
||||
[section:shared_mutex Class `shared_mutex` -- EXTENSION]
|
||||
|
||||
#include <boost/thread/shared_mutex.hpp>
|
||||
|
||||
class shared_mutex
|
||||
{
|
||||
public:
|
||||
shared_mutex(shared_mutex const&) = delete;
|
||||
shared_mutex& operator=(shared_mutex const&) = delete;
|
||||
|
||||
shared_mutex();
|
||||
~shared_mutex();
|
||||
|
||||
void lock_shared();
|
||||
bool try_lock_shared();
|
||||
template <class Rep, class Period>
|
||||
bool try_lock_shared_for(const chrono::duration<Rep, Period>& rel_time);
|
||||
template <class Clock, class Duration>
|
||||
bool try_lock_shared_until(const chrono::time_point<Clock, Duration>& abs_time);
|
||||
void unlock_shared();
|
||||
|
||||
void lock();
|
||||
bool try_lock();
|
||||
template <class Rep, class Period>
|
||||
bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
|
||||
template <class Clock, class Duration>
|
||||
bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
|
||||
void unlock();
|
||||
|
||||
#if defined BOOST_THREAD_PROVIDES_DEPRECATED_FEATURES_SINCE_V3_0_0
|
||||
// use upgrade_mutex instead.
|
||||
void lock_upgrade();
|
||||
void unlock_upgrade();
|
||||
|
||||
void unlock_upgrade_and_lock();
|
||||
void unlock_and_lock_upgrade();
|
||||
void unlock_and_lock_shared();
|
||||
void unlock_upgrade_and_lock_shared();
|
||||
#endif
|
||||
|
||||
#if defined BOOST_THREAD_USES_DATETIME
|
||||
bool timed_lock_shared(system_time const& timeout);
|
||||
bool timed_lock(system_time const& timeout);
|
||||
#endif
|
||||
|
||||
};
|
||||
|
||||
The class `boost::shared_mutex` provides an implementation of a multiple-reader / single-writer mutex. It implements the
|
||||
__shared_lockable_concept__.
|
||||
|
||||
Multiple concurrent calls to __lock_ref__, __try_lock_ref__, `__try_lock_for()`, `__try_lock_until()`, __timed_lock_ref__, __lock_shared_ref__,
|
||||
`__try_lock_shared_for()`, `__try_lock_shared_until()`, __try_lock_shared_ref__ and __timed_lock_shared_ref__ are permitted.
|
||||
|
||||
Note the the lack of reader-writer priority policies in shared_mutex. This is due to an algorithm credited to Alexander Terekhov which lets the OS decide which thread is the next to get the lock without caring whether a unique lock or shared lock is being sought. This results in a complete lack of reader or writer starvation. It is simply fair.
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:upgrade_mutex Class `upgrade_mutex` -- EXTENSION]
|
||||
|
||||
#include <boost/thread/shared_mutex.hpp>
|
||||
|
||||
class upgrade_mutex
|
||||
{
|
||||
public:
|
||||
upgrade_mutex(upgrade_mutex const&) = delete;
|
||||
upgrade_mutex& operator=(upgrade_mutex const&) = delete;
|
||||
|
||||
upgrade_mutex();
|
||||
~upgrade_mutex();
|
||||
|
||||
void lock_shared();
|
||||
bool try_lock_shared();
|
||||
template <class Rep, class Period>
|
||||
bool try_lock_shared_for(const chrono::duration<Rep, Period>& rel_time);
|
||||
template <class Clock, class Duration>
|
||||
bool try_lock_shared_until(const chrono::time_point<Clock, Duration>& abs_time);
|
||||
void unlock_shared();
|
||||
|
||||
void lock();
|
||||
bool try_lock();
|
||||
template <class Rep, class Period>
|
||||
bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
|
||||
template <class Clock, class Duration>
|
||||
bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
|
||||
void unlock();
|
||||
|
||||
void lock_upgrade();
|
||||
template <class Rep, class Period>
|
||||
bool try_lock_upgrade_for(const chrono::duration<Rep, Period>& rel_time);
|
||||
template <class Clock, class Duration>
|
||||
bool try_lock_upgrade_until(const chrono::time_point<Clock, Duration>& abs_time);
|
||||
void unlock_upgrade();
|
||||
|
||||
// Shared <-> Exclusive
|
||||
|
||||
#ifdef BOOST_THREAD_PROVIDES_SHARED_MUTEX_UPWARDS_CONVERSIONS
|
||||
bool try_unlock_shared_and_lock();
|
||||
template <class Rep, class Period>
|
||||
bool try_unlock_shared_and_lock_for(const chrono::duration<Rep, Period>& rel_time);
|
||||
template <class Clock, class Duration>
|
||||
bool try_unlock_shared_and_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
|
||||
#endif
|
||||
void unlock_and_lock_shared();
|
||||
|
||||
// Shared <-> Upgrade
|
||||
|
||||
#ifdef BOOST_THREAD_PROVIDES_SHARED_MUTEX_UPWARDS_CONVERSIONS
|
||||
bool try_unlock_shared_and_lock_upgrade();
|
||||
template <class Rep, class Period>
|
||||
bool try_unlock_shared_and_lock_upgrade_for(const chrono::duration<Rep, Period>& rel_time);
|
||||
template <class Clock, class Duration>
|
||||
bool try_unlock_shared_and_lock_upgrade_until(const chrono::time_point<Clock, Duration>& abs_time);
|
||||
#endif
|
||||
void unlock_upgrade_and_lock_shared();
|
||||
|
||||
// Upgrade <-> Exclusive
|
||||
|
||||
void unlock_upgrade_and_lock();
|
||||
#if defined(BOOST_THREAD_PLATFORM_PTHREAD)
|
||||
|| defined(BOOST_THREAD_PROVIDES_GENERIC_SHARED_MUTEX_ON_WIN)
|
||||
bool try_unlock_upgrade_and_lock();
|
||||
template <class Rep, class Period>
|
||||
bool try_unlock_upgrade_and_lock_for(const chrono::duration<Rep, Period>& rel_time);
|
||||
template <class Clock, class Duration>
|
||||
bool try_unlock_upgrade_and_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
|
||||
#endif
|
||||
void unlock_and_lock_upgrade();
|
||||
};
|
||||
|
||||
The class `boost::upgrade_mutex` provides an implementation of a multiple-reader / single-writer mutex. It implements the
|
||||
__upgrade_lockable_concept__.
|
||||
|
||||
Multiple concurrent calls to __lock_ref__, __try_lock_ref__, `__try_lock_for()`, `__try_lock_until()`, __timed_lock_ref__, __lock_shared_ref__,
|
||||
`__try_lock_shared_for()`, `__try_lock_shared_until()`, __try_lock_shared_ref__ and __timed_lock_shared_ref__ are permitted.
|
||||
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:null_mutex Class `null_mutex` -- EXTENSION]
|
||||
|
||||
#include <boost/thread/null_mutex.hpp>
|
||||
|
||||
class null_mutex
|
||||
{
|
||||
public:
|
||||
null_mutex(null_mutex const&) = delete;
|
||||
null_mutex& operator=(null_mutex const&) = delete;
|
||||
|
||||
null_mutex();
|
||||
~null_mutex();
|
||||
|
||||
void lock_shared();
|
||||
bool try_lock_shared();
|
||||
#ifdef BOOST_THREAD_USES_CHRONO
|
||||
template <class Rep, class Period>
|
||||
bool try_lock_shared_for(const chrono::duration<Rep, Period>& rel_time);
|
||||
template <class Clock, class Duration>
|
||||
bool try_lock_shared_until(const chrono::time_point<Clock, Duration>& abs_time);
|
||||
#endif
|
||||
void unlock_shared();
|
||||
|
||||
void lock();
|
||||
bool try_lock();
|
||||
#ifdef BOOST_THREAD_USES_CHRONO
|
||||
template <class Rep, class Period>
|
||||
bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
|
||||
template <class Clock, class Duration>
|
||||
bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
|
||||
#endif
|
||||
void unlock();
|
||||
|
||||
void lock_upgrade();
|
||||
#ifdef BOOST_THREAD_USES_CHRONO
|
||||
template <class Rep, class Period>
|
||||
bool try_lock_upgrade_for(const chrono::duration<Rep, Period>& rel_time);
|
||||
template <class Clock, class Duration>
|
||||
bool try_lock_upgrade_until(const chrono::time_point<Clock, Duration>& abs_time);
|
||||
#endif
|
||||
void unlock_upgrade();
|
||||
|
||||
// Shared <-> Exclusive
|
||||
|
||||
bool try_unlock_shared_and_lock();
|
||||
#ifdef BOOST_THREAD_USES_CHRONO
|
||||
template <class Rep, class Period>
|
||||
bool try_unlock_shared_and_lock_for(const chrono::duration<Rep, Period>& rel_time);
|
||||
template <class Clock, class Duration>
|
||||
bool try_unlock_shared_and_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
|
||||
#endif
|
||||
void unlock_and_lock_shared();
|
||||
|
||||
// Shared <-> Upgrade
|
||||
|
||||
bool try_unlock_shared_and_lock_upgrade();
|
||||
#ifdef BOOST_THREAD_USES_CHRONO
|
||||
template <class Rep, class Period>
|
||||
bool try_unlock_shared_and_lock_upgrade_for(const chrono::duration<Rep, Period>& rel_time);
|
||||
template <class Clock, class Duration>
|
||||
bool try_unlock_shared_and_lock_upgrade_until(const chrono::time_point<Clock, Duration>& abs_time);
|
||||
#endif
|
||||
void unlock_upgrade_and_lock_shared();
|
||||
|
||||
// Upgrade <-> Exclusive
|
||||
|
||||
void unlock_upgrade_and_lock();
|
||||
bool try_unlock_upgrade_and_lock();
|
||||
#ifdef BOOST_THREAD_USES_CHRONO
|
||||
template <class Rep, class Period>
|
||||
bool try_unlock_upgrade_and_lock_for(const chrono::duration<Rep, Period>& rel_time);
|
||||
template <class Clock, class Duration>
|
||||
bool try_unlock_upgrade_and_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
|
||||
#endif
|
||||
void unlock_and_lock_upgrade();
|
||||
};
|
||||
|
||||
The class `boost::null_mutex` provides a no-op implementation of a multiple-reader / single-writer mutex. It is a model of the
|
||||
__UpgradeLockable concept.
|
||||
|
||||
|
||||
[endsect]
|
||||
|
||||
@@ -1,4 +0,0 @@
|
||||
PRE
|
||||
{
|
||||
BACKGROUND-COLOR: lightcyan
|
||||
}
|
||||
44
doc/sync_tutorial.qbk
Normal file
44
doc/sync_tutorial.qbk
Normal file
@@ -0,0 +1,44 @@
|
||||
[/
|
||||
(C) Copyright 2012 Vicente J. Botet Escriba.
|
||||
Distributed under the Boost Software License, Version 1.0.
|
||||
(See accompanying file LICENSE_1_0.txt or copy at
|
||||
http://www.boost.org/LICENSE_1_0.txt).
|
||||
]
|
||||
|
||||
[section:tutorial Tutorial]
|
||||
|
||||
[@http://home.roadrunner.com/~hinnant/mutexes/locking.html Handling mutexes in C++] is an excellent tutorial. You need just replace std and ting by boost.
|
||||
|
||||
[@http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2406.html Mutex, Lock, Condition Variable Rationale] adds rationale for the design decisions made for mutexes, locks and condition variables.
|
||||
|
||||
|
||||
In addition to the C++11 standard locks, Boost.Thread provides other locks and some utilities that help the user to make their code thread-safe.
|
||||
|
||||
[include internal_locking.qbk]
|
||||
|
||||
[include external_locking.qbk]
|
||||
|
||||
[section:with Executing Around a Function]
|
||||
|
||||
In particular, the library provides some lock factories.
|
||||
|
||||
template <class Lockable, class Function>
|
||||
auto with_lock_guard(Lockable& m, Function f) -> decltype(fn())
|
||||
{
|
||||
auto&& _ = boost::make_lock_guard(f);
|
||||
f();
|
||||
}
|
||||
|
||||
|
||||
that can be used as
|
||||
|
||||
int i = with_lock_guard(mtx, {}() -> bool
|
||||
{
|
||||
// access the protected state
|
||||
return true;
|
||||
});
|
||||
|
||||
|
||||
[endsect] [/ With]
|
||||
|
||||
[endsect] [/ Tutorial]
|
||||
261
doc/thread.html
261
doc/thread.html
@@ -1,261 +0,0 @@
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content=
|
||||
"text/html; charset=iso-8859-1">
|
||||
<meta name="keywords" content=
|
||||
"threads, Boost.Threads, thread library, C++">
|
||||
<link rel="stylesheet" type="text/css" href="styles.css">
|
||||
|
||||
<title>Boost.Threads, thread</title>
|
||||
</head>
|
||||
|
||||
<body bgcolor="#ffffff" link="#0000ff" vlink="#800080">
|
||||
<table summary="header" border="0" cellpadding="7" cellspacing="0"
|
||||
width="100%">
|
||||
<tr>
|
||||
<td valign="top" width="300">
|
||||
<h3><img height="86" alt="C++ Boost" src=
|
||||
"../../../c++boost.gif" width="277"></h3>
|
||||
</td>
|
||||
|
||||
<td valign="top">
|
||||
<h1 align="center">Boost.Threads</h1>
|
||||
|
||||
<h2 align="center">Class thread</h2>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<hr>
|
||||
|
||||
<p><a href="#Introduction">Introduction</a><br>
|
||||
<a href="#Header">Header</a><br>
|
||||
<a href="#Synopsis">Synopsis</a><br>
|
||||
<a href="#Members">Members</a><br>
|
||||
<a href="#Example">Example</a></p>
|
||||
|
||||
<h2><a name="Introduction">Introduction</a></h2>
|
||||
|
||||
<p>The <code>thread</code> class represents threads of execution, and
|
||||
provides the functionality to create and manage threads within the <b>
|
||||
Boost.Threads</b> library. See <a href="definitions.html">
|
||||
Definitions</a> for a precise description of "thread of
|
||||
execution", and for definitions of threading related terms and of
|
||||
thread states such as "blocked".</p>
|
||||
|
||||
<p>A thread of execution has an initial function. For the program's
|
||||
initial thread, the initial function is <code>main()</code>. For other
|
||||
threads, the initial function is <code>operator()</code> of the
|
||||
function object passed to the class <code>thread</code>
|
||||
constructor.</p>
|
||||
|
||||
<p>A thread of execution is said to be "finished" or
|
||||
"finished execution" when its initial function returns or is
|
||||
terminated. This includes completion of all thread cleanup handlers,
|
||||
and completion of the normal C++ function return behaviors, such as
|
||||
destruction of automatic storage (stack) objects and releasing any
|
||||
associated implementation resources.</p>
|
||||
|
||||
<p>A thread object has an associated state which is either
|
||||
"joinable" or "non-joinable".</p>
|
||||
|
||||
<p>Except as described below, the policy used by an implementation of
|
||||
<b>Boost.Threads</b> to schedule transitions between thread states is
|
||||
unspecified.</p>
|
||||
|
||||
<p><b>Note:</b> Just as the lifetime of a file may be different from
|
||||
the lifetime of an iostream object which represents the file, the
|
||||
lifetime of a thread of execution may be different from the <code>
|
||||
thread</code> object which represents the thread of execution. In
|
||||
particular, after a call to <code>join()</code>, the thread of
|
||||
execution will no longer exist even though the <code>thread</code>
|
||||
object continues to exist until the end of its normal lifetime. The
|
||||
converse is also possible; if a <code>thread</code> object is destroyed
|
||||
without <code>join()</code> having first been called, the thread of
|
||||
execution continues until its initial function completes.</p>
|
||||
|
||||
<h2><a name="Header">Header</a></h2>
|
||||
<pre>
|
||||
#include <a href=
|
||||
"../../../boost/thread/thread.hpp"><boost/thread/thread.hpp></a>
|
||||
</pre>
|
||||
|
||||
<h2><a name="Synopsis">Synopsis</a></h2>
|
||||
<pre>
|
||||
namespace boost {
|
||||
|
||||
class thread : <a href=
|
||||
"../../utility/utility.htm#noncopyable">boost::noncopyable</a> // Exposition only.
|
||||
// Class thread meets the <a href=
|
||||
"overview.html#NonCopyable">NonCopyable</a> requirement.
|
||||
{
|
||||
public:
|
||||
thread();
|
||||
explicit thread(const boost::function0<void>& threadfunc);
|
||||
~thread();
|
||||
|
||||
bool operator==(const thread& rhs) const;
|
||||
bool operator!=(const thread& rhs) const;
|
||||
|
||||
void join();
|
||||
|
||||
static void sleep(const xtime& xt);
|
||||
static void yield();
|
||||
};
|
||||
|
||||
} // namespace boost
|
||||
</pre>
|
||||
|
||||
<h2><a name="Members">Members</a></h2>
|
||||
<hr>
|
||||
|
||||
<h3>Constructors</h3>
|
||||
<pre>
|
||||
thread();
|
||||
</pre>
|
||||
|
||||
<p><b>Effects:</b> Constructs a <code>thread</code> object representing
|
||||
the current thread of execution.</p>
|
||||
|
||||
<p><b>Postcondition:</b> <code>*this</code> is non-joinable.</p>
|
||||
|
||||
<p><b>Danger:</b> <code>*this</code> is valid only within the current
|
||||
thread.</p>
|
||||
<pre>
|
||||
thread(const <a href=
|
||||
"../../function/index.html">boost::function0</a><void>& threadfunc);
|
||||
</pre>
|
||||
|
||||
<p><b>Effects:</b> Starts a new thread of execution and constructs a
|
||||
<code>thread</code> object representing it. Copies <code>
|
||||
threadfunc</code> (which in turn copies the function object wrapped by
|
||||
<code>threadfunc</code>) to an internal location which persists for the
|
||||
lifetime of the new thread of execution. Calls <code>operator()</code>
|
||||
on the copy of the <code>threadfunc</code> function object in the new
|
||||
thread of execution.</p>
|
||||
|
||||
<p><b>Postcondition:</b> <code>*this</code> is joinable.</p>
|
||||
|
||||
<p><b>Throws:</b> <code>boost::thread_resource_error</code> if a new
|
||||
thread of execution cannot be started.</p>
|
||||
<hr>
|
||||
|
||||
<h3>Destructor</h3>
|
||||
<pre>
|
||||
~thread();
|
||||
</pre>
|
||||
|
||||
<p><b>Effects:</b> Destroys <code>*this</code>. The actual thread of
|
||||
execution may continue to execute after the <code>thread</code> object
|
||||
has been destroyed.</p>
|
||||
|
||||
<p><b>Notes:</b> If <code>*this</code> is joinable the actual thread of
|
||||
execution becomes "detached". Any resources used by the
|
||||
thread will be reclaimed when the thread of execution completes. To
|
||||
ensure such a thread of execution runs to completion before the <code>
|
||||
thread</code> object is destroyed, call <code>join()</code>.</p>
|
||||
<hr>
|
||||
|
||||
<h3>Comparison Operators</h3>
|
||||
<pre>
|
||||
bool operator==(const thread& rhs);
|
||||
</pre>
|
||||
|
||||
<p><b>Requires:</b> The thread is non-terminated or <code>*this</code>
|
||||
is joinable.</p>
|
||||
|
||||
<p><b>Returns:</b> <code>true</code> if <code>*this</code> and <code>
|
||||
rhs</code> represent the same thread of execution.</p>
|
||||
<pre>
|
||||
bool operator!=(const thread& rhs);
|
||||
</pre>
|
||||
|
||||
<p><b>Returns:</b> <code>!(*this==rhs)</code>.</p>
|
||||
<hr>
|
||||
|
||||
<h3>join</h3>
|
||||
<pre>
|
||||
void join();
|
||||
</pre>
|
||||
|
||||
<p><b>Requires:</b> <code>*this</code> is joinable.</p>
|
||||
|
||||
<p><b>Effects:</b> The current thread of execution blocks until the
|
||||
initial function of the thread of execution represented by <code>
|
||||
*this</code> finishes and all resources are reclaimed.</p>
|
||||
|
||||
<p><b>Postcondition:</b> <code>*this</code> is non-joinable.</p>
|
||||
|
||||
<p><b>Note:</b> If <code>*this == thread()</code> the result is
|
||||
implementation defined. If the implementation doesn't detect this
|
||||
the result will be <a href="definitions.html#Deadlock">
|
||||
deadlock</a>.</p>
|
||||
<hr>
|
||||
|
||||
<h3>sleep</h3>
|
||||
<pre>
|
||||
static void sleep(const <a href="xtime.html">xtime</a>& xt);
|
||||
</pre>
|
||||
|
||||
<p><b>Effects:</b> The current thread of execution blocks until <code>
|
||||
xt</code> is reached.</p>
|
||||
<hr>
|
||||
|
||||
<h3>yield</h3>
|
||||
<pre>
|
||||
static void yield();
|
||||
</pre>
|
||||
|
||||
<p><b>Effects:</b> The current thread of execution is placed in the
|
||||
"ready" state.</p>
|
||||
|
||||
<p><b>Notes:</b> Allow the current thread to give up the rest of its
|
||||
time slice (or other scheduling quota) to another thread. Particularly
|
||||
useful in non-preemptive implementations.</p>
|
||||
<hr>
|
||||
|
||||
<h2><a name="Example">Example Usage</a></h2>
|
||||
<pre>
|
||||
#include <boost/thread/thread.hpp>
|
||||
#include <iostream>
|
||||
|
||||
struct thread_alarm
|
||||
{
|
||||
thread_alarm(int secs) : m_secs(secs) { }
|
||||
void operator()()
|
||||
{
|
||||
boost::xtime xt;
|
||||
boost::xtime_get(&xt, boost::TIME_UTC);
|
||||
xt.sec += m_secs;
|
||||
|
||||
boost::thread::sleep(xt);
|
||||
|
||||
std::cout << "alarm sounded..." << std::endl;
|
||||
}
|
||||
|
||||
int m_secs;
|
||||
};
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
int secs = 5;
|
||||
std::cout << "setting alarm for 5 seconds..." << std::endl;
|
||||
boost::thread thrd(thread_alarm(secs));
|
||||
thrd.join();
|
||||
}
|
||||
</pre>
|
||||
|
||||
<p>The output is:</p>
|
||||
<pre>
|
||||
setting alarm for 5 seconds...
|
||||
alarm sounded...
|
||||
</pre>
|
||||
<hr>
|
||||
|
||||
<p>Revised
|
||||
<!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan -->05 November, 2001<!--webbot bot="Timestamp" endspan i-checksum="39359" --></p>
|
||||
|
||||
<p><i>© Copyright <a href="mailto:williamkempf@hotmail.com">
|
||||
William E. Kempf</a> 2001 all rights reserved.</i></p>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
252
doc/thread.qbk
Normal file
252
doc/thread.qbk
Normal file
@@ -0,0 +1,252 @@
|
||||
[/
|
||||
(C) Copyright 2008-11 Anthony Williams
|
||||
(C) Copyright 2011-12 Vicente J. Botet Escriba
|
||||
Distributed under the Boost Software License, Version 1.0.
|
||||
(See accompanying file LICENSE_1_0.txt or copy at
|
||||
http://www.boost.org/LICENSE_1_0.txt).
|
||||
]
|
||||
|
||||
[library Thread
|
||||
[quickbook 1.5]
|
||||
[version 4.0.0]
|
||||
[authors [Williams, Anthony] [Botet Escriba, Vicente J.]]
|
||||
[copyright 2007-11 Anthony Williams]
|
||||
[copyright 2011-12 Vicente J. Botet Escriba]
|
||||
[purpose C++ Library for launching threads and synchronizing data between them]
|
||||
[category text]
|
||||
[license
|
||||
Distributed under the Boost Software License, Version 1.0.
|
||||
(See accompanying file LICENSE_1_0.txt or copy at
|
||||
[@http://www.boost.org/LICENSE_1_0.txt])
|
||||
]
|
||||
]
|
||||
|
||||
[template lockable_concept_link[link_text] [link thread.synchronization.mutex_concepts.lockable [link_text]]]
|
||||
[def __lockable_concept__ [lockable_concept_link `Lockable` concept]]
|
||||
[def __lockable_concept_type__ [lockable_concept_link `Lockable`]]
|
||||
[def __BasicLockable [link thread.synchronization.mutex_concepts.basic_lockable `BasicLockable`]]
|
||||
[def __Lockable [link thread.synchronization.mutex_concepts.lockable `Lockable`]]
|
||||
[def __TimedLockable [link thread.synchronization.mutex_concepts.timed_lockable `TimedLockable`]]
|
||||
[def __SharedLockable [link thread.synchronization.mutex_concepts.shared_lockable `SharedLockable`]]
|
||||
[def __UpgradeLockable [link thread.synchronization.mutex_concepts.upgrade_lockable `UpgradeLockable`]]
|
||||
|
||||
[template timed_lockable_concept_link[link_text] [link thread.synchronization.mutex_concepts.timed_lockable [link_text]]]
|
||||
[def __timed_lockable_concept__ [timed_lockable_concept_link `TimedLockable` concept]]
|
||||
[def __timed_lockable_concept_type__ [timed_lockable_concept_link `TimedLockable`]]
|
||||
|
||||
[template shared_lockable_concept_link[link_text] [link thread.synchronization.mutex_concepts.shared_lockable [link_text]]]
|
||||
[def __shared_lockable_concept__ [shared_lockable_concept_link `SharedLockable` concept]]
|
||||
[def __shared_lockable_concept_type__ [shared_lockable_concept_link `SharedLockable`]]
|
||||
|
||||
[template upgrade_lockable_concept_link[link_text] [link thread.synchronization.mutex_concepts.upgrade_lockable [link_text]]]
|
||||
[def __upgrade_lockable_concept__ [upgrade_lockable_concept_link `UpgradeLockable` concept]]
|
||||
[def __upgrade_lockable_concept_type__ [upgrade_lockable_concept_link `UpgradeLockable`]]
|
||||
|
||||
|
||||
[template lock_ref_link[link_text] [link thread.synchronization.mutex_concepts.basic_lockable.lock [link_text]]]
|
||||
[def __lock_ref__ [lock_ref_link `lock()`]]
|
||||
[def __lock [link thread.synchronization.mutex_concepts.basic_lockable.lock `lock`]]
|
||||
|
||||
[template lock_multiple_ref_link[link_text] [link thread.synchronization.lock_functions.lock_multiple [link_text]]]
|
||||
[def __lock_multiple_ref__ [lock_multiple_ref_link `lock()`]]
|
||||
|
||||
[template try_lock_multiple_ref_link[link_text] [link thread.synchronization.lock_functions.try_lock_multiple [link_text]]]
|
||||
[def __try_lock_multiple_ref__ [try_lock_multiple_ref_link `try_lock()`]]
|
||||
|
||||
[template unlock_ref_link[link_text] [link thread.synchronization.mutex_concepts.basic_lockable.unlock [link_text]]]
|
||||
[def __unlock_ref__ [unlock_ref_link `unlock()`]]
|
||||
[def __unlock [link thread.synchronization.mutex_concepts.basic_lockable.unlock `unlock`]]
|
||||
|
||||
[template try_lock_ref_link[link_text] [link thread.synchronization.mutex_concepts.lockable.try_lock [link_text]]]
|
||||
[def __try_lock_ref__ [try_lock_ref_link `try_lock()`]]
|
||||
[def __try_lock [link thread.synchronization.mutex_concepts.lockable.try_lock `try_lock`]]
|
||||
|
||||
[template timed_lock_ref_link[link_text] [link thread.synchronization.mutex_concepts.timed_lockable.timed_lock [link_text]]]
|
||||
[def __timed_lock_ref__ [timed_lock_ref_link `timed_lock()`]]
|
||||
[def __timed_lock [link thread.synchronization.mutex_concepts.timed_lockable.timed_lock `timed_lock`]]
|
||||
|
||||
[def __try_lock_for [link thread.synchronization.mutex_concepts.timed_lockable.try_lock_for `try_lock_for`]]
|
||||
[def __try_lock_until [link thread.synchronization.mutex_concepts.timed_lockable.try_lock_until `try_lock_until`]]
|
||||
|
||||
[template timed_lock_duration_ref_link[link_text] [link thread.synchronization.mutex_concepts.timed_lockable.timed_lock_duration [link_text]]]
|
||||
[def __timed_lock_duration_ref__ [timed_lock_duration_ref_link `timed_lock()`]]
|
||||
[def __timed_lock_duration [link thread.synchronization.mutex_concepts.timed_lockable.timed_lock_duration `timed_lock`]]
|
||||
|
||||
[template lock_shared_ref_link[link_text] [link thread.synchronization.mutex_concepts.shared_lockable.lock_shared [link_text]]]
|
||||
[def __lock_shared_ref__ [lock_shared_ref_link `lock_shared()`]]
|
||||
[def __lock_shared [link thread.synchronization.mutex_concepts.shared_lockable.lock_shared `lock_shared()`]]
|
||||
|
||||
[template unlock_shared_ref_link[link_text] [link thread.synchronization.mutex_concepts.shared_lockable.unlock_shared [link_text]]]
|
||||
[def __unlock_shared_ref__ [unlock_shared_ref_link `unlock_shared()`]]
|
||||
[def __unlock_shared [link thread.synchronization.mutex_concepts.shared_lockable.unlock_shared `unlock_shared()`]]
|
||||
|
||||
[template try_lock_shared_ref_link[link_text] [link thread.synchronization.mutex_concepts.shared_lockable.try_lock_shared [link_text]]]
|
||||
[def __try_lock_shared_ref__ [try_lock_shared_ref_link `try_lock_shared()`]]
|
||||
[def __try_lock_shared [link thread.synchronization.mutex_concepts.shared_lockable.try_lock_shared `try_lock_shared`]]
|
||||
|
||||
[template timed_lock_shared_ref_link[link_text] [link thread.synchronization.mutex_concepts.shared_lockable.timed_lock_shared [link_text]]]
|
||||
[def __timed_lock_shared_ref__ [timed_lock_shared_ref_link `timed_lock_shared()`]]
|
||||
[def __try_lock_shared_for [link thread.synchronization.mutex_concepts.shared_lockable.try_lock_shared_for `try_lock_shared_for`]]
|
||||
[def __try_lock_shared_for [link thread.synchronization.mutex_concepts.shared_lockable.try_lock_shared_until `try_lock_shared_until`]]
|
||||
|
||||
[template timed_lock_shared_duration_ref_link[link_text] [link thread.synchronization.mutex_concepts.shared_lockable.timed_lock_shared_duration [link_text]]]
|
||||
[def __timed_lock_shared_duration_ref__ [timed_lock_shared_duration_ref_link `timed_lock_shared()`]]
|
||||
[def __try_lock_shared_for [link thread.synchronization.mutex_concepts.shared_lockable.try_lock_shared_for `try_lock_shared_for`]]
|
||||
[def __try_lock_shared_until [link thread.synchronization.mutex_concepts.shared_lockable.try_lock_shared_until `try_lock_shared_until`]]
|
||||
|
||||
[template lock_upgrade_ref_link[link_text] [link thread.synchronization.mutex_concepts.upgrade_lockable.lock_upgrade [link_text]]]
|
||||
[def __lock_upgrade_ref__ [lock_upgrade_ref_link `lock_upgrade()`]]
|
||||
[def __lock_upgrade [link thread.synchronization.mutex_concepts.upgrade_lockable.lock_upgrade `lock_upgrade`]]
|
||||
[def __try_lock_upgrade [link thread.synchronization.mutex_concepts.upgrade_lockable.try_lock_upgrade `try_lock_upgrade`]]
|
||||
[def __try_lock_upgrade_for [link thread.synchronization.mutex_concepts.upgrade_lockable.try_lock_upgrade_for `try_lock_upgrade_for`]]
|
||||
[def __try_lock_upgrade_until [link thread.synchronization.mutex_concepts.upgrade_lockable.try_lock_upgrade_until `try_lock_upgrade_until`]]
|
||||
|
||||
[template unlock_upgrade_ref_link[link_text] [link thread.synchronization.mutex_concepts.upgrade_lockable.unlock_upgrade [link_text]]]
|
||||
[def __unlock_upgrade_ref__ [unlock_upgrade_ref_link `unlock_upgrade()`]]
|
||||
[def __unlock_upgrade [link thread.synchronization.mutex_concepts.upgrade_lockable.unlock_upgrade `unlock_upgrade`]]
|
||||
|
||||
[template unlock_upgrade_and_lock_ref_link[link_text] [link thread.synchronization.mutex_concepts.upgrade_lockable.unlock_upgrade_and_lock [link_text]]]
|
||||
[def __unlock_upgrade_and_lock_ref__ [unlock_upgrade_and_lock_ref_link `unlock_upgrade_and_lock()`]]
|
||||
[def __unlock_upgrade_and_lock [link thread.synchronization.mutex_concepts.upgrade_lockable.unlock_upgrade_and_lock `unlock_upgrade_and_lock`]]
|
||||
|
||||
[template unlock_and_lock_upgrade_ref_link[link_text] [link thread.synchronization.mutex_concepts.upgrade_lockable.unlock_and_lock_upgrade [link_text]]]
|
||||
[def __unlock_and_lock_upgrade_ref__ [unlock_and_lock_upgrade_ref_link `unlock_and_lock_upgrade()`]]
|
||||
[def __unlock_and_lock_upgrade [link thread.synchronization.mutex_concepts.upgrade_lockable.unlock_and_lock_upgrade `unlock_and_lock_upgrade`]]
|
||||
|
||||
[template unlock_upgrade_and_lock_shared_ref_link[link_text] [link thread.synchronization.mutex_concepts.upgrade_lockable.unlock_upgrade_and_lock_shared [link_text]]]
|
||||
[def __unlock_upgrade_and_lock_shared_ref__ [unlock_upgrade_and_lock_shared_ref_link `unlock_upgrade_and_lock_shared()`]]
|
||||
[def __unlock_upgrade_and_lock_shared [link thread.synchronization.mutex_concepts.upgrade_lockable.unlock_upgrade_and_lock_shared `unlock_upgrade_and_lock_shared`]]
|
||||
|
||||
|
||||
[def __try_unlock_shared_and_lock [link thread.synchronization.mutex_concepts.upgrade_lockable.try_unlock_shared_and_lock `try_unlock_shared_and_lock`]]
|
||||
[def __try_unlock_shared_and_lock_for [link thread.synchronization.mutex_concepts.upgrade_lockable.try_unlock_shared_and_lock_for `try_unlock_shared_and_lock_for`]]
|
||||
[def __try_unlock_shared_and_lock_until [link thread.synchronization.mutex_concepts.upgrade_lockable.try_unlock_shared_and_lock_until `try_unlock_shared_and_lock_until`]]
|
||||
|
||||
[def __unlock_and_lock_shared [link thread.synchronization.mutex_concepts.upgrade_lockable.unlock_and_lock_shared `unlock_and_lock_shared`]]
|
||||
|
||||
[def __try_unlock_shared_and_lock_upgrade [link thread.synchronization.mutex_concepts.upgrade_lockable.try_unlock_shared_and_lock_upgrade `try_unlock_shared_and_lock_upgrade`]]
|
||||
[def __try_unlock_shared_and_lock_upgrade_for [link thread.synchronization.mutex_concepts.upgrade_lockable.try_unlock_shared_and_lock_upgrade_for `try_unlock_shared_and_lock_upgrade_for`]]
|
||||
[def __try_unlock_shared_and_lock_upgrade_until [link thread.synchronization.mutex_concepts.upgrade_lockable.try_unlock_shared_and_lock_upgrade_until `try_unlock_shared_and_lock_upgrade_until`]]
|
||||
|
||||
[def __try_unlock_upgrade_and_lock [link thread.synchronization.mutex_concepts.upgrade_lockable.try_unlock_upgrade_and_lock `try_unlock_upgrade_and_lock`]]
|
||||
[def __try_unlock_upgrade_and_lock_for [link thread.synchronization.mutex_concepts.upgrade_lockable.try_unlock_upgrade_and_lock_for `try_unlock_upgrade_and_lock_for`]]
|
||||
[def __try_unlock_upgrade_and_lock_until [link thread.synchronization.mutex_concepts.upgrade_lockable.try_unlock_upgrade_and_lock_until `try_unlock_upgrade_and_lock_until`]]
|
||||
|
||||
|
||||
[template owns_lock_ref_link[link_text] [link thread.synchronization.locks.unique_lock.owns_lock [link_text]]]
|
||||
[def __owns_lock_ref__ [owns_lock_ref_link `owns_lock()`]]
|
||||
[def __owns_lock [owns_lock_ref_link `owns_lock()`]]
|
||||
|
||||
[template owns_lock_shared_ref_link[link_text] [link thread.synchronization.locks.shared_lock.owns_lock [link_text]]]
|
||||
[def __owns_lock_shared_ref__ [owns_lock_shared_ref_link `owns_lock()`]]
|
||||
|
||||
[template mutex_func_ref_link[link_text] [link thread.synchronization.locks.unique_lock.mutex [link_text]]]
|
||||
[def __mutex_func_ref__ [mutex_func_ref_link `mutex()`]]
|
||||
|
||||
[def __boost_thread__ [*Boost.Thread]]
|
||||
[def __not_a_thread__ ['Not-a-Thread]]
|
||||
[def __interruption_points__ [link interruption_points ['interruption points]]]
|
||||
[def __lock_error__ `lock_error`]
|
||||
|
||||
[def __mutex__ [link thread.synchronization.mutex_types.mutex `boost::mutex`]]
|
||||
[def __try_mutex__ [link thread.synchronization.mutex_types.try_mutex `boost::try_mutex`]]
|
||||
[def __timed_mutex__ [link thread.synchronization.mutex_types.timed_mutex `boost::timed_mutex`]]
|
||||
[def __recursive_mutex__ [link thread.synchronization.mutex_types.recursive_mutex `boost::recursive_mutex`]]
|
||||
[def __recursive_try_mutex__ [link thread.synchronization.mutex_types.recursive_try_mutex `boost::recursive_try_mutex`]]
|
||||
[def __recursive_timed_mutex__ [link thread.synchronization.mutex_types.recursive_timed_mutex `boost::recursive_timed_mutex`]]
|
||||
[def __shared_mutex__ [link thread.synchronization.mutex_types.shared_mutex `boost::shared_mutex`]]
|
||||
|
||||
|
||||
[def __StrictLock [link thread.synchronization.lock_concepts.StrictLock `StrictLock`]]
|
||||
|
||||
[template unique_lock_link[link_text] [link thread.synchronization.locks.unique_lock [link_text]]]
|
||||
|
||||
[def __lock_guard__ [link thread.synchronization.lock_guard.lock_guard `boost::lock_guard`]]
|
||||
[def __unique_lock__ [unique_lock_link `boost::unique_lock`]]
|
||||
[def __unique_lock [unique_lock_link `boost::unique_lock`]]
|
||||
[def __shared_lock__ [link thread.synchronization.locks.shared_lock `boost::shared_lock`]]
|
||||
[def __upgrade_lock__ [link thread.synchronization.locks.upgrade_lock `boost::upgrade_lock`]]
|
||||
[def __upgrade_to_unique_lock__ [link thread.synchronization.locks.upgrade_to_unique_lock `boost::upgrade_to_unique_lock`]]
|
||||
[def __reverse_lock [link thread.synchronization.other_locks.reverse_lock `reverse_lock`]]
|
||||
[def __shared_lock_guard [link thread.synchronization.other_locks.shared_lock_guard `shared_lock_guard`]]
|
||||
[def __shared_lock_guard_constructor_adopt [link thread.synchronization.other_locks.shared_lock_guard `shared_lock_guard`]]
|
||||
|
||||
[def __strict_lock [link thread.synchronization.other_locks.strict_locks.strict_lock `strict_lock`]]
|
||||
[def __nested_strict_lock [link thread.synchronization.other_locks.strict_locks.nested_strict_lock `nested_strict_lock`]]
|
||||
|
||||
|
||||
[def __thread__ [link thread.thread_management.thread `boost::thread`]]
|
||||
[def __thread [link thread.thread_management.thread `thread`]]
|
||||
[def __thread_id__ [link thread.thread_management.thread.id `boost::thread::id`]]
|
||||
[template join_link[link_text] [link thread.thread_management.thread.join [link_text]]]
|
||||
[def __join__ [join_link `join()`]]
|
||||
|
||||
[def __try_join_for [link thread.thread_management.thread.try_join_for `try_join_for`]]
|
||||
[def __try_join_until [link thread.thread_management.thread.try_join_until `try_join_until`]]
|
||||
|
||||
[template timed_join_link[link_text] [link thread.thread_management.thread.timed_join [link_text]]]
|
||||
[def __timed_join__ [timed_join_link `timed_join()`]]
|
||||
[def __detach__ [link thread.thread_management.thread.detach `detach()`]]
|
||||
[def __interrupt__ [link thread.thread_management.thread.interrupt `interrupt()`]]
|
||||
[def __sleep__ [link thread.thread_management.this_thread.sleep `boost::this_thread::sleep()`]]
|
||||
[def __sleep_for [link thread.thread_management.this_thread.sleep_for `sleep_for`]]
|
||||
[def __sleep_until [link thread.thread_management.this_thread.sleep_until `sleep_until`]]
|
||||
[def __yield [link thread.thread_management.this_thread.yield `yield`]]
|
||||
[def __get_id [link thread.thread_management.thread.get_id `get_id`]]
|
||||
|
||||
[def __interruption_enabled__ [link thread.thread_management.this_thread.interruption_enabled `boost::this_thread::interruption_enabled()`]]
|
||||
[def __interruption_requested__ [link thread.thread_management.this_thread.interruption_requested `boost::this_thread::interruption_requested()`]]
|
||||
[def __interruption_point__ [link thread.thread_management.this_thread.interruption_point `boost::this_thread::interruption_point()`]]
|
||||
[def __disable_interruption__ [link thread.thread_management.this_thread.disable_interruption `boost::this_thread::disable_interruption`]]
|
||||
[def __restore_interruption__ [link thread.thread_management.this_thread.restore_interruption `boost::this_thread::restore_interruption`]]
|
||||
|
||||
[def __thread_resource_error__ `boost::thread_resource_error`]
|
||||
[def __thread_interrupted__ `boost::thread_interrupted`]
|
||||
[def __barrier__ [link thread.synchronization.barriers.barrier `boost::barrier`]]
|
||||
|
||||
[template cond_wait_link[link_text] [link thread.synchronization.condvar_ref.condition_variable.wait [link_text]]]
|
||||
[def __cond_wait__ [cond_wait_link `wait()`]]
|
||||
[template cond_timed_wait_link[link_text] [link thread.synchronization.condvar_ref.condition_variable.timed_wait [link_text]]]
|
||||
[def __cond_timed_wait__ [cond_timed_wait_link `timed_wait()`]]
|
||||
|
||||
[def __condition_variable [link thread.synchronization.condvar_ref.condition_variable `condition_variable`]]
|
||||
[def __wait_for [link thread.synchronization.condvar_ref.condition_variable.wait_for `wait_for`]]
|
||||
[def __wait_until [link thread.synchronization.condvar_ref.condition_variable.wait_until `wait_until`]]
|
||||
|
||||
|
||||
[template cond_any_wait_link[link_text] [link thread.synchronization.condvar_ref.condition_variable_any.wait [link_text]]]
|
||||
[def __cond_any_wait__ [cond_any_wait_link `wait()`]]
|
||||
[template cond_any_timed_wait_link[link_text] [link thread.synchronization.condvar_ref.condition_variable_any.timed_wait [link_text]]]
|
||||
[def __cond_any_timed_wait__ [cond_any_timed_wait_link `timed_wait()`]]
|
||||
|
||||
[def __condition_variable_any [link thread.synchronization.condvar_ref.condition_variable_any `condition_variable_any`]]
|
||||
[def __cvany_wait_for [link thread.synchronization.condvar_ref.condition_variable_any.wait_for `wait_for`]]
|
||||
[def __cvany_wait_until [link thread.synchronization.condvar_ref.condition_variable_any.wait_until `wait_until`]]
|
||||
|
||||
[def __blocked__ ['blocked]]
|
||||
|
||||
[include overview.qbk]
|
||||
[include changes.qbk]
|
||||
|
||||
[include thread_ref.qbk]
|
||||
[include scoped_thread.qbk]
|
||||
|
||||
[section:synchronization Synchronization]
|
||||
[include sync_tutorial.qbk]
|
||||
[include mutex_concepts.qbk]
|
||||
[include mutexes.qbk]
|
||||
[include condition_variables.qbk]
|
||||
[include once.qbk]
|
||||
[include barrier.qbk]
|
||||
[include futures.qbk]
|
||||
[endsect]
|
||||
|
||||
[include tss.qbk]
|
||||
|
||||
[include time.qbk]
|
||||
|
||||
[include emulations.qbk]
|
||||
|
||||
[include acknowledgements.qbk]
|
||||
|
||||
[include compliance.qbk]
|
||||
@@ -1,182 +0,0 @@
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content=
|
||||
"text/html; charset=iso-8859-1">
|
||||
<meta name="keywords" content="threads, BTL, thread library, C++">
|
||||
<link rel="stylesheet" type="text/css" href="styles.css">
|
||||
|
||||
<title>Boost.Threads, thread_group</title>
|
||||
</head>
|
||||
|
||||
<body bgcolor="#FFFFFF" link="#0000FF" vlink="#800080">
|
||||
<table summary="header" border="0" cellpadding="7" cellspacing="0"
|
||||
width="100%">
|
||||
<tr>
|
||||
<td valign="top" width="300">
|
||||
<h3><img src="../../../c++boost.gif" alt="C++ Boost" width=
|
||||
"277" height="86"></h3>
|
||||
</td>
|
||||
|
||||
<td valign="top">
|
||||
<h1 align="center">Boost.Threads</h1>
|
||||
|
||||
<h2 align="center">thread_group</h2>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<hr>
|
||||
|
||||
<p><a href="#Introduction">Introduction</a><br>
|
||||
<a href="#Header">Header</a><br>
|
||||
<a href="#Synopsis">Synopsis</a><br>
|
||||
<a href="#Members">Members</a><br>
|
||||
<a href="#Example">Example</a></p>
|
||||
|
||||
<h2><a name="Introduction">Introduction</a></h2>
|
||||
|
||||
<p>The <tt>thread_group</tt> class provides a container for easy
|
||||
grouping of threads to simplify several common thread creation and
|
||||
management idioms.</p>
|
||||
|
||||
<p>All <tt>thread_group</tt> member functions are <a href=
|
||||
"definitions.html#thread-safe">thread-safe</a>, except destruction.</p>
|
||||
|
||||
<h2><a name="Header">Header</a></h2>
|
||||
<pre>
|
||||
#include <a href=
|
||||
"../../../boost/thread/thread.hpp"><boost/thread/thread.hpp></a>
|
||||
</pre>
|
||||
|
||||
<h2><a name="Synopsis">Synopsis</a></h2>
|
||||
<pre>
|
||||
namespace boost
|
||||
{
|
||||
class thread_group : <a href=
|
||||
"../../utility/utility.htm#noncopyable">boost::noncopyable</a>
|
||||
{
|
||||
public:
|
||||
thread_group();
|
||||
~thread_group();
|
||||
|
||||
thread* create_thread(const boost::function0<void>& threadfunc);
|
||||
void add_thread(thread* thrd);
|
||||
void remove_thread(thread* thrd);
|
||||
void join_all();
|
||||
};
|
||||
}
|
||||
</pre>
|
||||
|
||||
<h2><a name="Members">Members</a></h2>
|
||||
<hr>
|
||||
|
||||
<h3>Constructor</h3>
|
||||
<pre>
|
||||
thread_group();
|
||||
</pre>
|
||||
|
||||
<p><b>Effects:</b> Constructs an empty <tt>thread_group</tt>
|
||||
container.</p>
|
||||
<hr>
|
||||
|
||||
<h3>Destructor</h3>
|
||||
<pre>
|
||||
~thread_group();
|
||||
</pre>
|
||||
|
||||
<p><b>Effects:</b> Destroys each contained thread object. Destroys
|
||||
<code>*this</code>.</p>
|
||||
|
||||
<p><b>Notes:</b> Behavior is undefined if another thread references
|
||||
*this during the execution of the destructor.</p>
|
||||
<hr>
|
||||
|
||||
<h3>create_thread</h3>
|
||||
<pre>
|
||||
thread* create_thread(const boost::function0<void>& threadfunc);
|
||||
</pre>
|
||||
|
||||
<p><b>Effects:</b> Creates a new <tt>thread</tt> object that executes
|
||||
<tt>threadfunc</tt> and adds it to the <tt>thread_group</tt> container
|
||||
object's list of managed <tt>thread</tt> objects.</p>
|
||||
|
||||
<p><b>Returns:</b> Pointer to the newly created thread.</p>
|
||||
<hr>
|
||||
|
||||
<h3>add_thread</h3>
|
||||
<pre>
|
||||
void add_thread(thread* thrd);
|
||||
</pre>
|
||||
|
||||
<p><b>Effects:</b> Adds <tt>thrd</tt> to the <tt>thread_group</tt>
|
||||
object's list of managed <tt>thread</tt> objects. The <tt>thrd</tt>
|
||||
object must have been allocated via operator new and will be deleted
|
||||
when the group is destroyed.</p>
|
||||
<hr>
|
||||
|
||||
<h3>remove_thread</h3>
|
||||
<pre>
|
||||
void remove_thread(thread* thrd);
|
||||
</pre>
|
||||
|
||||
<p><b>Effects:</b> Removes <code>*this</code>'s list of managed
|
||||
<tt>thread</tt> objects.</p>
|
||||
|
||||
<p><b>Throws:</b> ? if <tt>thrd</tt> is not it <code>*this</code>'s
|
||||
list of managed <tt>thread</tt> objects.</p>
|
||||
<hr>
|
||||
|
||||
<h3>join_all</h3>
|
||||
<pre>
|
||||
void join_all();
|
||||
</pre>
|
||||
|
||||
<p><b>Effects:</b> Calls <code>join()</code> on each of the managed
|
||||
<tt>thread</tt> objects.</p>
|
||||
<hr>
|
||||
|
||||
<h2><a name="Example">Example</a> Usage</h2>
|
||||
<pre>
|
||||
#include <boost/thread/thread.hpp>
|
||||
#include <iostream>
|
||||
|
||||
int count = 0;
|
||||
boost::mutex mutex;
|
||||
|
||||
void increment_count()
|
||||
{
|
||||
boost::mutex::lock lock(mutex);
|
||||
std::cout << "count = " << ++count << std::endl;
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
boost::thread_group threads;
|
||||
for (int i = 0; i < 10; ++i)
|
||||
threads.create_thread(&increment_count);
|
||||
threads.join_all();
|
||||
}
|
||||
</pre>
|
||||
|
||||
<p>The output is:</p>
|
||||
<pre>
|
||||
count = 1
|
||||
count = 2
|
||||
count = 3
|
||||
count = 4
|
||||
count = 5
|
||||
count = 6
|
||||
count = 7
|
||||
count = 8
|
||||
count = 9
|
||||
count = 10
|
||||
</pre>
|
||||
<hr>
|
||||
|
||||
<p>Revised
|
||||
<!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan -->05 November, 2001<!--webbot bot="Timestamp" endspan i-checksum="39359" --></p>
|
||||
|
||||
<p><i>© Copyright <a href="mailto:williamkempf@hotmail.com">
|
||||
William E. Kempf</a> 2001 all rights reserved.</i></p>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
1880
doc/thread_ref.qbk
Normal file
1880
doc/thread_ref.qbk
Normal file
File diff suppressed because it is too large
Load Diff
@@ -1,78 +0,0 @@
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content=
|
||||
"text/html; charset=iso-8859-1">
|
||||
<meta name="keywords" content="threads, BTL, thread library, C++">
|
||||
<link rel="stylesheet" type="text/css" href="styles.css">
|
||||
|
||||
<title>Boost.Threads, thread_resource_error</title>
|
||||
</head>
|
||||
|
||||
<body bgcolor="#FFFFFF" link="#0000FF" vlink="#800080">
|
||||
<table summary="header" border="0" cellpadding="7" cellspacing="0" width="100%">
|
||||
<tr>
|
||||
<td valign="top" width="300">
|
||||
<h3><img src="../../../c++boost.gif" alt="C++ Boost" width=
|
||||
"277" height="86"></h3>
|
||||
</td>
|
||||
|
||||
<td valign="top">
|
||||
<h1 align="center">Boost.Threads</h1>
|
||||
|
||||
<h2 align="center">thread_resource_error</h2>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<hr>
|
||||
|
||||
<p><a href="#Introduction">Introduction</a><br>
|
||||
<a href="#Header">Header</a><br>
|
||||
<a href="#Synopsis">Synopsis</a><br>
|
||||
<a href="#Members">Members</a><br>
|
||||
<a href="#Example">Example</a></p>
|
||||
|
||||
<h2><a name="Introduction">Introduction</a></h2>
|
||||
|
||||
<p>The <code>thread_resource_error</code> class defines an exception
|
||||
type that is thrown by constructors in the <b>Boost.Threads</b> library
|
||||
when thread related resources can not be acquired. This does not
|
||||
include memory allocation failures which instead throw
|
||||
std::bad_alloc.</p>
|
||||
|
||||
<h2><a name="Header">Header</a></h2>
|
||||
<pre>
|
||||
#include <a href=
|
||||
"../../../boost/thread/thread.hpp"><boost/thread/exceptions.hpp></a>
|
||||
</pre>
|
||||
|
||||
<h2><a name="Synopsis">Synopsis</a></h2>
|
||||
<pre>
|
||||
namespace boost
|
||||
{
|
||||
class thread_resource_error : public std::runtime_error
|
||||
{
|
||||
public:
|
||||
thread_resource_error();
|
||||
};
|
||||
}
|
||||
</pre>
|
||||
|
||||
<h2><a name="Members">Members</a></h2>
|
||||
<hr>
|
||||
|
||||
<h3>Constructor</h3>
|
||||
<pre>
|
||||
thread_resource_error();
|
||||
</pre>
|
||||
|
||||
<p>Constructs a <code>thread_resource_error</code> object.</p>
|
||||
<hr>
|
||||
|
||||
<p>Revised
|
||||
<!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan -->05 November, 2001<!--webbot bot="Timestamp" endspan i-checksum="39359" --></p>
|
||||
|
||||
<p><i>© Copyright <a href="mailto:williamkempf@hotmail.com">
|
||||
William E. Kempf</a> 2001 all rights reserved.</i></p>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -1,223 +0,0 @@
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content=
|
||||
"text/html; charset=iso-8859-1">
|
||||
<meta name="keywords" content=
|
||||
"threads, Boost.Threads, thread library, C++">
|
||||
<link rel="stylesheet" type="text/css" href="styles.css">
|
||||
|
||||
<title>Boost.Threads, thread_specific_ptr</title>
|
||||
</head>
|
||||
|
||||
<body bgcolor="#FFFFFF" link="#0000FF" vlink="#800080">
|
||||
<table summary="header" border="0" cellpadding="7" cellspacing="0"
|
||||
width="100%">
|
||||
<tr>
|
||||
<td valign="top" width="300">
|
||||
<h3><img src="../../../c++boost.gif" alt="C++ Boost" width=
|
||||
"277" height="86"></h3>
|
||||
</td>
|
||||
|
||||
<td valign="top">
|
||||
<h1 align="center">Boost.Threads</h1>
|
||||
|
||||
<h2 align="center">thread_specific_ptr</h2>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<hr>
|
||||
|
||||
<p><a href="#Introduction">Introduction</a><br>
|
||||
<a href="#Header">Header</a><br>
|
||||
<a href="#Synopsis">Synopsis</a><br>
|
||||
<a href="#Members">Members</a><br>
|
||||
<a href="#Example">Example</a></p>
|
||||
|
||||
<h2><a name="Introduction">Introduction</a></h2>
|
||||
|
||||
<p>The <code>thread_specific_ptr</code> class defines an interface for
|
||||
using thread specific storage. Thread specific storage is data
|
||||
associated with individual threads and is often used to make operations
|
||||
<a href="definitions.html#Thread-safe">thread-safe</a> that rely on
|
||||
global data.</p>
|
||||
|
||||
<p>Template <code>thread_specific_ptr</code> stores a pointer to an
|
||||
object obtained via <code>new</code> on a thread-by-thread basis and
|
||||
calls delete on the contained pointer when the thread terminates. Each
|
||||
thread initially stores the null pointer in each <code>
|
||||
thread_specific_ptr</code> instance.</p>
|
||||
|
||||
<p>The template <code>thread_specific_ptr</code> is useful in the
|
||||
following cases:</p>
|
||||
|
||||
<ul>
|
||||
<li>An interface was original written assuming a single thread of
|
||||
control and is being ported to a multi-threaded environment.</li>
|
||||
|
||||
<li>Each thread of control invokes sequences of methods that share
|
||||
data that must be logically accessed through a globally visible
|
||||
access point, but are physically unique for each thread, instead of
|
||||
being explicitly passed.</li>
|
||||
</ul>
|
||||
|
||||
<h2><a name="Header">Header</a></h2>
|
||||
<pre>
|
||||
#include <a href=
|
||||
"../../../boost/thread/tss.hpp"><boost/thread/tss.hpp></a>
|
||||
</pre>
|
||||
|
||||
<h2><a name="Synopsis">Synopsis</a></h2>
|
||||
<pre>
|
||||
namespace boost {
|
||||
|
||||
template <typename T>
|
||||
class thread_specific_ptr : private boost::noncopyable // Exposition only.
|
||||
// Class thread_specific_ptr meets the <a href=
|
||||
"overview.html#NonCopyable">NonCopyable</a> requirement.
|
||||
{
|
||||
public:
|
||||
thread_specific_ptr();
|
||||
~thread_specific_ptr();
|
||||
|
||||
T* get() const;
|
||||
T* operator->() const;
|
||||
T& operator*() const;
|
||||
T* release();
|
||||
void reset(T* p=0);
|
||||
};
|
||||
|
||||
} // namespace boost
|
||||
</pre>
|
||||
|
||||
<h2><a name="Members">Members</a></h2>
|
||||
<hr>
|
||||
|
||||
<h3>Constructor</h3>
|
||||
<pre>
|
||||
thread_specific_ptr();
|
||||
</pre>
|
||||
|
||||
<p><b>Postconditions:</b> A thread specific storage has been reserved
|
||||
for use by *this in all threads, with each thread initially storing a
|
||||
null pointer.</p>
|
||||
|
||||
<p><b>Requires:</b> The expression <code>delete get()</code> is well
|
||||
formed.</p>
|
||||
|
||||
<p><b>Throws:</b> <code>boost::thread_resource_error</code> if the
|
||||
necessary resources can not be obtained.</p>
|
||||
|
||||
<p><b>Notes:</b> There is an implementation specific limit to the
|
||||
number of thread specific storage objects that can be created, and this
|
||||
limit may be small.</p>
|
||||
<hr>
|
||||
|
||||
<h3>Destructor</h3>
|
||||
<pre>
|
||||
~thread_specific_ptr();
|
||||
</pre>
|
||||
|
||||
<p><b>Notes:</b> Does not destroy any data that may be stored in any
|
||||
thread's thread specific storage. For this reason you should not
|
||||
destroy a <code>thread_specific_ptr</code> object until you are certain
|
||||
there are no threads running that have made use of its thread specific
|
||||
storage.</p>
|
||||
<hr>
|
||||
|
||||
<h3>get</h3>
|
||||
<pre>
|
||||
T* get() const;
|
||||
</pre>
|
||||
|
||||
<p><b>Returns:</b> The object stored in thread specific storage for the
|
||||
current thread for *this.</p>
|
||||
|
||||
<p><b>Notes:</b> Each thread initially returns 0.</p>
|
||||
<hr>
|
||||
|
||||
<h3>Smart Pointer Operations</h3>
|
||||
<pre>
|
||||
T* operator->() const;
|
||||
</pre>
|
||||
|
||||
<p><b>Returns:</b> <code>get()</code></p>
|
||||
<pre>
|
||||
T& operator*() const;
|
||||
</pre>
|
||||
|
||||
<p><b>Returns:</b> <code>get()</code></p>
|
||||
|
||||
<p><b>Requires:</b> <code>get() != 0</code></p>
|
||||
<hr>
|
||||
|
||||
<h3>Release</h3>
|
||||
<pre>
|
||||
T* release();
|
||||
</pre>
|
||||
|
||||
<p><b>Returns:</b> <code>get()</code></p>
|
||||
|
||||
<p><b>Postcondition:</b> *this holds the null pointer for the current
|
||||
thread.</p>
|
||||
<hr>
|
||||
|
||||
<h3>Reset</h3>
|
||||
<pre>
|
||||
void reset(T* p=0);
|
||||
</pre>
|
||||
|
||||
<p><b>Effects:</b> If <code>get()!= p</code> then <code>delete
|
||||
get()</code>.</p>
|
||||
|
||||
<p><b>Postconditions:</b> <code>*this</code> holds the pointer <code>
|
||||
p</code> for the current thread.</p>
|
||||
|
||||
<p><b>Notes:</b> The pointer will be deleted when the thread
|
||||
terminates.</p>
|
||||
<hr>
|
||||
|
||||
<h2><a name="Example">Example Usage</a></h2>
|
||||
<pre>
|
||||
#include <a href=
|
||||
"../../../boost/thread/thread.hpp"><boost/thread/thread.hpp></a>
|
||||
#include <a href=
|
||||
"../../../boost/thread/tss.hpp"><boost/thread/tss.hpp></a>
|
||||
#include <cassert>
|
||||
|
||||
boost::thread_specific_ptr<int> value;
|
||||
|
||||
void increment()
|
||||
{
|
||||
int* p = value.get();
|
||||
++*p;
|
||||
}
|
||||
|
||||
void thread_proc()
|
||||
{
|
||||
value.reset(new int(0)); // initialize the thread's storage
|
||||
for (int i=0; i<10; ++i)
|
||||
{
|
||||
increment();
|
||||
int* p = value.get();
|
||||
assert(*p == i+1);
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
boost::thread_group threads;
|
||||
for (int i=0; i<5; ++i)
|
||||
threads.create_thread(&thread_proc);
|
||||
threads.join_all();
|
||||
}
|
||||
</pre>
|
||||
<hr>
|
||||
|
||||
<p>Revised
|
||||
<!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan -->05 November, 2001<!--webbot bot="Timestamp" endspan i-checksum="39359" --></p>
|
||||
|
||||
<p><i>© Copyright <a href="mailto:williamkempf@hotmail.com">
|
||||
William E. Kempf</a> 2001 all rights reserved.</i></p>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
90
doc/time.qbk
Normal file
90
doc/time.qbk
Normal file
@@ -0,0 +1,90 @@
|
||||
[/
|
||||
(C) Copyright 2007-8 Anthony Williams.
|
||||
Distributed under the Boost Software License, Version 1.0.
|
||||
(See accompanying file LICENSE_1_0.txt or copy at
|
||||
http://www.boost.org/LICENSE_1_0.txt).
|
||||
]
|
||||
|
||||
[section:time Time Requirements]
|
||||
|
||||
As of Boost 1.50.0, the __boost_thread__ library uses Boost.Chrono library for all operations that require a
|
||||
time out as defined in the standard c++11. These include (but are not limited to):
|
||||
|
||||
* `boost::this_thread::__sleep_for`
|
||||
* `boost::this_thread::__sleep_until`
|
||||
* `boost::__thread::__try_join_for`
|
||||
* `boost::__thread::__try_join_until`
|
||||
* `boost::__condition_variable::__wait_for`
|
||||
* `boost::__condition_variable::__wait_until`
|
||||
* `boost::__condition_variable_any::__cvany_wait_for`
|
||||
* `boost::__condition_variable_any::__cvany_wait_until`
|
||||
* `__TimedLockable::__try_lock_for`
|
||||
* `__TimedLockable::__try_lock_until`
|
||||
|
||||
[section:deprecated Deprecated]
|
||||
The time related functions introduced in Boost 1.35.0, using the [link date_time Boost.Date_Time] library are deprecated. These include (but are not limited to):
|
||||
|
||||
* __sleep__
|
||||
* __timed_join__
|
||||
* __cond_timed_wait__
|
||||
* __timed_lock_ref__
|
||||
|
||||
For the overloads that accept an absolute time parameter, an object of type [link thread.time.deprecated.system_time `boost::system_time`] is
|
||||
required. Typically, this will be obtained by adding a duration to the current time, obtained with a call to [link
|
||||
thread.time.deprecated.get_system_time `boost::get_system_time()`]. e.g.
|
||||
|
||||
boost::system_time const timeout=boost::get_system_time() + boost::posix_time::milliseconds(500);
|
||||
|
||||
extern bool done;
|
||||
extern boost::mutex m;
|
||||
extern boost::condition_variable cond;
|
||||
|
||||
boost::unique_lock<boost::mutex> lk(m);
|
||||
while(!done)
|
||||
{
|
||||
if(!cond.timed_wait(lk,timeout))
|
||||
{
|
||||
throw "timed out";
|
||||
}
|
||||
}
|
||||
|
||||
For the overloads that accept a ['TimeDuration] parameter, an object of any type that meets the [link
|
||||
date_time.posix_time.time_duration Boost.Date_Time Time Duration requirements] can be used, e.g.
|
||||
|
||||
boost::this_thread::sleep(boost::posix_time::milliseconds(25));
|
||||
|
||||
boost::mutex m;
|
||||
if(m.timed_lock(boost::posix_time::nanoseconds(100)))
|
||||
{
|
||||
// ...
|
||||
}
|
||||
|
||||
[section:system_time Typedef `system_time`]
|
||||
|
||||
#include <boost/thread/thread_time.hpp>
|
||||
|
||||
typedef boost::posix_time::ptime system_time;
|
||||
|
||||
See the documentation for [link date_time.posix_time.ptime_class `boost::posix_time::ptime`] in the Boost.Date_Time library.
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:get_system_time Non-member function `get_system_time()`]
|
||||
|
||||
#include <boost/thread/thread_time.hpp>
|
||||
|
||||
system_time get_system_time();
|
||||
|
||||
[variablelist
|
||||
|
||||
[[Returns:] [The current time.]]
|
||||
|
||||
[[Throws:] [Nothing.]]
|
||||
|
||||
]
|
||||
|
||||
[endsect]
|
||||
[endsect]
|
||||
|
||||
|
||||
[endsect]
|
||||
200
doc/tss.qbk
Normal file
200
doc/tss.qbk
Normal file
@@ -0,0 +1,200 @@
|
||||
[/
|
||||
(C) Copyright 2007-8 Anthony Williams.
|
||||
Distributed under the Boost Software License, Version 1.0.
|
||||
(See accompanying file LICENSE_1_0.txt or copy at
|
||||
http://www.boost.org/LICENSE_1_0.txt).
|
||||
]
|
||||
|
||||
[section Thread Local Storage]
|
||||
|
||||
[heading Synopsis]
|
||||
|
||||
Thread local storage allows multi-threaded applications to have a separate instance of a given data item for each thread. Where a
|
||||
single-threaded application would use static or global data, this could lead to contention, deadlock or data corruption in a
|
||||
multi-threaded application. One example is the C `errno` variable, used for storing the error code related to functions from the
|
||||
Standard C library. It is common practice (and required by POSIX) for compilers that support multi-threaded applications to provide
|
||||
a separate instance of `errno` for each thread, in order to avoid different threads competing to read or update the value.
|
||||
|
||||
Though compilers often provide this facility in the form of extensions to the declaration syntax (such as `__declspec(thread)` or
|
||||
`__thread` annotations on `static` or namespace-scope variable declarations), such support is non-portable, and is often limited in
|
||||
some way, such as only supporting POD types.
|
||||
|
||||
[heading Portable thread-local storage with `boost::thread_specific_ptr`]
|
||||
|
||||
`boost::thread_specific_ptr` provides a portable mechanism for thread-local storage that works on all compilers supported by
|
||||
__boost_thread__. Each instance of `boost::thread_specific_ptr` represents a pointer to an object (such as `errno`) where each
|
||||
thread must have a distinct value. The value for the current thread can be obtained using the `get()` member function, or by using
|
||||
the `*` and `->` pointer deference operators. Initially the pointer has a value of `NULL` in each thread, but the value for the
|
||||
current thread can be set using the `reset()` member function.
|
||||
|
||||
If the value of the pointer for the current thread is changed using `reset()`, then the previous value is destroyed by calling the
|
||||
cleanup routine. Alternatively, the stored value can be reset to `NULL` and the prior value returned by calling the `release()`
|
||||
member function, allowing the application to take back responsibility for destroying the object.
|
||||
|
||||
[heading Cleanup at thread exit]
|
||||
|
||||
When a thread exits, the objects associated with each `boost::thread_specific_ptr` instance are destroyed. By default, the object
|
||||
pointed to by a pointer `p` is destroyed by invoking `delete p`, but this can be overridden for a specific instance of
|
||||
`boost::thread_specific_ptr` by providing a cleanup routine to the constructor. In this case, the object is destroyed by invoking
|
||||
`func(p)` where `func` is the cleanup routine supplied to the constructor. The cleanup functions are called in an unspecified
|
||||
order. If a cleanup routine sets the value of associated with an instance of `boost::thread_specific_ptr` that has already been
|
||||
cleaned up, that value is added to the cleanup list. Cleanup finishes when there are no outstanding instances of
|
||||
`boost::thread_specific_ptr` with values.
|
||||
|
||||
Note: on some platforms, cleanup of thread-specific data is not
|
||||
performed for threads created with the platform's native API. On those
|
||||
platforms such cleanup is only done for threads that are started with
|
||||
`boost::thread` unless `boost::on_thread_exit()` is called manually
|
||||
from that thread.
|
||||
|
||||
[heading Rationale about the nature of the key]
|
||||
|
||||
Boost.Thread uses the address of the `thread_specific_ptr` instance as key of the thread specific pointers. This avoids to create/destroy a key which will need a lock to protect from race conditions. This has a little performance liability, as the access must be done using an associative container.
|
||||
|
||||
[section:thread_specific_ptr Class `thread_specific_ptr`]
|
||||
|
||||
// #include <boost/thread/tss.hpp>
|
||||
|
||||
namespace boost
|
||||
{
|
||||
template <typename T>
|
||||
class thread_specific_ptr
|
||||
{
|
||||
public:
|
||||
thread_specific_ptr();
|
||||
explicit thread_specific_ptr(void (*cleanup_function)(T*));
|
||||
~thread_specific_ptr();
|
||||
|
||||
T* get() const;
|
||||
T* operator->() const;
|
||||
T& operator*() const;
|
||||
|
||||
T* release();
|
||||
void reset(T* new_value=0);
|
||||
};
|
||||
}
|
||||
|
||||
[section:default_constructor `thread_specific_ptr();`]
|
||||
|
||||
[variablelist
|
||||
|
||||
[[Requires:] [`delete this->get()` is well-formed.]]
|
||||
|
||||
[[Effects:] [Construct a `thread_specific_ptr` object for storing a pointer to an object of type `T` specific to each thread. The
|
||||
default `delete`-based cleanup function will be used to destroy any thread-local objects when `reset()` is called, or the thread
|
||||
exits.]]
|
||||
|
||||
[[Throws:] [`boost::thread_resource_error` if an error occurs.]]
|
||||
|
||||
]
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:constructor_with_custom_cleanup `explicit thread_specific_ptr(void (*cleanup_function)(T*));`]
|
||||
|
||||
[variablelist
|
||||
|
||||
[[Requires:] [`cleanup_function(this->get())` does not throw any exceptions.]]
|
||||
|
||||
[[Effects:] [Construct a `thread_specific_ptr` object for storing a pointer to an object of type `T` specific to each thread. The
|
||||
supplied `cleanup_function` will be used to destroy any thread-local objects when `reset()` is called, or the thread exits.]]
|
||||
|
||||
[[Throws:] [`boost::thread_resource_error` if an error occurs.]]
|
||||
|
||||
]
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:destructor `~thread_specific_ptr();`]
|
||||
|
||||
[variablelist
|
||||
|
||||
[[Requires:] [All the thread specific instances associated to this thread_specific_ptr (except maybe the one associated to this thread) must be null.]]
|
||||
|
||||
[[Effects:] [Calls `this->reset()` to clean up the associated value for the current thread, and destroys `*this`.]]
|
||||
|
||||
[[Throws:] [Nothing.]]
|
||||
|
||||
[[Remarks:] [The requirement is due to the fact that in order to delete all these instances, the implementation should be forced to maintain a list of all the threads having an associated specific ptr, which is against the goal of thread specific data.]]
|
||||
|
||||
]
|
||||
|
||||
[note Care needs to be taken to ensure that any threads still running after an instance of `boost::thread_specific_ptr` has been
|
||||
destroyed do not call any member functions on that instance.]
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:get `T* get() const;`]
|
||||
|
||||
[variablelist
|
||||
|
||||
[[Returns:] [The pointer associated with the current thread.]]
|
||||
|
||||
[[Throws:] [Nothing.]]
|
||||
|
||||
]
|
||||
|
||||
[note The initial value associated with an instance of `boost::thread_specific_ptr` is `NULL` for each thread.]
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:operator_arrow `T* operator->() const;`]
|
||||
|
||||
[variablelist
|
||||
|
||||
[[Returns:] [`this->get()`]]
|
||||
|
||||
[[Throws:] [Nothing.]]
|
||||
|
||||
]
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:operator_star `T& operator*() const;`]
|
||||
|
||||
[variablelist
|
||||
|
||||
[[Requires:] [`this->get` is not `NULL`.]]
|
||||
|
||||
[[Returns:] [`*(this->get())`]]
|
||||
|
||||
[[Throws:] [Nothing.]]
|
||||
|
||||
]
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:reset `void reset(T* new_value=0);`]
|
||||
|
||||
[variablelist
|
||||
|
||||
[[Effects:] [If `this->get()!=new_value` and `this->get()` is non-`NULL`, invoke `delete this->get()` or
|
||||
`cleanup_function(this->get())` as appropriate. Store `new_value` as the pointer associated with the current thread.]]
|
||||
|
||||
[[Postcondition:] [`this->get()==new_value`]]
|
||||
|
||||
[[Throws:] [`boost::thread_resource_error` if an error occurs.]]
|
||||
|
||||
]
|
||||
|
||||
[endsect]
|
||||
|
||||
[section:release `T* release();`]
|
||||
|
||||
[variablelist
|
||||
|
||||
[[Effects:] [Return `this->get()` and store `NULL` as the pointer associated with the current thread without invoking the cleanup
|
||||
function.]]
|
||||
|
||||
[[Postcondition:] [`this->get()==0`]]
|
||||
|
||||
[[Throws:] [Nothing.]]
|
||||
|
||||
]
|
||||
|
||||
[endsect]
|
||||
|
||||
|
||||
[endsect]
|
||||
|
||||
[endsect]
|
||||
147
doc/xtime.html
147
doc/xtime.html
@@ -1,147 +0,0 @@
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content=
|
||||
"text/html; charset=iso-8859-1">
|
||||
<meta name="keywords" content=
|
||||
"threads, Boost.Threads, thread library, C++">
|
||||
<link rel="stylesheet" type="text/css" href="styles.css">
|
||||
|
||||
<title>Boost.Threads, xtime</title>
|
||||
</head>
|
||||
|
||||
<body bgcolor="#FFFFFF" link="#0000FF" vlink="#800080">
|
||||
<table summary="header" border="0" cellpadding="7" cellspacing="0"
|
||||
width="100%">
|
||||
<tr>
|
||||
<td valign="top" width="300">
|
||||
<h3><img src="../../../c++boost.gif" alt="C++ Boost" width=
|
||||
"277" height="86"></h3>
|
||||
</td>
|
||||
|
||||
<td valign="top">
|
||||
<h1 align="center">Boost.Threads</h1>
|
||||
|
||||
<h2 align="center">xtime</h2>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<hr>
|
||||
|
||||
<p><a href="#Introduction">Introduction</a><br>
|
||||
<a href="#Header">Header</a><br>
|
||||
<a href="#Synopsis">Synopsis</a><br>
|
||||
<a href="#Reference">Reference</a><br>
|
||||
<a href="#Example">Example</a></p>
|
||||
|
||||
<h2><a name="Introduction">Introduction</a></h2>
|
||||
|
||||
<p>The <code>xtime</code> type is used to represent a point on some
|
||||
time scale or a duration in time. This type may be proposed for the C
|
||||
standard by Markus Kuhn. <b>Boost.Threads</b> provides only a very
|
||||
minimal implementation of this proposal and it's expected that a
|
||||
full implementation will be provided in Boost as a separate library, at
|
||||
which time <b>Boost.Threads</b> will deprecate its implementation.</p>
|
||||
|
||||
<h2><a name="Header">Header</a></h2>
|
||||
<pre>
|
||||
#include <a href=
|
||||
"../../../boost/thread/xtime.hpp"><boost/thread/xtime.hpp></a>
|
||||
</pre>
|
||||
|
||||
<h2><a name="Synopsis">Synopsis</a></h2>
|
||||
<pre>
|
||||
namespace boost {
|
||||
|
||||
enum
|
||||
{
|
||||
TIME_UTC=1,
|
||||
};
|
||||
|
||||
struct xtime
|
||||
{
|
||||
#if defined(BOOST_NO_INT64_T)
|
||||
int_fast32_t sec;
|
||||
#else
|
||||
int_fast64_t sec;
|
||||
#endif
|
||||
int_fast32_t nsec;
|
||||
};
|
||||
|
||||
int xtime_get(struct xtime* xtp, int clock_type);
|
||||
|
||||
} // namespace boost
|
||||
</pre>
|
||||
|
||||
<h2><a name="Reference">Reference</a></h2>
|
||||
<hr>
|
||||
|
||||
<h3>TIME_UTC</h3>
|
||||
|
||||
<p>The clock type for Coordinated Universal Time (UTC). The epoch for
|
||||
this clock type is 1970-01-01 00:00:00. This is the only clock type
|
||||
supported by <b>Boost.Threads</b>.</p>
|
||||
<hr>
|
||||
|
||||
<h3>xtime</h3>
|
||||
<pre>
|
||||
struct xtime
|
||||
{
|
||||
#if defined(BOOST_NO_INT64_T)
|
||||
int_fast32_t sec;
|
||||
#else
|
||||
int_fast64_t sec;
|
||||
#endif
|
||||
int_fast32_t nsec;
|
||||
};
|
||||
</pre>
|
||||
|
||||
<p><b>sec</b> represents the whole seconds that have passed since the
|
||||
epoch.</p>
|
||||
|
||||
<p><b>nsec</b> represents the nanoseconds since <code>sec.</code></p>
|
||||
<hr>
|
||||
|
||||
<h3>xtime_get</h3>
|
||||
<pre>
|
||||
int xtime_get(struct xtime* xtp, int clock_type);
|
||||
</pre>
|
||||
|
||||
<p><b>Postcondition:</b> <code>xtp</code> represents the current point
|
||||
in time as a duration since the epoch specified by the <code>
|
||||
clock_type</code>.</p>
|
||||
|
||||
<p><b>Returns:</b> <code>clock_type</code> if successful, otherwise
|
||||
0.</p>
|
||||
|
||||
<p><b>Notes:</b> The resolution is implementation specific. For many
|
||||
implementations the best resolution of time is far more than one
|
||||
nanosecond, and even when the resolution is reasonably good, the
|
||||
latency of a call to <code>xtime_get()</code> may be significant. For
|
||||
maximum portability, avoid durations of less than one second.</p>
|
||||
<hr>
|
||||
|
||||
<h2><a name="Example">Example Usage</a></h2>
|
||||
<pre>
|
||||
#include <a href=
|
||||
"../../../boost/thread/thread.hpp"><boost/thread/thread.hpp></a>
|
||||
#include <a href=
|
||||
"../../../boost/thread/tss.hpp"><boost/thread/xtime.hpp></a>
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
boost::xtime xt;
|
||||
boost::xtime_get(&xt, boost::TIME_UTC);
|
||||
xt.sec += 1;
|
||||
boost::thread::sleep(xt); // Sleep for 1 second
|
||||
}
|
||||
</pre>
|
||||
<hr>
|
||||
|
||||
<p>Revised
|
||||
<!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B, %Y" startspan -->05 November, 2001<!--webbot bot="Timestamp" endspan i-checksum="39359" --></p>
|
||||
|
||||
<p><i>© Copyright <a href="mailto:williamkempf@hotmail.com">
|
||||
William E. Kempf</a> 2001 all rights reserved.</i></p>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -1,66 +0,0 @@
|
||||
# (C) Copyright William E. Kempf 2001. Permission to copy, use, modify, sell and
|
||||
# distribute this software is granted provided this copyright notice appears
|
||||
# in all copies. This software is provided "as is" without express or implied
|
||||
# warranty, and with no claim as to its suitability for any purpose.
|
||||
#
|
||||
# Boost.Threads build and test Jamfile
|
||||
#
|
||||
# Declares the following targets:
|
||||
# 1. monitor, an example program.
|
||||
# 2. starvephil, an example program.
|
||||
# 3. tennis, an example program.
|
||||
|
||||
# declare the location of this subproject relative to the root
|
||||
subproject libs/thread/example ;
|
||||
|
||||
# Do some OS-specific setup
|
||||
if $(NT)
|
||||
{
|
||||
BOOST_THREADMON_LIB = <lib>../build/libboost_threadmon ;
|
||||
}
|
||||
else
|
||||
{
|
||||
BOOST_THREADMON_LIB = ;
|
||||
}
|
||||
|
||||
#######################
|
||||
|
||||
#
|
||||
# Declare the Boost.Threads monitor example program.
|
||||
#
|
||||
|
||||
exe monitor : monitor/monitor.cpp
|
||||
<lib>../build/libboost_thread
|
||||
$(BOOST_THREADMON_LIB)
|
||||
# requirements
|
||||
: <include>$(BOOST_ROOT)
|
||||
<threading>multi
|
||||
: debug release ;
|
||||
|
||||
#######################
|
||||
|
||||
#
|
||||
# Declare the Boost.Threads starvephil example program.
|
||||
#
|
||||
|
||||
exe starvephil : starvephil/starvephil.cpp
|
||||
<lib>../build/libboost_thread
|
||||
$(BOOST_THREADMON_LIB)
|
||||
# requirements
|
||||
: <include>$(BOOST_ROOT)
|
||||
<threading>multi
|
||||
: debug release ;
|
||||
|
||||
#######################
|
||||
|
||||
#
|
||||
# Declare the Boost.Threads tennis example program.
|
||||
#
|
||||
|
||||
exe tennis : tennis/tennis.cpp
|
||||
<lib>../build/libboost_thread
|
||||
$(BOOST_THREADMON_LIB)
|
||||
# requirements
|
||||
: <include>$(BOOST_ROOT)
|
||||
<threading>multi
|
||||
: debug release ;
|
||||
23
example/Jamfile.v2
Normal file
23
example/Jamfile.v2
Normal file
@@ -0,0 +1,23 @@
|
||||
# Copyright (C) 2001-2003
|
||||
# William E. Kempf
|
||||
#
|
||||
# Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
project boost/thread/example
|
||||
: requirements <library>../build//boost_thread <threading>multi
|
||||
;
|
||||
|
||||
|
||||
exe monitor : monitor.cpp ;
|
||||
exe starvephil : starvephil.cpp ;
|
||||
exe tennis : tennis.cpp ;
|
||||
exe condition : condition.cpp ;
|
||||
exe mutex : mutex.cpp ;
|
||||
exe once : once.cpp ;
|
||||
exe recursive_mutex : recursive_mutex.cpp ;
|
||||
exe thread : thread.cpp ;
|
||||
exe thread_group : thread_group.cpp ;
|
||||
exe tss : tss.cpp ;
|
||||
exe xtime : xtime.cpp ;
|
||||
|
||||
116
example/ba_externallly_locked.cpp
Normal file
116
example/ba_externallly_locked.cpp
Normal file
@@ -0,0 +1,116 @@
|
||||
// Copyright (C) 2012 Vicente Botet
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#define BOOST_THREAD_VERSION 4
|
||||
|
||||
#include <boost/thread/mutex.hpp>
|
||||
#include <boost/thread/lockable_adapter.hpp>
|
||||
#include <boost/thread/externally_locked.hpp>
|
||||
#include <boost/thread/strict_lock.hpp>
|
||||
#include <boost/thread/lock_types.hpp>
|
||||
#include <iostream>
|
||||
|
||||
using namespace boost;
|
||||
|
||||
class BankAccount
|
||||
{
|
||||
int balance_;
|
||||
public:
|
||||
void Deposit(int amount)
|
||||
{
|
||||
balance_ += amount;
|
||||
}
|
||||
void Withdraw(int amount)
|
||||
{
|
||||
balance_ -= amount;
|
||||
}
|
||||
int GetBalance()
|
||||
{
|
||||
return balance_;
|
||||
}
|
||||
};
|
||||
|
||||
//[AccountManager
|
||||
class AccountManager: public basic_lockable_adapter<mutex>
|
||||
{
|
||||
public:
|
||||
typedef basic_lockable_adapter<mutex> lockable_base_type;
|
||||
AccountManager() :
|
||||
lockable_base_type(), checkingAcct_(*this), savingsAcct_(*this)
|
||||
{
|
||||
}
|
||||
inline void Checking2Savings(int amount);
|
||||
inline void AMoreComplicatedChecking2Savings(int amount);
|
||||
private:
|
||||
/*<-*/
|
||||
bool some_condition()
|
||||
{
|
||||
return true;
|
||||
} /*->*/
|
||||
externally_locked<BankAccount, AccountManager > checkingAcct_;
|
||||
externally_locked<BankAccount, AccountManager > savingsAcct_;
|
||||
};
|
||||
//]
|
||||
|
||||
//[Checking2Savings
|
||||
void AccountManager::Checking2Savings(int amount)
|
||||
{
|
||||
strict_lock<AccountManager> guard(*this);
|
||||
checkingAcct_.get(guard).Withdraw(amount);
|
||||
savingsAcct_.get(guard).Deposit(amount);
|
||||
}
|
||||
//]
|
||||
|
||||
//#if DO_NOT_COMPILE
|
||||
////[AMoreComplicatedChecking2Savings_DO_NOT_COMPILE
|
||||
//void AccountManager::AMoreComplicatedChecking2Savings(int amount) {
|
||||
// unique_lock<AccountManager> guard(*this);
|
||||
// if (some_condition()) {
|
||||
// guard.lock();
|
||||
// }
|
||||
// checkingAcct_.get(guard).Withdraw(amount);
|
||||
// savingsAcct_.get(guard).Deposit(amount);
|
||||
// guard1.unlock();
|
||||
//}
|
||||
////]
|
||||
//#elif DO_NOT_COMPILE_2
|
||||
////[AMoreComplicatedChecking2Savings_DO_NOT_COMPILE2
|
||||
//void AccountManager::AMoreComplicatedChecking2Savings(int amount) {
|
||||
// unique_lock<AccountManager> guard1(*this);
|
||||
// if (some_condition()) {
|
||||
// guard1.lock();
|
||||
// }
|
||||
// {
|
||||
// strict_lock<AccountManager> guard(guard1);
|
||||
// checkingAcct_.get(guard).Withdraw(amount);
|
||||
// savingsAcct_.get(guard).Deposit(amount);
|
||||
// }
|
||||
// guard1.unlock();
|
||||
//}
|
||||
////]
|
||||
//#else
|
||||
////[AMoreComplicatedChecking2Savings
|
||||
void AccountManager::AMoreComplicatedChecking2Savings(int amount) {
|
||||
unique_lock<AccountManager> guard1(*this);
|
||||
if (some_condition()) {
|
||||
guard1.lock();
|
||||
}
|
||||
{
|
||||
nested_strict_lock<unique_lock<AccountManager> > guard(guard1);
|
||||
checkingAcct_.get(guard).Withdraw(amount);
|
||||
savingsAcct_.get(guard).Deposit(amount);
|
||||
}
|
||||
guard1.unlock();
|
||||
}
|
||||
////]
|
||||
//#endif
|
||||
|
||||
int main()
|
||||
{
|
||||
AccountManager mgr;
|
||||
mgr.Checking2Savings(100);
|
||||
return 0;
|
||||
}
|
||||
|
||||
91
example/condition.cpp
Normal file
91
example/condition.cpp
Normal file
@@ -0,0 +1,91 @@
|
||||
// Copyright (C) 2001-2003
|
||||
// William E. Kempf
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <boost/utility.hpp>
|
||||
#include <boost/thread/condition_variable.hpp>
|
||||
#include <boost/thread/thread.hpp>
|
||||
#include "../test/remove_error_code_unused_warning.hpp"
|
||||
|
||||
class bounded_buffer : private boost::noncopyable
|
||||
{
|
||||
public:
|
||||
typedef boost::unique_lock<boost::mutex> lock;
|
||||
|
||||
bounded_buffer(int n) : begin(0), end(0), buffered(0), circular_buf(n) { }
|
||||
|
||||
void send (int m) {
|
||||
lock lk(monitor);
|
||||
while (buffered == circular_buf.size())
|
||||
buffer_not_full.wait(lk);
|
||||
circular_buf[end] = m;
|
||||
end = (end+1) % circular_buf.size();
|
||||
++buffered;
|
||||
buffer_not_empty.notify_one();
|
||||
}
|
||||
int receive() {
|
||||
lock lk(monitor);
|
||||
while (buffered == 0)
|
||||
buffer_not_empty.wait(lk);
|
||||
int i = circular_buf[begin];
|
||||
begin = (begin+1) % circular_buf.size();
|
||||
--buffered;
|
||||
buffer_not_full.notify_one();
|
||||
return i;
|
||||
}
|
||||
|
||||
private:
|
||||
int begin, end;
|
||||
std::vector<int>::size_type buffered;
|
||||
std::vector<int> circular_buf;
|
||||
boost::condition_variable_any buffer_not_full, buffer_not_empty;
|
||||
boost::mutex monitor;
|
||||
};
|
||||
|
||||
bounded_buffer buf(2);
|
||||
|
||||
boost::mutex io_mutex;
|
||||
|
||||
void sender() {
|
||||
int n = 0;
|
||||
while (n < 1000000) {
|
||||
buf.send(n);
|
||||
if(!(n%10000))
|
||||
{
|
||||
boost::unique_lock<boost::mutex> io_lock(io_mutex);
|
||||
std::cout << "sent: " << n << std::endl;
|
||||
}
|
||||
++n;
|
||||
}
|
||||
buf.send(-1);
|
||||
}
|
||||
|
||||
void receiver() {
|
||||
int n;
|
||||
do {
|
||||
n = buf.receive();
|
||||
if(!(n%10000))
|
||||
{
|
||||
boost::unique_lock<boost::mutex> io_lock(io_mutex);
|
||||
std::cout << "received: " << n << std::endl;
|
||||
}
|
||||
} while (n != -1); // -1 indicates end of buffer
|
||||
buf.send(-1);
|
||||
}
|
||||
|
||||
int main(int, char*[])
|
||||
{
|
||||
boost::thread thrd1(&sender);
|
||||
boost::thread thrd2(&receiver);
|
||||
boost::thread thrd3(&receiver);
|
||||
boost::thread thrd4(&receiver);
|
||||
thrd1.join();
|
||||
thrd2.join();
|
||||
thrd3.join();
|
||||
thrd4.join();
|
||||
return 0;
|
||||
}
|
||||
73
example/future_then.cpp
Normal file
73
example/future_then.cpp
Normal file
@@ -0,0 +1,73 @@
|
||||
// Copyright (C) 2012 Vicente Botet
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
//#define BOOST_THREAD_VERSION 4
|
||||
//#define BOOST_THREAD_USES_LOG
|
||||
#define BOOST_THREAD_USES_LOG_THREAD_ID
|
||||
//#define BOOST_THREAD_DONT_PROVIDE_FUTURE_INVALID_AFTER_GET
|
||||
|
||||
#include <boost/thread/detail/log.hpp>
|
||||
#include <boost/thread/future.hpp>
|
||||
#include <boost/assert.hpp>
|
||||
#include <string>
|
||||
#if defined BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION
|
||||
|
||||
int p1()
|
||||
{
|
||||
BOOST_THREAD_LOG << "P1" << BOOST_THREAD_END_LOG;
|
||||
return 123;
|
||||
}
|
||||
|
||||
int p2(boost::future<int>& f)
|
||||
{
|
||||
BOOST_THREAD_LOG << "<P2" << BOOST_THREAD_END_LOG;
|
||||
try
|
||||
{
|
||||
return 2 * f.get();
|
||||
}
|
||||
catch (std::exception& ex)
|
||||
{
|
||||
BOOST_THREAD_LOG << "ERRORRRRR "<<ex.what() << "" << BOOST_THREAD_END_LOG;
|
||||
BOOST_ASSERT(false);
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
BOOST_THREAD_LOG << " ERRORRRRR exception thrown" << BOOST_THREAD_END_LOG;
|
||||
BOOST_ASSERT(false);
|
||||
}
|
||||
BOOST_THREAD_LOG << "P2>" << BOOST_THREAD_END_LOG;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
BOOST_THREAD_LOG << "<MAIN" << BOOST_THREAD_END_LOG;
|
||||
try
|
||||
{
|
||||
boost::future<int> f1 = boost::async(&p1);
|
||||
BOOST_THREAD_LOG << BOOST_THREAD_END_LOG;
|
||||
boost::future<int> f2 = f1.then(&p2);
|
||||
BOOST_THREAD_LOG << BOOST_THREAD_END_LOG;
|
||||
BOOST_THREAD_LOG << f2.get() << BOOST_THREAD_END_LOG;
|
||||
}
|
||||
catch (std::exception& ex)
|
||||
{
|
||||
BOOST_THREAD_LOG << "ERRORRRRR "<<ex.what() << "" << BOOST_THREAD_END_LOG;
|
||||
return 1;
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
BOOST_THREAD_LOG << " ERRORRRRR exception thrown" << BOOST_THREAD_END_LOG;
|
||||
return 2;
|
||||
}
|
||||
BOOST_THREAD_LOG << "MAIN>" << BOOST_THREAD_END_LOG;
|
||||
return 0;
|
||||
}
|
||||
#else
|
||||
|
||||
int main()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
42
example/make_future.cpp
Normal file
42
example/make_future.cpp
Normal file
@@ -0,0 +1,42 @@
|
||||
// Copyright (C) 2012 Vicente Botet
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#define BOOST_THREAD_VERSION 4
|
||||
|
||||
#include <boost/thread/future.hpp>
|
||||
#include <iostream>
|
||||
|
||||
int p1() { return 5; }
|
||||
|
||||
boost::future<int> compute(int x)
|
||||
{
|
||||
if (x == 0) return boost::make_future(0);
|
||||
if (x < 0) return boost::make_future(-1);
|
||||
//boost::future<int> f1 = boost::async([]() { return x+1; });
|
||||
boost::future<int> f1 = boost::async(boost::launch::async, p1);
|
||||
return boost::move(f1);
|
||||
}
|
||||
boost::shared_future<int> shared_compute(int x)
|
||||
{
|
||||
if (x == 0) return boost::make_shared_future(0);
|
||||
if (x < 0) return boost::make_shared_future(-1);
|
||||
//boost::future<int> f1 = boost::async([]() { return x+1; });
|
||||
boost::shared_future<int> f1 = boost::async(p1).share();
|
||||
return boost::move(f1);
|
||||
}
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
boost::future<int> f = compute(2);
|
||||
std::cout << f.get() << std::endl;
|
||||
}
|
||||
{
|
||||
boost::shared_future<int> f = shared_compute(2);
|
||||
std::cout << f.get() << std::endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -1,26 +1,32 @@
|
||||
// Copyright (C) 2001-2003
|
||||
// William E. Kempf
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
#include <boost/thread/condition.hpp>
|
||||
#include <boost/thread/condition_variable.hpp>
|
||||
#include <boost/thread/mutex.hpp>
|
||||
#include <boost/thread/recursive_mutex.hpp>
|
||||
#include <boost/thread/thread.hpp>
|
||||
|
||||
namespace {
|
||||
const int ITERS = 100;
|
||||
boost::mutex io_mutex;
|
||||
};
|
||||
const int ITERS = 100;
|
||||
boost::mutex io_mutex;
|
||||
} // namespace
|
||||
|
||||
template <typename M>
|
||||
class buffer_t
|
||||
{
|
||||
public:
|
||||
typedef typename M::scoped_lock scoped_lock;
|
||||
|
||||
typedef boost::unique_lock<M> scoped_lock;
|
||||
|
||||
buffer_t(int n)
|
||||
: p(0), c(0), full(0), buf(n)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
void send(int m)
|
||||
{
|
||||
scoped_lock lk(mutex);
|
||||
@@ -29,7 +35,7 @@ public:
|
||||
buf[p] = m;
|
||||
p = (p+1) % buf.size();
|
||||
++full;
|
||||
cond.notify_all();
|
||||
cond.notify_one();
|
||||
}
|
||||
int receive()
|
||||
{
|
||||
@@ -39,44 +45,43 @@ public:
|
||||
int i = buf[c];
|
||||
c = (c+1) % buf.size();
|
||||
--full;
|
||||
cond.notify_all();
|
||||
cond.notify_one();
|
||||
return i;
|
||||
}
|
||||
|
||||
|
||||
static buffer_t& get_buffer()
|
||||
{
|
||||
static buffer_t buf(2);
|
||||
return buf;
|
||||
}
|
||||
|
||||
|
||||
static void do_sender_thread()
|
||||
{
|
||||
for (int n = 0; n < ITERS; ++n)
|
||||
{
|
||||
get_buffer().send(n);
|
||||
{
|
||||
boost::mutex::scoped_lock lock(io_mutex);
|
||||
std::cout << "sent: " << n << std::endl;
|
||||
boost::unique_lock<boost::mutex> lock(io_mutex);
|
||||
std::cout << "sending: " << n << std::endl;
|
||||
}
|
||||
get_buffer().send(n);
|
||||
}
|
||||
}
|
||||
|
||||
static void do_receiver_thread()
|
||||
{
|
||||
for (int x=0; x < (ITERS/2); ++x)
|
||||
{
|
||||
int n = get_buffer().receive();
|
||||
{
|
||||
boost::unique_lock<boost::mutex> lock(io_mutex);
|
||||
std::cout << "received: " << n << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void do_receiver_thread()
|
||||
{
|
||||
int n;
|
||||
do
|
||||
{
|
||||
n = get_buffer().receive();
|
||||
{
|
||||
boost::mutex::scoped_lock lock(io_mutex);
|
||||
std::cout << "received: " << n << std::endl;
|
||||
}
|
||||
} while (n < ITERS - 1);
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
M mutex;
|
||||
boost::condition cond;
|
||||
boost::condition_variable_any cond;
|
||||
unsigned int p, c, full;
|
||||
std::vector<int> buf;
|
||||
};
|
||||
@@ -84,12 +89,15 @@ private:
|
||||
template <typename M>
|
||||
void do_test(M* dummy=0)
|
||||
{
|
||||
(void)dummy;
|
||||
typedef buffer_t<M> buffer_type;
|
||||
buffer_type::get_buffer();
|
||||
boost::thread thrd1(&buffer_type::do_sender_thread);
|
||||
boost::thread thrd1(&buffer_type::do_receiver_thread);
|
||||
boost::thread thrd2(&buffer_type::do_receiver_thread);
|
||||
boost::thread thrd3(&buffer_type::do_sender_thread);
|
||||
thrd1.join();
|
||||
thrd2.join();
|
||||
thrd3.join();
|
||||
}
|
||||
|
||||
void test_buffer()
|
||||
47
example/mutex.cpp
Normal file
47
example/mutex.cpp
Normal file
@@ -0,0 +1,47 @@
|
||||
// Copyright (C) 2001-2003
|
||||
// William E. Kempf
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#include <boost/thread/mutex.hpp>
|
||||
#include <boost/thread/thread.hpp>
|
||||
#include <iostream>
|
||||
|
||||
boost::mutex io_mutex; // The iostreams are not guaranteed to be thread-safe!
|
||||
|
||||
class counter
|
||||
{
|
||||
public:
|
||||
counter() : count(0) { }
|
||||
|
||||
int increment() {
|
||||
boost::unique_lock<boost::mutex> scoped_lock(mutex);
|
||||
return ++count;
|
||||
}
|
||||
|
||||
private:
|
||||
boost::mutex mutex;
|
||||
int count;
|
||||
};
|
||||
|
||||
counter c;
|
||||
|
||||
void change_count()
|
||||
{
|
||||
int i = c.increment();
|
||||
boost::unique_lock<boost::mutex> scoped_lock(io_mutex);
|
||||
std::cout << "count == " << i << std::endl;
|
||||
}
|
||||
|
||||
int main(int, char*[])
|
||||
{
|
||||
const int num_threads = 4;
|
||||
boost::thread_group thrds;
|
||||
for (int i=0; i < num_threads; ++i)
|
||||
thrds.create_thread(&change_count);
|
||||
|
||||
thrds.join_all();
|
||||
|
||||
return 0;
|
||||
}
|
||||
63
example/not_interleaved.cpp
Normal file
63
example/not_interleaved.cpp
Normal file
@@ -0,0 +1,63 @@
|
||||
// (C) Copyright 2012 Howard Hinnant
|
||||
// (C) Copyright 2012 Vicente Botet
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
// adapted from the example given by Howard Hinnant in
|
||||
|
||||
#define BOOST_THREAD_VERSION 4
|
||||
|
||||
#include <iostream>
|
||||
#include <boost/thread/scoped_thread.hpp>
|
||||
#include <boost/thread/externally_locked_stream.hpp>
|
||||
|
||||
void use_cerr(boost::externally_locked_stream<std::ostream> &mcerr)
|
||||
{
|
||||
using namespace boost;
|
||||
auto tf = chrono::steady_clock::now() + chrono::seconds(10);
|
||||
while (chrono::steady_clock::now() < tf)
|
||||
{
|
||||
mcerr << "logging data to cerr\n";
|
||||
this_thread::sleep_for(chrono::milliseconds(500));
|
||||
}
|
||||
}
|
||||
|
||||
void use_cout(boost::externally_locked_stream<std::ostream> &mcout)
|
||||
{
|
||||
using namespace boost;
|
||||
auto tf = chrono::steady_clock::now() + chrono::seconds(5);
|
||||
while (chrono::steady_clock::now() < tf)
|
||||
{
|
||||
mcout << "logging data to cout\n";
|
||||
this_thread::sleep_for(chrono::milliseconds(250));
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
using namespace boost;
|
||||
|
||||
recursive_mutex terminal_mutex;
|
||||
|
||||
externally_locked_stream<std::ostream> mcerr(std::cerr, terminal_mutex);
|
||||
externally_locked_stream<std::ostream> mcout(std::cout, terminal_mutex);
|
||||
externally_locked_stream<std::istream> mcin(std::cin, terminal_mutex);
|
||||
|
||||
scoped_thread<> t1(thread(use_cerr, boost::ref(mcerr)));
|
||||
scoped_thread<> t2(thread(use_cout, boost::ref(mcout)));
|
||||
this_thread::sleep_for(chrono::seconds(2));
|
||||
std::string nm;
|
||||
{
|
||||
strict_lock<recursive_mutex> lk(terminal_mutex);
|
||||
auto& gcout = mcout.hold(lk);
|
||||
auto& gcin = mcin.hold(lk);
|
||||
gcout << "Enter name: ";
|
||||
//gcin >> nm;
|
||||
}
|
||||
t1.join();
|
||||
t2.join();
|
||||
mcout << nm << '\n';
|
||||
return 1;
|
||||
}
|
||||
|
||||
38
example/once.cpp
Normal file
38
example/once.cpp
Normal file
@@ -0,0 +1,38 @@
|
||||
// Copyright (C) 2001-2003
|
||||
// William E. Kempf
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#define BOOST_THREAD_PROVIDES_ONCE_CXX11
|
||||
|
||||
#include <boost/thread/thread.hpp>
|
||||
#include <boost/thread/once.hpp>
|
||||
#include <cassert>
|
||||
|
||||
int value=0;
|
||||
#ifdef BOOST_THREAD_PROVIDES_ONCE_CXX11
|
||||
boost::once_flag once;
|
||||
#else
|
||||
boost::once_flag once = BOOST_ONCE_INIT;
|
||||
boost::once_flag once2 = once;
|
||||
#endif
|
||||
|
||||
void init()
|
||||
{
|
||||
++value;
|
||||
}
|
||||
|
||||
void thread_proc()
|
||||
{
|
||||
boost::call_once(&init, once);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
boost::thread_group threads;
|
||||
for (int i=0; i<5; ++i)
|
||||
threads.create_thread(&thread_proc);
|
||||
threads.join_all();
|
||||
assert(value == 1);
|
||||
}
|
||||
237
example/perf_condition_variable.cpp
Normal file
237
example/perf_condition_variable.cpp
Normal file
@@ -0,0 +1,237 @@
|
||||
// (C) Copyright 2012 Vicente J. Botet Escriba
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See
|
||||
// accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
// This performance test is based on the performance test provided by maxim.yegorushkin
|
||||
// at https://svn.boost.org/trac/boost/ticket/7422
|
||||
|
||||
#define BOOST_THREAD_DONT_PROVIDE_INTERRUPTIONS
|
||||
|
||||
#include <boost/thread/condition_variable.hpp>
|
||||
#include <boost/thread/mutex.hpp>
|
||||
#include <boost/chrono/stopwatches/simple_stopwatch.hpp>
|
||||
|
||||
#include <condition_variable>
|
||||
#include <future>
|
||||
#include <limits>
|
||||
#include <cstdio>
|
||||
#include <thread>
|
||||
#include <mutex>
|
||||
#include <vector>
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
namespace
|
||||
{
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// class Stopwatch
|
||||
// {
|
||||
// public:
|
||||
// typedef long long rep;
|
||||
//
|
||||
// static rep now()
|
||||
// {
|
||||
// timespec ts;
|
||||
// if (clock_gettime(CLOCK_MONOTONIC, &ts)) abort();
|
||||
// return ts.tv_sec * rep(1000000000) + ts.tv_nsec;
|
||||
// }
|
||||
//
|
||||
// Stopwatch() :
|
||||
// start_(now())
|
||||
// {
|
||||
// }
|
||||
//
|
||||
// rep elapsed() const
|
||||
// {
|
||||
// return now() - start_;
|
||||
// }
|
||||
//
|
||||
// private:
|
||||
// rep start_;
|
||||
// };
|
||||
|
||||
typedef boost::chrono::simple_stopwatch<> Stopwatch;
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
struct BoostTypes
|
||||
{
|
||||
typedef boost::condition_variable condition_variable;
|
||||
typedef boost::mutex mutex;
|
||||
typedef boost::mutex::scoped_lock scoped_lock;
|
||||
};
|
||||
|
||||
struct StdTypes
|
||||
{
|
||||
typedef std::condition_variable condition_variable;
|
||||
typedef std::mutex mutex;
|
||||
typedef std::unique_lock<std::mutex> scoped_lock;
|
||||
};
|
||||
|
||||
template <class Types>
|
||||
struct SharedData: Types
|
||||
{
|
||||
unsigned const iterations;
|
||||
unsigned counter;
|
||||
unsigned semaphore;
|
||||
typename Types::condition_variable cnd;
|
||||
typename Types::mutex mtx;
|
||||
Stopwatch::rep producer_time;
|
||||
|
||||
SharedData(unsigned iterations, unsigned consumers) :
|
||||
iterations(iterations), counter(), semaphore(consumers) // Initialize to the number of consumers. (*)
|
||||
, producer_time()
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template <class S>
|
||||
void producer_thread(S* shared_data)
|
||||
{
|
||||
Stopwatch sw;
|
||||
|
||||
unsigned const consumers = shared_data->semaphore; // (*)
|
||||
for (unsigned i = shared_data->iterations; i--;)
|
||||
{
|
||||
{
|
||||
typename S::scoped_lock lock(shared_data->mtx);
|
||||
// Wait till all consumers signal.
|
||||
while (consumers != shared_data->semaphore)
|
||||
{
|
||||
shared_data->cnd.wait(lock);
|
||||
}
|
||||
shared_data->semaphore = 0;
|
||||
// Signal consumers.
|
||||
++shared_data->counter;
|
||||
}
|
||||
shared_data->cnd.notify_all();
|
||||
}
|
||||
|
||||
shared_data->producer_time = sw.elapsed().count();
|
||||
}
|
||||
|
||||
template <class S>
|
||||
void consumer_thread(S* shared_data)
|
||||
{
|
||||
unsigned counter = 0;
|
||||
while (counter != shared_data->iterations)
|
||||
{
|
||||
{
|
||||
typename S::scoped_lock lock(shared_data->mtx);
|
||||
// Wait till the producer signals.
|
||||
while (counter == shared_data->counter)
|
||||
{
|
||||
shared_data->cnd.wait(lock);
|
||||
}
|
||||
counter = shared_data->counter;
|
||||
// Signal the producer.
|
||||
++shared_data->semaphore;
|
||||
}
|
||||
shared_data->cnd.notify_all();
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template <class Types>
|
||||
Stopwatch::rep benchmark_ping_pong(unsigned consumer_count)
|
||||
{
|
||||
typedef SharedData<Types> S;
|
||||
|
||||
auto best_producer_time = std::numeric_limits<Stopwatch::rep>::max();
|
||||
|
||||
std::vector<std::thread> consumers
|
||||
{ consumer_count };
|
||||
|
||||
// Run the benchmark 10 times and report the best time.
|
||||
for (int times = 10; times--;)
|
||||
{
|
||||
S shared_data
|
||||
{ 100000, consumer_count };
|
||||
|
||||
// Start the consumers.
|
||||
for (unsigned i = 0; i < consumer_count; ++i)
|
||||
consumers[i] = std::thread
|
||||
{ consumer_thread<S> , &shared_data };
|
||||
// Start the producer and wait till it finishes.
|
||||
std::thread
|
||||
{ producer_thread<S> , &shared_data }.join();
|
||||
// Wait till consumers finish.
|
||||
for (unsigned i = 0; i < consumer_count; ++i)
|
||||
consumers[i].join();
|
||||
|
||||
best_producer_time = std::min(best_producer_time, shared_data.producer_time);
|
||||
|
||||
}
|
||||
|
||||
return best_producer_time;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
} // namespace
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// sudo chrt -f 99 /usr/bin/time -f "\n***\ntime: %E\ncontext switches: %c\nwaits: %w" /home/max/otsquant/build/Linux-x86_64-64.g++-release/test/test
|
||||
|
||||
/*
|
||||
|
||||
Producer-consumer ping-pong tests. It aims to benchmark condition variables with and without
|
||||
thread cancellation support by comparing the time it took to complete the benchmark.
|
||||
|
||||
Condition variable with thread cancellation support is boost::condition_variable from
|
||||
boost-1.51. Without - std::condition_variable that comes with gcc-4.7.2.
|
||||
|
||||
One producer, one to CONSUMER_MAX consumers. The benchmark calls
|
||||
condition_variable::notify_all() without holding a mutex to maximize contention within this
|
||||
function. Each benchmark for a number of consumers is run three times and the best time is
|
||||
picked to get rid of outliers.
|
||||
|
||||
The results are reported for each benchmark for a number of consumers. The most important number
|
||||
is (std - boost) / std * 100. Positive numbers are when boost::condition_variable is faster,
|
||||
negative it is slower.
|
||||
|
||||
*/
|
||||
|
||||
int main()
|
||||
{
|
||||
std::printf("MAIN\n");
|
||||
enum
|
||||
{
|
||||
CONSUMER_MAX = 2
|
||||
};
|
||||
|
||||
struct
|
||||
{
|
||||
Stopwatch::rep boost, std;
|
||||
} best_times[CONSUMER_MAX] = {};
|
||||
|
||||
for (unsigned i = 1; i <= CONSUMER_MAX; ++i)
|
||||
{
|
||||
auto& b = best_times[i - 1];
|
||||
std::printf("STD: %d\n", i);
|
||||
b.std = benchmark_ping_pong<StdTypes> (i);
|
||||
std::printf("BOOST: %d\n", i);
|
||||
b.boost = benchmark_ping_pong<BoostTypes> (i);
|
||||
|
||||
std::printf("consumers: %4d\n", i);
|
||||
std::printf("best std producer time: %15.9fsec\n", b.std * 1e-9);
|
||||
std::printf("best boost producer time: %15.9fsec\n", b.boost * 1e-9);
|
||||
std::printf("(std - boost) / std: %7.2f%%\n", (b.std - b.boost) * 100. / b.std);
|
||||
}
|
||||
|
||||
printf("\ncsv:\n\n");
|
||||
printf("consumers,(std-boost)/std,std,boost\n");
|
||||
for (unsigned i = 1; i <= CONSUMER_MAX; ++i)
|
||||
{
|
||||
auto& b = best_times[i - 1];
|
||||
printf("%d,%f,%lld,%lld\n", i, (b.std - b.boost) * 100. / b.std, b.std, b.boost);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
49
example/recursive_mutex.cpp
Normal file
49
example/recursive_mutex.cpp
Normal file
@@ -0,0 +1,49 @@
|
||||
// Copyright (C) 2001-2003
|
||||
// William E. Kempf
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#include <boost/thread/recursive_mutex.hpp>
|
||||
#include <boost/thread/thread.hpp>
|
||||
#include <iostream>
|
||||
|
||||
class counter
|
||||
{
|
||||
public:
|
||||
counter() : count(0) { }
|
||||
|
||||
int add(int val) {
|
||||
boost::unique_lock<boost::recursive_mutex> scoped_lock(mutex);
|
||||
count += val;
|
||||
return count;
|
||||
}
|
||||
int increment() {
|
||||
boost::unique_lock<boost::recursive_mutex> scoped_lock(mutex);
|
||||
return add(1);
|
||||
}
|
||||
|
||||
private:
|
||||
boost::recursive_mutex mutex;
|
||||
int count;
|
||||
};
|
||||
|
||||
counter c;
|
||||
|
||||
void change_count()
|
||||
{
|
||||
std::cout << "count == " << c.increment() << std::endl;
|
||||
}
|
||||
|
||||
int main(int, char*[])
|
||||
{
|
||||
const int num_threads=4;
|
||||
|
||||
boost::thread_group threads;
|
||||
for (int i=0; i < num_threads; ++i)
|
||||
threads.create_thread(&change_count);
|
||||
|
||||
threads.join_all();
|
||||
|
||||
return 0;
|
||||
}
|
||||
69
example/scoped_thread.cpp
Normal file
69
example/scoped_thread.cpp
Normal file
@@ -0,0 +1,69 @@
|
||||
// (C) Copyright 2009-2012 Anthony Williams
|
||||
// (C) Copyright 2012 Vicente Botet
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#define BOOST_THREAD_VERSION 3
|
||||
|
||||
#include <iostream>
|
||||
#include <boost/thread/scoped_thread.hpp>
|
||||
|
||||
void do_something(int& i)
|
||||
{
|
||||
++i;
|
||||
}
|
||||
|
||||
struct func
|
||||
{
|
||||
int& i;
|
||||
|
||||
func(int& i_) :
|
||||
i(i_)
|
||||
{
|
||||
}
|
||||
|
||||
void operator()()
|
||||
{
|
||||
for (unsigned j = 0; j < 1000000; ++j)
|
||||
{
|
||||
do_something(i);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
void do_something_in_current_thread()
|
||||
{
|
||||
}
|
||||
|
||||
//void do_something_with_current_thread(boost::thread&& th)
|
||||
//{
|
||||
// th.join();
|
||||
//}
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
int some_local_state;
|
||||
boost::strict_scoped_thread<> t( (boost::thread(func(some_local_state))));
|
||||
|
||||
do_something_in_current_thread();
|
||||
}
|
||||
{
|
||||
int some_local_state;
|
||||
boost::thread t(( func(some_local_state) ));
|
||||
boost::strict_scoped_thread<> g( (boost::move(t)) );
|
||||
|
||||
do_something_in_current_thread();
|
||||
}
|
||||
// {
|
||||
// int some_local_state;
|
||||
// boost::thread t(( func(some_local_state) ));
|
||||
// boost::strict_scoped_thread<> g( (boost::move(t)) );
|
||||
//
|
||||
// do_something_in_current_thread();
|
||||
// do_something_with_current_thread(boost::thread(g));
|
||||
// }
|
||||
return 0;
|
||||
}
|
||||
|
||||
144
example/shared_monitor.cpp
Normal file
144
example/shared_monitor.cpp
Normal file
@@ -0,0 +1,144 @@
|
||||
// Copyright (C) 2012 Vicente J. Botet Escriba
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#include <iostream>
|
||||
#include <boost/thread/mutex.hpp>
|
||||
#include <boost/thread/shared_mutex.hpp>
|
||||
#include <boost/thread/lock_algorithms.hpp>
|
||||
#include <boost/thread/thread.hpp>
|
||||
#if defined BOOST_THREAD_DONT_USE_CHRONO
|
||||
#include <boost/chrono/chrono_io.hpp>
|
||||
#endif
|
||||
#include <cassert>
|
||||
#include <vector>
|
||||
|
||||
#define EXCLUSIVE 1
|
||||
#define SHARED 2
|
||||
|
||||
#define MODE SHARED
|
||||
|
||||
class A
|
||||
{
|
||||
#if MODE == EXCLUSIVE
|
||||
typedef boost::mutex mutex_type;
|
||||
#elif MODE == SHARED
|
||||
typedef boost::shared_mutex mutex_type;
|
||||
#else
|
||||
#error MODE not set
|
||||
#endif
|
||||
typedef std::vector<double> C;
|
||||
mutable mutex_type mut_;
|
||||
C data_;
|
||||
public:
|
||||
A() : data_(10000000) {}
|
||||
A(const A& a);
|
||||
A& operator=(const A& a);
|
||||
|
||||
void compute(const A& x, const A& y);
|
||||
};
|
||||
|
||||
A::A(const A& a)
|
||||
{
|
||||
#if MODE == EXCLUSIVE
|
||||
boost::unique_lock<mutex_type> lk(a.mut_);
|
||||
#elif MODE == SHARED
|
||||
boost::shared_lock<mutex_type> lk(a.mut_);
|
||||
#else
|
||||
#error MODE not set
|
||||
#endif
|
||||
data_ = a.data_;
|
||||
}
|
||||
|
||||
A&
|
||||
A::operator=(const A& a)
|
||||
{
|
||||
if (this != &a)
|
||||
{
|
||||
boost::unique_lock<mutex_type> lk1(mut_, boost::defer_lock);
|
||||
#if MODE == EXCLUSIVE
|
||||
boost::unique_lock<mutex_type> lk2(a.mut_, boost::defer_lock);
|
||||
#elif MODE == SHARED
|
||||
boost::shared_lock<mutex_type> lk2(a.mut_, boost::defer_lock);
|
||||
#else
|
||||
#error MODE not set
|
||||
#endif
|
||||
boost::lock(lk1, lk2);
|
||||
data_ = a.data_;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
void
|
||||
A::compute(const A& x, const A& y)
|
||||
{
|
||||
boost::unique_lock<mutex_type> lk1(mut_, boost::defer_lock);
|
||||
#if MODE == EXCLUSIVE
|
||||
boost::unique_lock<mutex_type> lk2(x.mut_, boost::defer_lock);
|
||||
boost::unique_lock<mutex_type> lk3(y.mut_, boost::defer_lock);
|
||||
#elif MODE == SHARED
|
||||
boost::shared_lock<mutex_type> lk2(x.mut_, boost::defer_lock);
|
||||
boost::shared_lock<mutex_type> lk3(y.mut_, boost::defer_lock);
|
||||
#else
|
||||
#error MODE not set
|
||||
#endif
|
||||
boost::lock(lk1, lk2, lk3);
|
||||
assert(data_.size() == x.data_.size());
|
||||
assert(data_.size() == y.data_.size());
|
||||
for (unsigned i = 0; i < data_.size(); ++i)
|
||||
data_[i] = (x.data_[i] + y.data_[i]) / 2;
|
||||
}
|
||||
|
||||
A a1;
|
||||
A a2;
|
||||
|
||||
void test_s()
|
||||
{
|
||||
A la3 = a1;
|
||||
for (int i = 0; i < 150; ++i)
|
||||
{
|
||||
la3.compute(a1, a2);
|
||||
}
|
||||
}
|
||||
|
||||
void test_w()
|
||||
{
|
||||
A la3 = a1;
|
||||
for (int i = 0; i < 10; ++i)
|
||||
{
|
||||
la3.compute(a1, a2);
|
||||
a1 = la3;
|
||||
a2 = la3;
|
||||
#if defined BOOST_THREAD_DONT_USE_CHRONO
|
||||
boost::this_thread::sleep_for(boost::chrono::seconds(1));
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
#if defined BOOST_THREAD_DONT_USE_CHRONO
|
||||
typedef boost::chrono::high_resolution_clock Clock;
|
||||
typedef boost::chrono::duration<double> sec;
|
||||
Clock::time_point t0 = Clock::now();
|
||||
#endif
|
||||
std::vector<boost::thread*> v;
|
||||
boost::thread thw(test_w);
|
||||
v.push_back(&thw);
|
||||
boost::thread thr0(test_w);
|
||||
v.push_back(&thr0);
|
||||
boost::thread thr1(test_w);
|
||||
v.push_back(&thr1);
|
||||
boost::thread thr2(test_w);
|
||||
v.push_back(&thr2);
|
||||
boost::thread thr3(test_w);
|
||||
v.push_back(&thr3);
|
||||
for (std::size_t i = 0; i < v.size(); ++i)
|
||||
v[i]->join();
|
||||
#if defined BOOST_THREAD_DONT_USE_CHRONO
|
||||
Clock::time_point t1 = Clock::now();
|
||||
std::cout << sec(t1-t0) << '\n';
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
746
example/shared_mutex.cpp
Normal file
746
example/shared_mutex.cpp
Normal file
@@ -0,0 +1,746 @@
|
||||
// Copyright (C) 2012 Vicente J. Botet Escriba
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#define BOOST_THREAD_PROVIDES_SHARED_MUTEX_UPWARDS_CONVERSIONS
|
||||
#define BOOST_THREAD_PROVIDES_EXPLICIT_LOCK_CONVERSION
|
||||
#define BOOST_THREAD_PROVIDES_GENERIC_SHARED_MUTEX_ON_WIN
|
||||
|
||||
#include <iostream>
|
||||
#include <boost/thread/mutex.hpp>
|
||||
#include <boost/thread/shared_mutex.hpp>
|
||||
#include <boost/thread/lock_algorithms.hpp>
|
||||
#include <boost/thread/thread.hpp>
|
||||
#include <vector>
|
||||
|
||||
#if defined BOOST_THREAD_USES_CHRONO
|
||||
#include <boost/chrono/chrono_io.hpp>
|
||||
|
||||
|
||||
enum {reading, writing};
|
||||
int state = reading;
|
||||
|
||||
#if 1
|
||||
|
||||
boost::mutex&
|
||||
cout_mut()
|
||||
{
|
||||
static boost::mutex m;
|
||||
return m;
|
||||
}
|
||||
|
||||
void
|
||||
print(const char* tag, unsigned count, char ch)
|
||||
{
|
||||
boost::lock_guard<boost::mutex> _(cout_mut());
|
||||
std::cout << tag << count << ch;
|
||||
}
|
||||
|
||||
#elif 0
|
||||
|
||||
boost::recursive_mutex&
|
||||
cout_mut()
|
||||
{
|
||||
static boost::recursive_mutex m;
|
||||
return m;
|
||||
}
|
||||
|
||||
void print() {}
|
||||
|
||||
template <class A0, class ...Args>
|
||||
void
|
||||
print(const A0& a0, const Args& ...args)
|
||||
{
|
||||
boost::lock_guard<boost::recursive_mutex> _(cout_mut());
|
||||
std::cout << a0;
|
||||
print(args...);
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
template <class A0, class A1, class A2>
|
||||
void
|
||||
print(const A0&, const A1& a1, const A2&)
|
||||
{
|
||||
assert(a1 > 10000);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
namespace S
|
||||
{
|
||||
|
||||
boost::shared_mutex mut;
|
||||
|
||||
void reader()
|
||||
{
|
||||
typedef boost::chrono::steady_clock Clock;
|
||||
unsigned count = 0;
|
||||
Clock::time_point until = Clock::now() + boost::chrono::seconds(3);
|
||||
while (Clock::now() < until)
|
||||
{
|
||||
mut.lock_shared();
|
||||
assert(state == reading);
|
||||
++count;
|
||||
mut.unlock_shared();
|
||||
}
|
||||
print("reader = ", count, '\n');
|
||||
}
|
||||
|
||||
void writer()
|
||||
{
|
||||
typedef boost::chrono::steady_clock Clock;
|
||||
unsigned count = 0;
|
||||
Clock::time_point until = Clock::now() + boost::chrono::seconds(3);
|
||||
while (Clock::now() < until)
|
||||
{
|
||||
mut.lock();
|
||||
state = writing;
|
||||
assert(state == writing);
|
||||
state = reading;
|
||||
++count;
|
||||
mut.unlock();
|
||||
}
|
||||
print("writer = ", count, '\n');
|
||||
}
|
||||
|
||||
void try_reader()
|
||||
{
|
||||
typedef boost::chrono::steady_clock Clock;
|
||||
unsigned count = 0;
|
||||
Clock::time_point until = Clock::now() + boost::chrono::seconds(3);
|
||||
while (Clock::now() < until)
|
||||
{
|
||||
if (mut.try_lock_shared())
|
||||
{
|
||||
assert(state == reading);
|
||||
++count;
|
||||
mut.unlock_shared();
|
||||
}
|
||||
}
|
||||
print("try_reader = ", count, '\n');
|
||||
}
|
||||
|
||||
void try_writer()
|
||||
{
|
||||
typedef boost::chrono::steady_clock Clock;
|
||||
unsigned count = 0;
|
||||
Clock::time_point until = Clock::now() + boost::chrono::seconds(3);
|
||||
while (Clock::now() < until)
|
||||
{
|
||||
if (mut.try_lock())
|
||||
{
|
||||
state = writing;
|
||||
assert(state == writing);
|
||||
state = reading;
|
||||
++count;
|
||||
mut.unlock();
|
||||
}
|
||||
}
|
||||
print("try_writer = ", count, '\n');
|
||||
}
|
||||
|
||||
void try_for_reader()
|
||||
{
|
||||
typedef boost::chrono::steady_clock Clock;
|
||||
unsigned count = 0;
|
||||
Clock::time_point until = Clock::now() + boost::chrono::seconds(3);
|
||||
while (Clock::now() < until)
|
||||
{
|
||||
if (mut.try_lock_shared_for(boost::chrono::microseconds(5)))
|
||||
{
|
||||
assert(state == reading);
|
||||
++count;
|
||||
mut.unlock_shared();
|
||||
}
|
||||
}
|
||||
print("try_for_reader = ", count, '\n');
|
||||
}
|
||||
|
||||
void try_for_writer()
|
||||
{
|
||||
typedef boost::chrono::steady_clock Clock;
|
||||
unsigned count = 0;
|
||||
Clock::time_point until = Clock::now() + boost::chrono::seconds(3);
|
||||
while (Clock::now() < until)
|
||||
{
|
||||
if (mut.try_lock_for(boost::chrono::microseconds(5)))
|
||||
{
|
||||
state = writing;
|
||||
assert(state == writing);
|
||||
state = reading;
|
||||
++count;
|
||||
mut.unlock();
|
||||
}
|
||||
}
|
||||
print("try_for_writer = ", count, '\n');
|
||||
}
|
||||
|
||||
void
|
||||
test_shared_mutex()
|
||||
{
|
||||
std::cout << __FILE__ << "[" <<__LINE__ << "]" << std::endl;
|
||||
{
|
||||
boost::thread t1(reader);
|
||||
boost::thread t2(writer);
|
||||
boost::thread t3(reader);
|
||||
t1.join();
|
||||
t2.join();
|
||||
t3.join();
|
||||
}
|
||||
std::cout << __FILE__ << "[" <<__LINE__ << "]" << std::endl;
|
||||
{
|
||||
boost::thread t1(try_reader);
|
||||
boost::thread t2(try_writer);
|
||||
boost::thread t3(try_reader);
|
||||
t1.join();
|
||||
t2.join();
|
||||
t3.join();
|
||||
}
|
||||
std::cout << __FILE__ << "[" <<__LINE__ << "]" << std::endl;
|
||||
{
|
||||
boost::thread t1(try_for_reader);
|
||||
boost::thread t2(try_for_writer);
|
||||
boost::thread t3(try_for_reader);
|
||||
t1.join();
|
||||
t2.join();
|
||||
t3.join();
|
||||
}
|
||||
std::cout << __FILE__ << "[" <<__LINE__ << "]" << std::endl;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
namespace U
|
||||
{
|
||||
|
||||
boost::upgrade_mutex mut;
|
||||
|
||||
void reader()
|
||||
{
|
||||
typedef boost::chrono::steady_clock Clock;
|
||||
unsigned count = 0;
|
||||
Clock::time_point until = Clock::now() + boost::chrono::seconds(3);
|
||||
while (Clock::now() < until)
|
||||
{
|
||||
mut.lock_shared();
|
||||
assert(state == reading);
|
||||
++count;
|
||||
mut.unlock_shared();
|
||||
}
|
||||
print("reader = ", count, '\n');
|
||||
}
|
||||
|
||||
void writer()
|
||||
{
|
||||
typedef boost::chrono::steady_clock Clock;
|
||||
unsigned count = 0;
|
||||
Clock::time_point until = Clock::now() + boost::chrono::seconds(3);
|
||||
while (Clock::now() < until)
|
||||
{
|
||||
mut.lock();
|
||||
state = writing;
|
||||
assert(state == writing);
|
||||
state = reading;
|
||||
++count;
|
||||
mut.unlock();
|
||||
}
|
||||
print("writer = ", count, '\n');
|
||||
}
|
||||
|
||||
void try_reader()
|
||||
{
|
||||
typedef boost::chrono::steady_clock Clock;
|
||||
unsigned count = 0;
|
||||
Clock::time_point until = Clock::now() + boost::chrono::seconds(3);
|
||||
while (Clock::now() < until)
|
||||
{
|
||||
if (mut.try_lock_shared())
|
||||
{
|
||||
assert(state == reading);
|
||||
++count;
|
||||
mut.unlock_shared();
|
||||
}
|
||||
}
|
||||
print("try_reader = ", count, '\n');
|
||||
}
|
||||
|
||||
void try_writer()
|
||||
{
|
||||
typedef boost::chrono::steady_clock Clock;
|
||||
unsigned count = 0;
|
||||
Clock::time_point until = Clock::now() + boost::chrono::seconds(3);
|
||||
while (Clock::now() < until)
|
||||
{
|
||||
if (mut.try_lock())
|
||||
{
|
||||
state = writing;
|
||||
assert(state == writing);
|
||||
state = reading;
|
||||
++count;
|
||||
mut.unlock();
|
||||
}
|
||||
}
|
||||
print("try_writer = ", count, '\n');
|
||||
}
|
||||
|
||||
void try_for_reader()
|
||||
{
|
||||
typedef boost::chrono::steady_clock Clock;
|
||||
unsigned count = 0;
|
||||
Clock::time_point until = Clock::now() + boost::chrono::seconds(3);
|
||||
while (Clock::now() < until)
|
||||
{
|
||||
if (mut.try_lock_shared_for(boost::chrono::microseconds(5)))
|
||||
{
|
||||
assert(state == reading);
|
||||
++count;
|
||||
mut.unlock_shared();
|
||||
}
|
||||
}
|
||||
print("try_for_reader = ", count, '\n');
|
||||
}
|
||||
|
||||
void try_for_writer()
|
||||
{
|
||||
typedef boost::chrono::steady_clock Clock;
|
||||
unsigned count = 0;
|
||||
Clock::time_point until = Clock::now() + boost::chrono::seconds(3);
|
||||
while (Clock::now() < until)
|
||||
{
|
||||
if (mut.try_lock_for(boost::chrono::microseconds(5)))
|
||||
{
|
||||
state = writing;
|
||||
assert(state == writing);
|
||||
state = reading;
|
||||
++count;
|
||||
mut.unlock();
|
||||
}
|
||||
}
|
||||
print("try_for_writer = ", count, '\n');
|
||||
}
|
||||
|
||||
void upgradable()
|
||||
{
|
||||
typedef boost::chrono::steady_clock Clock;
|
||||
unsigned count = 0;
|
||||
Clock::time_point until = Clock::now() + boost::chrono::seconds(3);
|
||||
while (Clock::now() < until)
|
||||
{
|
||||
mut.lock_upgrade();
|
||||
assert(state == reading);
|
||||
++count;
|
||||
mut.unlock_upgrade();
|
||||
}
|
||||
print("upgradable = ", count, '\n');
|
||||
}
|
||||
|
||||
void try_upgradable()
|
||||
{
|
||||
typedef boost::chrono::steady_clock Clock;
|
||||
unsigned count = 0;
|
||||
Clock::time_point until = Clock::now() + boost::chrono::seconds(3);
|
||||
while (Clock::now() < until)
|
||||
{
|
||||
if (mut.try_lock_upgrade())
|
||||
{
|
||||
assert(state == reading);
|
||||
++count;
|
||||
mut.unlock_upgrade();
|
||||
}
|
||||
}
|
||||
print("try_upgradable = ", count, '\n');
|
||||
}
|
||||
|
||||
void try_for_upgradable()
|
||||
{
|
||||
typedef boost::chrono::steady_clock Clock;
|
||||
unsigned count = 0;
|
||||
Clock::time_point until = Clock::now() + boost::chrono::seconds(3);
|
||||
while (Clock::now() < until)
|
||||
{
|
||||
if (mut.try_lock_upgrade_for(boost::chrono::microseconds(5)))
|
||||
{
|
||||
assert(state == reading);
|
||||
++count;
|
||||
mut.unlock_upgrade();
|
||||
}
|
||||
}
|
||||
print("try_for_upgradable = ", count, '\n');
|
||||
}
|
||||
|
||||
void clockwise()
|
||||
{
|
||||
typedef boost::chrono::steady_clock Clock;
|
||||
unsigned count = 0;
|
||||
Clock::time_point until = Clock::now() + boost::chrono::seconds(3);
|
||||
while (Clock::now() < until)
|
||||
{
|
||||
mut.lock_shared();
|
||||
assert(state == reading);
|
||||
if (mut.try_unlock_shared_and_lock())
|
||||
{
|
||||
state = writing;
|
||||
}
|
||||
else if (mut.try_unlock_shared_and_lock_upgrade())
|
||||
{
|
||||
assert(state == reading);
|
||||
mut.unlock_upgrade_and_lock();
|
||||
state = writing;
|
||||
}
|
||||
else
|
||||
{
|
||||
mut.unlock_shared();
|
||||
continue;
|
||||
}
|
||||
assert(state == writing);
|
||||
state = reading;
|
||||
mut.unlock_and_lock_upgrade();
|
||||
assert(state == reading);
|
||||
mut.unlock_upgrade_and_lock_shared();
|
||||
assert(state == reading);
|
||||
mut.unlock_shared();
|
||||
++count;
|
||||
}
|
||||
print("clockwise = ", count, '\n');
|
||||
}
|
||||
|
||||
void counter_clockwise()
|
||||
{
|
||||
typedef boost::chrono::steady_clock Clock;
|
||||
unsigned count = 0;
|
||||
Clock::time_point until = Clock::now() + boost::chrono::seconds(3);
|
||||
while (Clock::now() < until)
|
||||
{
|
||||
mut.lock_upgrade();
|
||||
assert(state == reading);
|
||||
mut.unlock_upgrade_and_lock();
|
||||
assert(state == reading);
|
||||
state = writing;
|
||||
assert(state == writing);
|
||||
state = reading;
|
||||
mut.unlock_and_lock_shared();
|
||||
assert(state == reading);
|
||||
mut.unlock_shared();
|
||||
++count;
|
||||
}
|
||||
print("counter_clockwise = ", count, '\n');
|
||||
}
|
||||
|
||||
void try_clockwise()
|
||||
{
|
||||
typedef boost::chrono::steady_clock Clock;
|
||||
unsigned count = 0;
|
||||
Clock::time_point until = Clock::now() + boost::chrono::seconds(3);
|
||||
while (Clock::now() < until)
|
||||
{
|
||||
if (mut.try_lock_shared())
|
||||
{
|
||||
assert(state == reading);
|
||||
if (mut.try_unlock_shared_and_lock())
|
||||
{
|
||||
state = writing;
|
||||
}
|
||||
else if (mut.try_unlock_shared_and_lock_upgrade())
|
||||
{
|
||||
assert(state == reading);
|
||||
mut.unlock_upgrade_and_lock();
|
||||
state = writing;
|
||||
}
|
||||
else
|
||||
{
|
||||
mut.unlock_shared();
|
||||
continue;
|
||||
}
|
||||
assert(state == writing);
|
||||
state = reading;
|
||||
mut.unlock_and_lock_upgrade();
|
||||
assert(state == reading);
|
||||
mut.unlock_upgrade_and_lock_shared();
|
||||
assert(state == reading);
|
||||
mut.unlock_shared();
|
||||
++count;
|
||||
}
|
||||
}
|
||||
print("try_clockwise = ", count, '\n');
|
||||
}
|
||||
|
||||
void try_for_clockwise()
|
||||
{
|
||||
typedef boost::chrono::steady_clock Clock;
|
||||
unsigned count = 0;
|
||||
Clock::time_point until = Clock::now() + boost::chrono::seconds(3);
|
||||
while (Clock::now() < until)
|
||||
{
|
||||
if (mut.try_lock_shared_for(boost::chrono::microseconds(5)))
|
||||
{
|
||||
assert(state == reading);
|
||||
if (mut.try_unlock_shared_and_lock_for(boost::chrono::microseconds(5)))
|
||||
{
|
||||
state = writing;
|
||||
}
|
||||
else if (mut.try_unlock_shared_and_lock_upgrade_for(boost::chrono::microseconds(5)))
|
||||
{
|
||||
assert(state == reading);
|
||||
mut.unlock_upgrade_and_lock();
|
||||
state = writing;
|
||||
}
|
||||
else
|
||||
{
|
||||
mut.unlock_shared();
|
||||
continue;
|
||||
}
|
||||
assert(state == writing);
|
||||
state = reading;
|
||||
mut.unlock_and_lock_upgrade();
|
||||
assert(state == reading);
|
||||
mut.unlock_upgrade_and_lock_shared();
|
||||
assert(state == reading);
|
||||
mut.unlock_shared();
|
||||
++count;
|
||||
}
|
||||
}
|
||||
print("try_for_clockwise = ", count, '\n');
|
||||
}
|
||||
|
||||
void try_counter_clockwise()
|
||||
{
|
||||
typedef boost::chrono::steady_clock Clock;
|
||||
unsigned count = 0;
|
||||
Clock::time_point until = Clock::now() + boost::chrono::seconds(3);
|
||||
while (Clock::now() < until)
|
||||
{
|
||||
if (mut.try_lock_upgrade())
|
||||
{
|
||||
assert(state == reading);
|
||||
if (mut.try_unlock_upgrade_and_lock())
|
||||
{
|
||||
assert(state == reading);
|
||||
state = writing;
|
||||
assert(state == writing);
|
||||
state = reading;
|
||||
mut.unlock_and_lock_shared();
|
||||
assert(state == reading);
|
||||
mut.unlock_shared();
|
||||
++count;
|
||||
}
|
||||
else
|
||||
{
|
||||
mut.unlock_upgrade();
|
||||
}
|
||||
}
|
||||
}
|
||||
print("try_counter_clockwise = ", count, '\n');
|
||||
}
|
||||
|
||||
void try_for_counter_clockwise()
|
||||
{
|
||||
typedef boost::chrono::steady_clock Clock;
|
||||
unsigned count = 0;
|
||||
Clock::time_point until = Clock::now() + boost::chrono::seconds(3);
|
||||
while (Clock::now() < until)
|
||||
{
|
||||
if (mut.try_lock_upgrade_for(boost::chrono::microseconds(5)))
|
||||
{
|
||||
assert(state == reading);
|
||||
if (mut.try_unlock_upgrade_and_lock_for(boost::chrono::microseconds(5)))
|
||||
{
|
||||
assert(state == reading);
|
||||
state = writing;
|
||||
assert(state == writing);
|
||||
state = reading;
|
||||
mut.unlock_and_lock_shared();
|
||||
assert(state == reading);
|
||||
mut.unlock_shared();
|
||||
++count;
|
||||
}
|
||||
else
|
||||
{
|
||||
mut.unlock_upgrade();
|
||||
}
|
||||
}
|
||||
}
|
||||
print("try_for_counter_clockwise = ", count, '\n');
|
||||
}
|
||||
|
||||
void
|
||||
test_upgrade_mutex()
|
||||
{
|
||||
std::cout << __FILE__ << "[" <<__LINE__ << "]" << std::endl;
|
||||
{
|
||||
boost::thread t1(reader);
|
||||
boost::thread t2(writer);
|
||||
boost::thread t3(reader);
|
||||
t1.join();
|
||||
t2.join();
|
||||
t3.join();
|
||||
}
|
||||
std::cout << __FILE__ << "[" <<__LINE__ << "]" << std::endl;
|
||||
{
|
||||
boost::thread t1(try_reader);
|
||||
boost::thread t2(try_writer);
|
||||
boost::thread t3(try_reader);
|
||||
t1.join();
|
||||
t2.join();
|
||||
t3.join();
|
||||
}
|
||||
std::cout << __FILE__ << "[" <<__LINE__ << "]" << std::endl;
|
||||
{
|
||||
boost::thread t1(try_for_reader);
|
||||
boost::thread t2(try_for_writer);
|
||||
boost::thread t3(try_for_reader);
|
||||
t1.join();
|
||||
t2.join();
|
||||
t3.join();
|
||||
}
|
||||
std::cout << __FILE__ << "[" <<__LINE__ << "]" << std::endl;
|
||||
{
|
||||
boost::thread t1(reader);
|
||||
boost::thread t2(writer);
|
||||
boost::thread t3(upgradable);
|
||||
t1.join();
|
||||
t2.join();
|
||||
t3.join();
|
||||
}
|
||||
std::cout << __FILE__ << "[" <<__LINE__ << "]" << std::endl;
|
||||
{
|
||||
boost::thread t1(reader);
|
||||
boost::thread t2(writer);
|
||||
boost::thread t3(try_upgradable);
|
||||
t1.join();
|
||||
t2.join();
|
||||
t3.join();
|
||||
}
|
||||
std::cout << __FILE__ << "[" <<__LINE__ << "]" << std::endl;
|
||||
{
|
||||
boost::thread t1(reader);
|
||||
boost::thread t2(writer);
|
||||
boost::thread t3(try_for_upgradable);
|
||||
t1.join();
|
||||
t2.join();
|
||||
t3.join();
|
||||
}
|
||||
std::cout << __FILE__ << "[" <<__LINE__ << "]" << std::endl;
|
||||
{
|
||||
state = reading;
|
||||
boost::thread t1(clockwise);
|
||||
boost::thread t2(counter_clockwise);
|
||||
boost::thread t3(clockwise);
|
||||
boost::thread t4(counter_clockwise);
|
||||
t1.join();
|
||||
t2.join();
|
||||
t3.join();
|
||||
t4.join();
|
||||
}
|
||||
std::cout << __FILE__ << "[" <<__LINE__ << "]" << std::endl;
|
||||
{
|
||||
state = reading;
|
||||
boost::thread t1(try_clockwise);
|
||||
boost::thread t2(try_counter_clockwise);
|
||||
t1.join();
|
||||
t2.join();
|
||||
}
|
||||
std::cout << __FILE__ << "[" <<__LINE__ << "]" << std::endl;
|
||||
// {
|
||||
// state = reading;
|
||||
// boost::thread t1(try_for_clockwise);
|
||||
// boost::thread t2(try_for_counter_clockwise);
|
||||
// t1.join();
|
||||
// t2.join();
|
||||
// }
|
||||
// std::cout << __FILE__ << "[" <<__LINE__ << "]" << std::endl;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
namespace Assignment
|
||||
{
|
||||
|
||||
class A
|
||||
{
|
||||
typedef boost::upgrade_mutex mutex_type;
|
||||
typedef boost::shared_lock<mutex_type> SharedLock;
|
||||
typedef boost::upgrade_lock<mutex_type> UpgradeLock;
|
||||
typedef boost::unique_lock<mutex_type> Lock;
|
||||
|
||||
mutable mutex_type mut_;
|
||||
std::vector<double> data_;
|
||||
|
||||
public:
|
||||
|
||||
A(const A& a)
|
||||
{
|
||||
SharedLock _(a.mut_);
|
||||
data_ = a.data_;
|
||||
}
|
||||
|
||||
A& operator=(const A& a)
|
||||
{
|
||||
if (this != &a)
|
||||
{
|
||||
Lock this_lock(mut_, boost::defer_lock);
|
||||
SharedLock that_lock(a.mut_, boost::defer_lock);
|
||||
boost::lock(this_lock, that_lock);
|
||||
data_ = a.data_;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
void swap(A& a)
|
||||
{
|
||||
Lock this_lock(mut_, boost::defer_lock);
|
||||
Lock that_lock(a.mut_, boost::defer_lock);
|
||||
boost::lock(this_lock, that_lock);
|
||||
data_.swap(a.data_);
|
||||
}
|
||||
|
||||
void average(A& a)
|
||||
{
|
||||
assert(data_.size() == a.data_.size());
|
||||
assert(this != &a);
|
||||
|
||||
Lock this_lock(mut_, boost::defer_lock);
|
||||
UpgradeLock share_that_lock(a.mut_, boost::defer_lock);
|
||||
boost::lock(this_lock, share_that_lock);
|
||||
|
||||
for (unsigned i = 0; i < data_.size(); ++i)
|
||||
data_[i] = (data_[i] + a.data_[i]) / 2;
|
||||
|
||||
SharedLock share_this_lock(boost::move(this_lock));
|
||||
Lock that_lock(boost::move(share_that_lock));
|
||||
a.data_ = data_;
|
||||
}
|
||||
};
|
||||
|
||||
} // Assignment
|
||||
|
||||
void temp()
|
||||
{
|
||||
using namespace boost;
|
||||
static upgrade_mutex mut;
|
||||
unique_lock<upgrade_mutex> ul(mut);
|
||||
shared_lock<upgrade_mutex> sl;
|
||||
sl = BOOST_THREAD_MAKE_RV_REF(shared_lock<upgrade_mutex>(boost::move(ul)));
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
std::cout << __FILE__ << "[" <<__LINE__ << "]" << std::endl;
|
||||
typedef boost::chrono::high_resolution_clock Clock;
|
||||
typedef boost::chrono::duration<double> sec;
|
||||
Clock::time_point t0 = Clock::now();
|
||||
|
||||
std::cout << __FILE__ << "[" <<__LINE__ << "]" << std::endl;
|
||||
S::test_shared_mutex();
|
||||
std::cout << __FILE__ << "[" <<__LINE__ << "]" << std::endl;
|
||||
U::test_upgrade_mutex();
|
||||
std::cout << __FILE__ << "[" <<__LINE__ << "]" << std::endl;
|
||||
Clock::time_point t1 = Clock::now();
|
||||
std::cout << sec(t1 - t0) << '\n';
|
||||
return 0;
|
||||
}
|
||||
|
||||
#else
|
||||
#error "This platform doesn't support Boost.Chrono"
|
||||
#endif
|
||||
187
example/starvephil.cpp
Normal file
187
example/starvephil.cpp
Normal file
@@ -0,0 +1,187 @@
|
||||
// Copyright (C) 2001-2003
|
||||
// William E. Kempf
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#define BOOST_THREAD_VERSION 2
|
||||
|
||||
#include <boost/thread/mutex.hpp>
|
||||
#include <boost/thread/condition.hpp>
|
||||
#include <boost/thread/thread.hpp>
|
||||
#include <boost/thread/xtime.hpp>
|
||||
#include <iostream>
|
||||
#include <time.h>
|
||||
|
||||
namespace
|
||||
{
|
||||
boost::mutex iomx;
|
||||
} // namespace
|
||||
|
||||
class canteen
|
||||
{
|
||||
public:
|
||||
canteen() : m_chickens(0) { }
|
||||
|
||||
void get(int id)
|
||||
{
|
||||
boost::unique_lock<boost::mutex> lock(m_mutex);
|
||||
while (m_chickens == 0)
|
||||
{
|
||||
{
|
||||
boost::unique_lock<boost::mutex> lk(iomx);
|
||||
std::cout << "(" << clock() << ") Phil" << id <<
|
||||
": wot, no chickens? I'll WAIT ..." << std::endl;
|
||||
}
|
||||
m_condition.wait(lock);
|
||||
}
|
||||
{
|
||||
boost::unique_lock<boost::mutex> lk(iomx);
|
||||
std::cout << "(" << clock() << ") Phil" << id <<
|
||||
": those chickens look good ... one please ..." << std::endl;
|
||||
}
|
||||
m_chickens--;
|
||||
}
|
||||
void put(int value)
|
||||
{
|
||||
boost::unique_lock<boost::mutex> lock(m_mutex);
|
||||
{
|
||||
boost::unique_lock<boost::mutex> lk(iomx);
|
||||
std::cout << "(" << clock()
|
||||
<< ") Chef: ouch ... make room ... this dish is "
|
||||
<< "very hot ..." << std::endl;
|
||||
}
|
||||
boost::xtime xt;
|
||||
boost::xtime_get(&xt, boost::TIME_UTC_);
|
||||
xt.sec += 3;
|
||||
boost::thread::sleep(xt);
|
||||
m_chickens += value;
|
||||
{
|
||||
boost::unique_lock<boost::mutex> lk(iomx);
|
||||
std::cout << "(" << clock() <<
|
||||
") Chef: more chickens ... " << m_chickens <<
|
||||
" now available ... NOTIFYING ..." << std::endl;
|
||||
}
|
||||
m_condition.notify_all();
|
||||
}
|
||||
|
||||
private:
|
||||
boost::mutex m_mutex;
|
||||
boost::condition m_condition;
|
||||
int m_chickens;
|
||||
};
|
||||
|
||||
canteen g_canteen;
|
||||
|
||||
void chef()
|
||||
{
|
||||
const int chickens = 4;
|
||||
{
|
||||
boost::unique_lock<boost::mutex> lock(iomx);
|
||||
std::cout << "(" << clock() << ") Chef: starting ..." << std::endl;
|
||||
}
|
||||
for (;;)
|
||||
{
|
||||
{
|
||||
boost::unique_lock<boost::mutex> lock(iomx);
|
||||
std::cout << "(" << clock() << ") Chef: cooking ..." << std::endl;
|
||||
}
|
||||
boost::xtime xt;
|
||||
boost::xtime_get(&xt, boost::TIME_UTC_);
|
||||
xt.sec += 2;
|
||||
boost::thread::sleep(xt);
|
||||
{
|
||||
boost::unique_lock<boost::mutex> lock(iomx);
|
||||
std::cout << "(" << clock() << ") Chef: " << chickens
|
||||
<< " chickens, ready-to-go ..." << std::endl;
|
||||
}
|
||||
g_canteen.put(chickens);
|
||||
}
|
||||
}
|
||||
|
||||
struct phil
|
||||
{
|
||||
phil(int id) : m_id(id) { }
|
||||
void run() {
|
||||
{
|
||||
boost::unique_lock<boost::mutex> lock(iomx);
|
||||
std::cout << "(" << clock() << ") Phil" << m_id
|
||||
<< ": starting ..." << std::endl;
|
||||
}
|
||||
for (;;)
|
||||
{
|
||||
if (m_id > 0)
|
||||
{
|
||||
boost::xtime xt;
|
||||
boost::xtime_get(&xt, boost::TIME_UTC_);
|
||||
xt.sec += 3;
|
||||
boost::thread::sleep(xt);
|
||||
}
|
||||
{
|
||||
boost::unique_lock<boost::mutex> lk(iomx);
|
||||
std::cout << "(" << clock() << ") Phil" << m_id
|
||||
<< ": gotta eat ..." << std::endl;
|
||||
}
|
||||
g_canteen.get(m_id);
|
||||
{
|
||||
boost::unique_lock<boost::mutex> lk(iomx);
|
||||
std::cout << "(" << clock() << ") Phil" << m_id
|
||||
<< ": mmm ... that's good ..." << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
static void do_thread(void* param) {
|
||||
static_cast<phil*>(param)->run();
|
||||
}
|
||||
|
||||
int m_id;
|
||||
};
|
||||
|
||||
struct thread_adapt
|
||||
{
|
||||
thread_adapt(void (*func)(void*), void* param)
|
||||
: _func(func), _param(param)
|
||||
{
|
||||
}
|
||||
int operator()() const
|
||||
{
|
||||
_func(_param);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void (*_func)(void*);
|
||||
void* _param;
|
||||
};
|
||||
|
||||
class thread_adapter
|
||||
{
|
||||
public:
|
||||
thread_adapter(void (*func)(void*), void* param)
|
||||
: _func(func), _param(param)
|
||||
{
|
||||
}
|
||||
void operator()() const { _func(_param); }
|
||||
private:
|
||||
void (*_func)(void*);
|
||||
void* _param;
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
boost::thread thrd_chef(&chef);
|
||||
phil p[] = { phil(0), phil(1), phil(2), phil(3), phil(4) };
|
||||
boost::thread thrd_phil0(thread_adapter(&phil::do_thread, &p[0]));
|
||||
boost::thread thrd_phil1(thread_adapter(&phil::do_thread, &p[1]));
|
||||
boost::thread thrd_phil2(thread_adapter(&phil::do_thread, &p[2]));
|
||||
boost::thread thrd_phil3(thread_adapter(&phil::do_thread, &p[3]));
|
||||
boost::thread thrd_phil4(thread_adapter(&phil::do_thread, &p[4]));
|
||||
|
||||
thrd_chef.join();
|
||||
thrd_phil0.join();
|
||||
thrd_phil1.join();
|
||||
thrd_phil2.join();
|
||||
thrd_phil3.join();
|
||||
thrd_phil4.join();
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1,171 +0,0 @@
|
||||
#include <boost/thread/mutex.hpp>
|
||||
#include <boost/thread/condition.hpp>
|
||||
#include <boost/thread/thread.hpp>
|
||||
#include <boost/thread/xtime.hpp>
|
||||
#include <iostream>
|
||||
#include <time.h>
|
||||
|
||||
namespace
|
||||
{
|
||||
boost::mutex iomx;
|
||||
};
|
||||
|
||||
class canteen
|
||||
{
|
||||
public:
|
||||
canteen() : m_chickens(0) { }
|
||||
|
||||
void get(int id)
|
||||
{
|
||||
boost::mutex::scoped_lock lock(m_mutex);
|
||||
while (m_chickens == 0)
|
||||
{
|
||||
{
|
||||
boost::mutex::scoped_lock lock(iomx);
|
||||
std::cout << "(" << clock() << ") Phil" << id <<
|
||||
": wot, no chickens? I'll WAIT ..." << std::endl;
|
||||
}
|
||||
m_condition.wait(lock);
|
||||
}
|
||||
{
|
||||
boost::mutex::scoped_lock lock(iomx);
|
||||
std::cout << "(" << clock() << ") Phil" << id <<
|
||||
": those chickens look good ... one please ..." << std::endl;
|
||||
}
|
||||
m_chickens--;
|
||||
}
|
||||
void put(int value)
|
||||
{
|
||||
boost::mutex::scoped_lock lock(m_mutex);
|
||||
{
|
||||
boost::mutex::scoped_lock lock(iomx);
|
||||
std::cout << "(" << clock() <<
|
||||
") Chef: ouch ... make room ... this dish is very hot ..." << std::endl;
|
||||
}
|
||||
boost::xtime xt;
|
||||
boost::xtime_get(&xt, boost::TIME_UTC);
|
||||
xt.sec += 3;
|
||||
boost::thread::sleep(xt);
|
||||
m_chickens += value;
|
||||
{
|
||||
boost::mutex::scoped_lock lock(iomx);
|
||||
std::cout << "(" << clock() <<
|
||||
") Chef: more chickens ... " << m_chickens <<
|
||||
" now available ... NOTIFYING ..." << std::endl;
|
||||
}
|
||||
m_condition.notify_all();
|
||||
}
|
||||
|
||||
private:
|
||||
boost::mutex m_mutex;
|
||||
boost::condition m_condition;
|
||||
int m_chickens;
|
||||
};
|
||||
|
||||
canteen g_canteen;
|
||||
|
||||
void chef()
|
||||
{
|
||||
const int chickens = 4;
|
||||
{
|
||||
boost::mutex::scoped_lock lock(iomx);
|
||||
std::cout << "(" << clock() << ") Chef: starting ..." << std::endl;
|
||||
}
|
||||
for (;;)
|
||||
{
|
||||
{
|
||||
boost::mutex::scoped_lock lock(iomx);
|
||||
std::cout << "(" << clock() << ") Chef: cooking ..." << std::endl;
|
||||
}
|
||||
boost::xtime xt;
|
||||
boost::xtime_get(&xt, boost::TIME_UTC);
|
||||
xt.sec += 2;
|
||||
boost::thread::sleep(xt);
|
||||
{
|
||||
boost::mutex::scoped_lock lock(iomx);
|
||||
std::cout << "(" << clock() << ") Chef: " << chickens
|
||||
<< " chickens, ready-to-go ..." << std::endl;
|
||||
}
|
||||
g_canteen.put(chickens);
|
||||
}
|
||||
}
|
||||
|
||||
struct phil
|
||||
{
|
||||
phil(int id) : m_id(id) { }
|
||||
void run() {
|
||||
{
|
||||
boost::mutex::scoped_lock lock(iomx);
|
||||
std::cout << "(" << clock() << ") Phil" << m_id << ": starting ..." << std::endl;
|
||||
}
|
||||
for (;;)
|
||||
{
|
||||
if (m_id > 0)
|
||||
{
|
||||
boost::xtime xt;
|
||||
boost::xtime_get(&xt, boost::TIME_UTC);
|
||||
xt.sec += 3;
|
||||
boost::thread::sleep(xt);
|
||||
}
|
||||
{
|
||||
boost::mutex::scoped_lock lock(iomx);
|
||||
std::cout << "(" << clock() << ") Phil" << m_id
|
||||
<< ": gotta eat ..." << std::endl;
|
||||
}
|
||||
g_canteen.get(m_id);
|
||||
{
|
||||
boost::mutex::scoped_lock lock(iomx);
|
||||
std::cout << "(" << clock() << ") Phil" << m_id
|
||||
<< ": mmm ... that's good ..." << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
static void do_thread(void* param) {
|
||||
static_cast<phil*>(param)->run();
|
||||
}
|
||||
|
||||
int m_id;
|
||||
};
|
||||
|
||||
struct thread_adapt
|
||||
{
|
||||
thread_adapt(void (*func)(void*), void* param) : _func(func), _param(param) { }
|
||||
int operator()() const
|
||||
{
|
||||
_func(_param);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void (*_func)(void*);
|
||||
void* _param;
|
||||
};
|
||||
|
||||
class thread_adapter
|
||||
{
|
||||
public:
|
||||
thread_adapter(void (*func)(void*), void* param) : _func(func), _param(param) { }
|
||||
void operator()() const { _func(_param); }
|
||||
private:
|
||||
void (*_func)(void*);
|
||||
void* _param;
|
||||
};
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
boost::thread thrd_chef(&chef);
|
||||
phil p[] = { phil(0), phil(1), phil(2), phil(3), phil(4) };
|
||||
boost::thread thrd_phil0(thread_adapter(&phil::do_thread, &p[0]));
|
||||
boost::thread thrd_phil1(thread_adapter(&phil::do_thread, &p[1]));
|
||||
boost::thread thrd_phil2(thread_adapter(&phil::do_thread, &p[2]));
|
||||
boost::thread thrd_phil3(thread_adapter(&phil::do_thread, &p[3]));
|
||||
boost::thread thrd_phil4(thread_adapter(&phil::do_thread, &p[4]));
|
||||
|
||||
thrd_chef.join();
|
||||
thrd_phil0.join();
|
||||
thrd_phil1.join();
|
||||
thrd_phil2.join();
|
||||
thrd_phil3.join();
|
||||
thrd_phil4.join();
|
||||
|
||||
return 0;
|
||||
}
|
||||
40
example/strict_lock.cpp
Normal file
40
example/strict_lock.cpp
Normal file
@@ -0,0 +1,40 @@
|
||||
// Copyright (C) 2012 Vicente Botet
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#define BOOST_THREAD_VERSION 4
|
||||
|
||||
#include <boost/thread/mutex.hpp>
|
||||
#include <boost/thread/lock_traits.hpp>
|
||||
#include <boost/thread/shared_mutex.hpp>
|
||||
#include <boost/thread/strict_lock.hpp>
|
||||
#include <boost/thread/lock_types.hpp>
|
||||
#include <iostream>
|
||||
|
||||
|
||||
BOOST_STATIC_ASSERT(boost::is_strict_lock<boost::strict_lock<boost::mutex> >::value);
|
||||
BOOST_CONCEPT_ASSERT(( boost::BasicLockable<boost::mutex> ));
|
||||
BOOST_CONCEPT_ASSERT(( boost::StrictLock<boost::strict_lock<boost::mutex> > ));
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
boost::mutex mtx;
|
||||
boost::strict_lock<boost::mutex> lk(mtx);
|
||||
std::cout << __FILE__ << std::endl;
|
||||
}
|
||||
{
|
||||
boost::timed_mutex mtx;
|
||||
boost::unique_lock<boost::timed_mutex> lk(mtx);
|
||||
boost::nested_strict_lock<boost::unique_lock<boost::timed_mutex> > nlk(lk);
|
||||
std::cout << __FILE__ << std::endl;
|
||||
}
|
||||
{
|
||||
boost::mutex mtx;
|
||||
boost::unique_lock<boost::mutex> lk(mtx, boost::defer_lock);
|
||||
boost::nested_strict_lock<boost::unique_lock<boost::mutex> > nlk(lk);
|
||||
std::cout << __FILE__ << std::endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
259
example/synchronized_person.cpp
Normal file
259
example/synchronized_person.cpp
Normal file
@@ -0,0 +1,259 @@
|
||||
// (C) Copyright 2012 Vicente Botet
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#define BOOST_THREAD_VERSION 4
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <boost/thread/synchronized_value.hpp>
|
||||
|
||||
//class SafePerson {
|
||||
//public:
|
||||
// std::string GetName() const {
|
||||
// const_unique_access<std::string> name(nameGuard);
|
||||
// return *name;
|
||||
// }
|
||||
// void SetName(const std::string& newName) {
|
||||
// unique_access<std::string> name(nameGuard);
|
||||
// *name = newName;
|
||||
// }
|
||||
//private:
|
||||
// unique_access_guard<std::string> nameGuard;
|
||||
//};
|
||||
|
||||
class SafePerson {
|
||||
public:
|
||||
std::string GetName() const {
|
||||
return *name;
|
||||
}
|
||||
void SetName(const std::string& newName) {
|
||||
*name = newName;
|
||||
}
|
||||
|
||||
private:
|
||||
boost::synchronized_value<std::string> name;
|
||||
};
|
||||
|
||||
class Person {
|
||||
public:
|
||||
std::string GetName() const {
|
||||
return name;
|
||||
}
|
||||
void SetName(const std::string& newName) {
|
||||
name = newName;
|
||||
}
|
||||
private:
|
||||
std::string name;
|
||||
};
|
||||
typedef boost::synchronized_value<Person> Person_ts;
|
||||
|
||||
|
||||
//class SafeMemberPerson {
|
||||
//public:
|
||||
// SafeMemberPerson(unsigned int age) :
|
||||
// memberGuard(age)
|
||||
// { }
|
||||
// std::string GetName() const {
|
||||
// const_unique_access<Member> member(memberGuard);
|
||||
// return member->name;
|
||||
// }
|
||||
// void SetName(const std::string& newName) {
|
||||
// unique_access<Member> member(memberGuard);
|
||||
// member->name = newName;
|
||||
// }
|
||||
//private:
|
||||
// struct Member
|
||||
// {
|
||||
// Member(unsigned int age) :
|
||||
// age(age)
|
||||
// { }
|
||||
// std::string name;
|
||||
// unsigned int age;
|
||||
// };
|
||||
// unique_access_guard<Member> memberGuard;
|
||||
//};
|
||||
|
||||
class SafeMemberPerson {
|
||||
public:
|
||||
SafeMemberPerson(unsigned int age) :
|
||||
member(Member(age))
|
||||
{ }
|
||||
std::string GetName() const {
|
||||
return member->name;
|
||||
}
|
||||
void SetName(const std::string& newName) {
|
||||
member->name = newName;
|
||||
}
|
||||
private:
|
||||
struct Member {
|
||||
Member(unsigned int age) :
|
||||
age(age)
|
||||
{ }
|
||||
std::string name;
|
||||
unsigned int age;
|
||||
};
|
||||
boost::synchronized_value<Member> member;
|
||||
};
|
||||
|
||||
|
||||
class Person2 {
|
||||
public:
|
||||
Person2(unsigned int age) : age_(age)
|
||||
{}
|
||||
std::string GetName() const {
|
||||
return name_;
|
||||
}
|
||||
void SetName(const std::string& newName) {
|
||||
name_ = newName;
|
||||
}
|
||||
unsigned int GetAge() const {
|
||||
return age_;
|
||||
}
|
||||
|
||||
private:
|
||||
std::string name_;
|
||||
unsigned int age_;
|
||||
};
|
||||
typedef boost::synchronized_value<Person2> Person2_ts;
|
||||
|
||||
//===================
|
||||
|
||||
//class HelperPerson {
|
||||
//public:
|
||||
// HelperPerson(unsigned int age) :
|
||||
// memberGuard(age)
|
||||
// { }
|
||||
// std::string GetName() const {
|
||||
// const_unique_access<Member> member(memberGuard);
|
||||
// Invariant(member);
|
||||
// return member->name;
|
||||
// }
|
||||
// void SetName(const std::string& newName) {
|
||||
// unique_access<Member> member(memberGuard);
|
||||
// Invariant(member);
|
||||
// member->name = newName;
|
||||
// }
|
||||
//private:
|
||||
// void Invariant(const_unique_access<Member>& member) const {
|
||||
// if (member->age < 0) throw std::runtime_error("Age cannot be negative");
|
||||
// }
|
||||
// struct Member {
|
||||
// Member(unsigned int age) :
|
||||
// age(age)
|
||||
// { }
|
||||
// std::string name;
|
||||
// unsigned int age;
|
||||
// };
|
||||
// unique_access_guard<Member> memberGuard;
|
||||
//};
|
||||
|
||||
class HelperPerson {
|
||||
public:
|
||||
HelperPerson(unsigned int age) :
|
||||
member(age)
|
||||
{ }
|
||||
std::string GetName() const {
|
||||
#if ! defined BOOST_NO_CXX11_AUTO_DECLARATIONS
|
||||
auto memberSync = member.synchronize();
|
||||
#else
|
||||
boost::const_strict_lock_ptr<Member> memberSync = member.synchronize();
|
||||
#endif
|
||||
Invariant(memberSync);
|
||||
return memberSync->name;
|
||||
}
|
||||
void SetName(const std::string& newName) {
|
||||
#if ! defined BOOST_NO_CXX11_AUTO_DECLARATIONS
|
||||
auto memberSync = member.synchronize();
|
||||
#else
|
||||
boost::strict_lock_ptr<Member> memberSync = member.synchronize();
|
||||
#endif
|
||||
Invariant(memberSync);
|
||||
memberSync->name = newName;
|
||||
}
|
||||
private:
|
||||
struct Member {
|
||||
Member(unsigned int age) :
|
||||
age(age)
|
||||
{ }
|
||||
std::string name;
|
||||
unsigned int age;
|
||||
};
|
||||
void Invariant(boost::const_strict_lock_ptr<Member> & mbr) const
|
||||
{
|
||||
if (mbr->age < 1) throw std::runtime_error("Age cannot be negative");
|
||||
}
|
||||
boost::synchronized_value<Member> member;
|
||||
};
|
||||
|
||||
class Person3 {
|
||||
public:
|
||||
Person3(unsigned int age) :
|
||||
age_(age)
|
||||
{ }
|
||||
std::string GetName() const {
|
||||
Invariant();
|
||||
return name_;
|
||||
}
|
||||
void SetName(const std::string& newName) {
|
||||
Invariant();
|
||||
name_ = newName;
|
||||
}
|
||||
private:
|
||||
std::string name_;
|
||||
unsigned int age_;
|
||||
void Invariant() const {
|
||||
if (age_ < 1) throw std::runtime_error("Age cannot be negative");
|
||||
}
|
||||
};
|
||||
|
||||
typedef boost::synchronized_value<Person3> Person3_ts;
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
SafePerson p;
|
||||
p.SetName("Vicente");
|
||||
}
|
||||
{
|
||||
Person_ts p;
|
||||
p->SetName("Vicente");
|
||||
}
|
||||
{
|
||||
SafeMemberPerson p(1);
|
||||
p.SetName("Vicente");
|
||||
}
|
||||
{
|
||||
Person2_ts p(1);
|
||||
p->SetName("Vicente");
|
||||
}
|
||||
{
|
||||
HelperPerson p(1);
|
||||
p.SetName("Vicente");
|
||||
}
|
||||
{
|
||||
Person3_ts p(1);
|
||||
p->SetName("Vicente");
|
||||
}
|
||||
{
|
||||
Person3_ts p1(1);
|
||||
Person3_ts p2(2);
|
||||
Person3_ts p3(3);
|
||||
#if ! defined BOOST_NO_CXX11_AUTO_DECLARATIONS
|
||||
auto lk1 = p1.unique_synchronize(boost::defer_lock);
|
||||
auto lk2 = p2.unique_synchronize(boost::defer_lock);
|
||||
auto lk3 = p3.unique_synchronize(boost::defer_lock);
|
||||
#else
|
||||
boost::unique_lock_ptr<Person3> lk1 = p1.unique_synchronize(boost::defer_lock);
|
||||
boost::unique_lock_ptr<Person3> lk2 = p2.unique_synchronize(boost::defer_lock);
|
||||
boost::unique_lock_ptr<Person3> lk3 = p3.unique_synchronize(boost::defer_lock);
|
||||
#endif
|
||||
boost::lock(lk1,lk2,lk3);
|
||||
|
||||
lk1->SetName("Carmen");
|
||||
lk2->SetName("Javier");
|
||||
lk3->SetName("Matias");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
85
example/synchronized_value.cpp
Normal file
85
example/synchronized_value.cpp
Normal file
@@ -0,0 +1,85 @@
|
||||
// (C) Copyright 2010 Just Software Solutions Ltd http://www.justsoftwaresolutions.co.uk
|
||||
// (C) Copyright 2012 Vicente Botet
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#define BOOST_THREAD_VERSION 4
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <boost/thread/synchronized_value.hpp>
|
||||
|
||||
void addTrailingSlashIfMissing(boost::synchronized_value<std::string> & path)
|
||||
{
|
||||
boost::strict_lock_ptr<std::string> u=path.synchronize();
|
||||
|
||||
if(u->empty() || (*u->rbegin()!='/'))
|
||||
{
|
||||
*u+='/';
|
||||
}
|
||||
}
|
||||
|
||||
void f(const boost::synchronized_value<int> &v) {
|
||||
std::cout<<"v="<<*v<<std::endl;
|
||||
}
|
||||
|
||||
void g(const boost::const_strict_lock_ptr<int> &v) {
|
||||
std::cout<<"v="<<*v<<std::endl;
|
||||
}
|
||||
|
||||
bool checkIfMissingTrailingSlash(boost::synchronized_value<std::string> & path)
|
||||
{
|
||||
boost::strict_lock_ptr<std::string> u=path.synchronize();
|
||||
|
||||
return (u->empty() || (*u->rbegin()!='/'));
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
boost::synchronized_value<int> v1;
|
||||
*v1=42;
|
||||
std::cout<<"v1="<<*v1<<std::endl;
|
||||
f(v1);
|
||||
int i=*v1;
|
||||
std::cout<<"i="<<i<<std::endl;
|
||||
|
||||
{
|
||||
boost::strict_lock_ptr<int> u=v1.synchronize();
|
||||
|
||||
*u+=43;
|
||||
std::cout<<"v1="<<*u<<std::endl;
|
||||
g(u);
|
||||
}
|
||||
boost::synchronized_value<int> v2(2);
|
||||
std::cout<<"v2="<<*v2<<std::endl;
|
||||
v2 = 3;
|
||||
std::cout<<"v2="<<*v2<<std::endl;
|
||||
|
||||
boost::synchronized_value<int> v3(v2);
|
||||
std::cout<<"v3="<<*v3<<std::endl;
|
||||
v3 = v1;
|
||||
std::cout<<"v3="<<*v3<<std::endl;
|
||||
|
||||
std::cout<<"v2="<<*v3<<std::endl;
|
||||
std::cout<<"v3="<<*v3<<std::endl;
|
||||
swap(v3,v2);
|
||||
v1.swap(v2);
|
||||
std::cout<<"v3="<<*v3<<std::endl;
|
||||
}
|
||||
{
|
||||
boost::synchronized_value<std::string> s;
|
||||
addTrailingSlashIfMissing(s);
|
||||
std::cout<<"s="<<std::string(*s)<<std::endl;
|
||||
}
|
||||
{
|
||||
boost::synchronized_value<std::string> s;
|
||||
s->append("foo/");
|
||||
s.synchronize()->append("foo");
|
||||
addTrailingSlashIfMissing(s);
|
||||
std::cout<<"s="<<std::string(*s)<<std::endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1,3 +1,9 @@
|
||||
// Copyright (C) 2001-2003
|
||||
// William E. Kempf
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#include <boost/thread/mutex.hpp>
|
||||
#include <boost/thread/condition.hpp>
|
||||
#include <boost/thread/thread.hpp>
|
||||
@@ -5,8 +11,8 @@
|
||||
#include <iostream>
|
||||
|
||||
#if defined(BOOST_HAS_WINTHREADS)
|
||||
# include <windows.h>
|
||||
# include <process.h>
|
||||
# include <windows.h>
|
||||
# include <process.h>
|
||||
#endif
|
||||
|
||||
enum game_state
|
||||
@@ -35,7 +41,7 @@ char* player_name(int state)
|
||||
|
||||
void player(void* param)
|
||||
{
|
||||
boost::mutex::scoped_lock lock(mutex);
|
||||
boost::unique_lock<boost::mutex> lock(mutex);
|
||||
|
||||
int active = (int)param;
|
||||
int other = active == PLAYER_A ? PLAYER_B : PLAYER_A;
|
||||
@@ -49,7 +55,10 @@ void player(void* param)
|
||||
{
|
||||
cond.wait(lock);
|
||||
if (state == other)
|
||||
std::cout << "---" << player_name(active) << ": Spurious wakeup!" << std::endl;
|
||||
{
|
||||
std::cout << "---" << player_name(active)
|
||||
<< ": Spurious wakeup!" << std::endl;
|
||||
}
|
||||
} while (state == other);
|
||||
}
|
||||
|
||||
@@ -60,7 +69,10 @@ void player(void* param)
|
||||
|
||||
struct thread_adapt
|
||||
{
|
||||
thread_adapt(void (*func)(void*), void* param) : _func(func), _param(param) { }
|
||||
thread_adapt(void (*func)(void*), void* param)
|
||||
: _func(func), _param(param)
|
||||
{
|
||||
}
|
||||
int operator()() const
|
||||
{
|
||||
_func(_param);
|
||||
@@ -74,7 +86,10 @@ struct thread_adapt
|
||||
class thread_adapter
|
||||
{
|
||||
public:
|
||||
thread_adapter(void (*func)(void*), void* param) : _func(func), _param(param) { }
|
||||
thread_adapter(void (*func)(void*), void* param)
|
||||
: _func(func), _param(param)
|
||||
{
|
||||
}
|
||||
void operator()() const { _func(_param); }
|
||||
private:
|
||||
void (*_func)(void*);
|
||||
@@ -89,19 +104,19 @@ int main(int argc, char* argv[])
|
||||
boost::thread thrdb(thread_adapter(&player, (void*)PLAYER_B));
|
||||
|
||||
boost::xtime xt;
|
||||
boost::xtime_get(&xt, boost::TIME_UTC);
|
||||
boost::xtime_get(&xt, boost::TIME_UTC_);
|
||||
xt.sec += 1;
|
||||
boost::thread::sleep(xt);
|
||||
{
|
||||
boost::mutex::scoped_lock lock(mutex);
|
||||
boost::unique_lock<boost::mutex> lock(mutex);
|
||||
std::cout << "---Noise ON..." << std::endl;
|
||||
}
|
||||
|
||||
for (int i = 0; i < 1000000; ++i)
|
||||
for (int i = 0; i < 1000000000; ++i)
|
||||
cond.notify_all();
|
||||
|
||||
{
|
||||
boost::mutex::scoped_lock lock(mutex);
|
||||
boost::unique_lock<boost::mutex> lock(mutex);
|
||||
std::cout << "---Noise OFF..." << std::endl;
|
||||
state = GAME_OVER;
|
||||
cond.notify_all();
|
||||
37
example/thread.cpp
Normal file
37
example/thread.cpp
Normal file
@@ -0,0 +1,37 @@
|
||||
// Copyright (C) 2001-2003
|
||||
// William E. Kempf
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#define BOOST_THREAD_VERSION 2
|
||||
|
||||
#include <boost/thread/thread.hpp>
|
||||
#include <boost/thread/xtime.hpp>
|
||||
#include <iostream>
|
||||
|
||||
struct thread_alarm
|
||||
{
|
||||
thread_alarm(int secs) : m_secs(secs) { }
|
||||
void operator()()
|
||||
{
|
||||
boost::xtime xt;
|
||||
boost::xtime_get(&xt, boost::TIME_UTC_);
|
||||
xt.sec += m_secs;
|
||||
|
||||
boost::thread::sleep(xt);
|
||||
|
||||
std::cout << "alarm sounded..." << std::endl;
|
||||
}
|
||||
|
||||
int m_secs;
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
int secs = 5;
|
||||
std::cout << "setting alarm for 5 seconds..." << std::endl;
|
||||
thread_alarm alarm(secs);
|
||||
boost::thread thrd(alarm);
|
||||
thrd.join();
|
||||
}
|
||||
72
example/thread_group.cpp
Normal file
72
example/thread_group.cpp
Normal file
@@ -0,0 +1,72 @@
|
||||
// Copyright (C) 2001-2003
|
||||
// William E. Kempf
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#include <boost/thread/thread.hpp>
|
||||
#include <iostream>
|
||||
#include <boost/detail/lightweight_test.hpp>
|
||||
|
||||
int count = 0;
|
||||
boost::mutex mutex;
|
||||
|
||||
void increment_count()
|
||||
{
|
||||
boost::unique_lock<boost::mutex> lock(mutex);
|
||||
std::cout << "count = " << ++count << std::endl;
|
||||
}
|
||||
|
||||
boost::thread_group threads2;
|
||||
boost::thread* th2 = 0;
|
||||
|
||||
void increment_count_2()
|
||||
{
|
||||
boost::unique_lock<boost::mutex> lock(mutex);
|
||||
BOOST_TEST(threads2.is_this_thread_in());
|
||||
std::cout << "count = " << ++count << std::endl;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
boost::thread_group threads;
|
||||
for (int i = 0; i < 3; ++i)
|
||||
threads.create_thread(&increment_count);
|
||||
threads.join_all();
|
||||
}
|
||||
#if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
|
||||
{
|
||||
boost::thread_group threads;
|
||||
for (int i = 0; i < 3; ++i)
|
||||
threads.create_thread(&increment_count);
|
||||
threads.interrupt_all();
|
||||
threads.join_all();
|
||||
}
|
||||
#endif
|
||||
{
|
||||
boost::thread_group threads;
|
||||
boost::thread* th = new boost::thread(&increment_count);
|
||||
threads.add_thread(th);
|
||||
BOOST_TEST(! threads.is_this_thread_in());
|
||||
threads.join_all();
|
||||
}
|
||||
{
|
||||
boost::thread_group threads;
|
||||
boost::thread* th = new boost::thread(&increment_count);
|
||||
threads.add_thread(th);
|
||||
BOOST_TEST(threads.is_thread_in(th));
|
||||
threads.remove_thread(th);
|
||||
BOOST_TEST(! threads.is_thread_in(th));
|
||||
th->join();
|
||||
}
|
||||
{
|
||||
{
|
||||
boost::unique_lock<boost::mutex> lock(mutex);
|
||||
boost::thread* th2 = new boost::thread(&increment_count_2);
|
||||
threads2.add_thread(th2);
|
||||
}
|
||||
threads2.join_all();
|
||||
}
|
||||
return boost::report_errors();
|
||||
}
|
||||
57
example/thread_guard.cpp
Normal file
57
example/thread_guard.cpp
Normal file
@@ -0,0 +1,57 @@
|
||||
// (C) Copyright 2009-2012 Anthony Williams
|
||||
// (C) Copyright 2012 Vicente Botet
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <boost/thread/thread.hpp>
|
||||
#include <boost/thread/thread_guard.hpp>
|
||||
|
||||
void do_something(int& i)
|
||||
{
|
||||
++i;
|
||||
}
|
||||
|
||||
struct func
|
||||
{
|
||||
int& i;
|
||||
|
||||
func(int& i_):i(i_){}
|
||||
|
||||
void operator()()
|
||||
{
|
||||
for(unsigned j=0;j<1000000;++j)
|
||||
{
|
||||
do_something(i);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
func& operator=(func const&);
|
||||
|
||||
};
|
||||
|
||||
void do_something_in_current_thread()
|
||||
{}
|
||||
|
||||
|
||||
void f()
|
||||
{
|
||||
int some_local_state;
|
||||
func my_func(some_local_state);
|
||||
boost::thread t(my_func);
|
||||
boost::thread_guard<> g(t);
|
||||
|
||||
do_something_in_current_thread();
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
f();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
36
example/tss.cpp
Normal file
36
example/tss.cpp
Normal file
@@ -0,0 +1,36 @@
|
||||
// Copyright (C) 2001-2003
|
||||
// William E. Kempf
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#include <boost/thread/thread.hpp>
|
||||
#include <boost/thread/tss.hpp>
|
||||
#include <cassert>
|
||||
|
||||
boost::thread_specific_ptr<int> value;
|
||||
|
||||
void increment()
|
||||
{
|
||||
int* p = value.get();
|
||||
++*p;
|
||||
}
|
||||
|
||||
void thread_proc()
|
||||
{
|
||||
value.reset(new int(0)); // initialize the thread's storage
|
||||
for (int i=0; i<10; ++i)
|
||||
{
|
||||
increment();
|
||||
int* p = value.get();
|
||||
assert(*p == i+1);
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
boost::thread_group threads;
|
||||
for (int i=0; i<5; ++i)
|
||||
threads.create_thread(&thread_proc);
|
||||
threads.join_all();
|
||||
}
|
||||
18
example/xtime.cpp
Normal file
18
example/xtime.cpp
Normal file
@@ -0,0 +1,18 @@
|
||||
// Copyright (C) 2001-2003
|
||||
// William E. Kempf
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#define BOOST_THREAD_VERSION 2
|
||||
|
||||
#include <boost/thread/thread.hpp>
|
||||
#include <boost/thread/xtime.hpp>
|
||||
|
||||
int main()
|
||||
{
|
||||
boost::xtime xt;
|
||||
boost::xtime_get(&xt, boost::TIME_UTC_);
|
||||
xt.sec += 1;
|
||||
boost::thread::sleep(xt); // Sleep for 1 second
|
||||
}
|
||||
26
include/boost/thread.hpp
Normal file
26
include/boost/thread.hpp
Normal file
@@ -0,0 +1,26 @@
|
||||
// Copyright (C) 2001-2003
|
||||
// William E. Kempf
|
||||
// (C) Copyright 2008-9 Anthony Williams
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
// See www.boost.org/libs/thread for documentation.
|
||||
|
||||
#if !defined(BOOST_THREAD_WEK01082003_HPP)
|
||||
#define BOOST_THREAD_WEK01082003_HPP
|
||||
|
||||
#include <boost/thread/thread.hpp>
|
||||
#include <boost/thread/condition_variable.hpp>
|
||||
#include <boost/thread/exceptions.hpp>
|
||||
#include <boost/thread/mutex.hpp>
|
||||
#include <boost/thread/once.hpp>
|
||||
#include <boost/thread/recursive_mutex.hpp>
|
||||
#include <boost/thread/tss.hpp>
|
||||
#include <boost/thread/thread_time.hpp>
|
||||
#include <boost/thread/locks.hpp>
|
||||
#include <boost/thread/shared_mutex.hpp>
|
||||
#include <boost/thread/barrier.hpp>
|
||||
#include <boost/thread/future.hpp>
|
||||
|
||||
#endif
|
||||
65
include/boost/thread/barrier.hpp
Normal file
65
include/boost/thread/barrier.hpp
Normal file
@@ -0,0 +1,65 @@
|
||||
// Copyright (C) 2002-2003
|
||||
// David Moore, William E. Kempf
|
||||
// Copyright (C) 2007-8 Anthony Williams
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_BARRIER_JDM030602_HPP
|
||||
#define BOOST_BARRIER_JDM030602_HPP
|
||||
|
||||
#include <boost/thread/detail/config.hpp>
|
||||
#include <boost/throw_exception.hpp>
|
||||
|
||||
#include <boost/thread/mutex.hpp>
|
||||
#include <boost/thread/lock_types.hpp>
|
||||
#include <boost/thread/condition_variable.hpp>
|
||||
#include <string>
|
||||
#include <stdexcept>
|
||||
|
||||
#include <boost/config/abi_prefix.hpp>
|
||||
|
||||
namespace boost
|
||||
{
|
||||
|
||||
class barrier
|
||||
{
|
||||
public:
|
||||
barrier(unsigned int count)
|
||||
: m_threshold(count), m_count(count), m_generation(0)
|
||||
{
|
||||
if (count == 0)
|
||||
boost::throw_exception(thread_exception(system::errc::invalid_argument, "barrier constructor: count cannot be zero."));
|
||||
}
|
||||
|
||||
bool wait()
|
||||
{
|
||||
boost::unique_lock<boost::mutex> lock(m_mutex);
|
||||
unsigned int gen = m_generation;
|
||||
|
||||
if (--m_count == 0)
|
||||
{
|
||||
m_generation++;
|
||||
m_count = m_threshold;
|
||||
m_cond.notify_all();
|
||||
return true;
|
||||
}
|
||||
|
||||
while (gen == m_generation)
|
||||
m_cond.wait(lock);
|
||||
return false;
|
||||
}
|
||||
|
||||
private:
|
||||
mutex m_mutex;
|
||||
condition_variable m_cond;
|
||||
unsigned int m_threshold;
|
||||
unsigned int m_count;
|
||||
unsigned int m_generation;
|
||||
};
|
||||
|
||||
} // namespace boost
|
||||
|
||||
#include <boost/config/abi_suffix.hpp>
|
||||
|
||||
#endif
|
||||
@@ -1,159 +1,21 @@
|
||||
// Copyright (C) 2001
|
||||
// William E. Kempf
|
||||
#ifndef BOOST_THREAD_CONDITION_HPP
|
||||
#define BOOST_THREAD_CONDITION_HPP
|
||||
// (C) Copyright 2007 Anthony Williams
|
||||
//
|
||||
// Permission to use, copy, modify, distribute and sell this software
|
||||
// and its documentation for any purpose is hereby granted without fee,
|
||||
// provided that the above copyright notice appear in all copies and
|
||||
// that both that copyright notice and this permission notice appear
|
||||
// in supporting documentation. William E. Kempf makes no representations
|
||||
// about the suitability of this software for any purpose.
|
||||
// It is provided "as is" without express or implied warranty.
|
||||
// Distributed under the Boost Software License, Version 1.0. (See
|
||||
// accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_CONDITION_WEK070601_HPP
|
||||
#define BOOST_CONDITION_WEK070601_HPP
|
||||
#include <boost/thread/detail/config.hpp>
|
||||
|
||||
#include <boost/config.hpp>
|
||||
#ifndef BOOST_HAS_THREADS
|
||||
# error Thread support is unavailable!
|
||||
#endif
|
||||
#if defined BOOST_THREAD_PROVIDES_CONDITION
|
||||
|
||||
#include <boost/thread/exceptions.hpp>
|
||||
#include <boost/utility.hpp>
|
||||
#include <boost/thread/detail/lock.hpp>
|
||||
#include <boost/thread/condition_variable.hpp>
|
||||
|
||||
#if defined(BOOST_HAS_PTHREADS)
|
||||
# include <pthread.h>
|
||||
#endif
|
||||
|
||||
namespace boost {
|
||||
|
||||
struct xtime;
|
||||
|
||||
class condition : private noncopyable
|
||||
namespace boost
|
||||
{
|
||||
public:
|
||||
condition();
|
||||
~condition();
|
||||
typedef condition_variable_any condition;
|
||||
}
|
||||
|
||||
void notify_one();
|
||||
void notify_all();
|
||||
|
||||
template <typename L>
|
||||
void wait(L& lock)
|
||||
{
|
||||
if (!lock)
|
||||
throw lock_error();
|
||||
|
||||
do_wait(lock.m_mutex);
|
||||
}
|
||||
|
||||
template <typename L, typename Pr>
|
||||
void wait(L& lock, Pr pred)
|
||||
{
|
||||
if (!lock)
|
||||
throw lock_error();
|
||||
|
||||
while (!pred())
|
||||
do_wait(lock.m_mutex);
|
||||
}
|
||||
|
||||
template <typename L>
|
||||
bool timed_wait(L& lock, const xtime& xt)
|
||||
{
|
||||
if (!lock)
|
||||
throw lock_error();
|
||||
|
||||
return do_timed_wait(lock.m_mutex, xt);
|
||||
}
|
||||
|
||||
template <typename L, typename Pr>
|
||||
bool timed_wait(L& lock, const xtime& xt, Pr pred)
|
||||
{
|
||||
if (!lock)
|
||||
throw lock_error();
|
||||
|
||||
while (!pred())
|
||||
{
|
||||
if (!do_timed_wait(lock.m_mutex, xt))
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private:
|
||||
template <typename M>
|
||||
void do_wait(M& mutex)
|
||||
{
|
||||
#if defined(BOOST_HAS_WINTHREADS)
|
||||
enter_wait();
|
||||
#endif
|
||||
|
||||
typedef typename detail::thread::lock_ops<M> lock_ops;
|
||||
lock_ops::lock_state state;
|
||||
lock_ops::unlock(mutex, state);
|
||||
|
||||
#if defined(BOOST_HAS_PTHREADS)
|
||||
do_wait(state.pmutex);
|
||||
#elif defined(BOOST_HAS_WINTHREADS)
|
||||
do_wait();
|
||||
#endif
|
||||
|
||||
lock_ops::lock(mutex, state);
|
||||
}
|
||||
|
||||
template <typename M>
|
||||
bool do_timed_wait(M& mutex, const xtime& xt)
|
||||
{
|
||||
#if defined(BOOST_HAS_WINTHREADS)
|
||||
enter_wait();
|
||||
#endif
|
||||
|
||||
typedef typename detail::thread::lock_ops<M> lock_ops;
|
||||
lock_ops::lock_state state;
|
||||
lock_ops::unlock(mutex, state);
|
||||
|
||||
bool ret = false;
|
||||
|
||||
#if defined(BOOST_HAS_PTHREADS)
|
||||
ret = do_timed_wait(xt, state.pmutex);
|
||||
#elif defined(BOOST_HAS_WINTHREADS)
|
||||
ret = do_timed_wait(xt);
|
||||
#endif
|
||||
|
||||
lock_ops::lock(mutex, state);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
#if defined(BOOST_HAS_WINTHREADS)
|
||||
void enter_wait();
|
||||
void do_wait();
|
||||
bool do_timed_wait(const xtime& xt);
|
||||
#elif defined(BOOST_HAS_PTHREADS)
|
||||
void do_wait(pthread_mutex_t* pmutex);
|
||||
bool do_timed_wait(const xtime& xt, pthread_mutex_t* pmutex);
|
||||
#endif
|
||||
|
||||
#if defined(BOOST_HAS_WINTHREADS)
|
||||
void* m_gate;
|
||||
void* m_queue;
|
||||
void* m_mutex;
|
||||
unsigned m_gone; // # threads that timed out and never made it to the m_queue
|
||||
unsigned long m_blocked; // # threads m_blocked m_waiting for the condition
|
||||
unsigned m_waiting; // # threads m_waiting no longer m_waiting for the condition but still
|
||||
// m_waiting to be removed from the m_queue
|
||||
#elif defined(BOOST_HAS_PTHREADS)
|
||||
pthread_cond_t m_condition;
|
||||
#endif
|
||||
};
|
||||
|
||||
} // namespace boost
|
||||
|
||||
// Change Log:
|
||||
// 8 Feb 01 WEKEMPF Initial version.
|
||||
// 22 May 01 WEKEMPF Modified to use xtime for time outs.
|
||||
// 23 May 01 WEKEMPF Removed "duration" timed_waits, as they are too difficult
|
||||
// to use with spurious wakeups.
|
||||
|
||||
#endif // BOOST_CONDITION_WEK070601_HPP
|
||||
|
||||
21
include/boost/thread/condition_variable.hpp
Normal file
21
include/boost/thread/condition_variable.hpp
Normal file
@@ -0,0 +1,21 @@
|
||||
#ifndef BOOST_THREAD_CONDITION_VARIABLE_HPP
|
||||
#define BOOST_THREAD_CONDITION_VARIABLE_HPP
|
||||
|
||||
// condition_variable.hpp
|
||||
//
|
||||
// (C) Copyright 2007 Anthony Williams
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See
|
||||
// accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#include <boost/thread/detail/platform.hpp>
|
||||
#if defined(BOOST_THREAD_PLATFORM_WIN32)
|
||||
#include <boost/thread/win32/condition_variable.hpp>
|
||||
#elif defined(BOOST_THREAD_PLATFORM_PTHREAD)
|
||||
#include <boost/thread/pthread/condition_variable.hpp>
|
||||
#else
|
||||
#error "Boost threads unavailable on this platform"
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -1,100 +0,0 @@
|
||||
// Copyright (C) 2001
|
||||
// William E. Kempf
|
||||
//
|
||||
// Permission to use, copy, modify, distribute and sell this software
|
||||
// and its documentation for any purpose is hereby granted without fee,
|
||||
// provided that the above copyright notice appear in all copies and
|
||||
// that both that copyright notice and this permission notice appear
|
||||
// in supporting documentation. William E. Kempf makes no representations
|
||||
// about the suitability of this software for any purpose.
|
||||
// It is provided "as is" without express or implied warranty.
|
||||
|
||||
// This file is used to configure Boost.Threads during development
|
||||
// in order to decouple dependency on any Boost release. Once
|
||||
// accepted into Boost these contents will be moved to <boost/config>
|
||||
// or some other appropriate build configuration and all
|
||||
// #include <boost/thread/config.hpp> statements will be changed
|
||||
// accordingly.
|
||||
|
||||
#ifndef BOOST_THREAD_CONFIG_WEK070601_HPP
|
||||
#define BOOST_THREAD_CONFIG_WEK070601_HPP
|
||||
|
||||
#include <boost/config.hpp>
|
||||
|
||||
#error "Included <boost/thread/config.hpp>"
|
||||
|
||||
/*// Define if threading support is enabled for the toolset.
|
||||
#undef BOOST_HAS_THREADS
|
||||
|
||||
// Define if threading should be implemented in terms of Win32 threads.
|
||||
#undef BOOST_HAS_WINTHREADS
|
||||
|
||||
// Define if threading should be implemented in terms of POSIX threads.
|
||||
#undef BOOST_HAS_PTHREADS
|
||||
|
||||
// Define if BOOST_HAS_PTHREADS and pthread_delay_np() exists.
|
||||
#undef BOOST_HAS_PTHREAD_DELAY_NP
|
||||
|
||||
// Define if BOOST_HAS_PTHREADS and not BOOST_HAS_PTHREAD_DELAY_NP
|
||||
// but nanosleep can be used instead.
|
||||
#undef BOOST_HAS_NANOSLEEP
|
||||
|
||||
// Define if BOOST_HAS_PTHREADS and pthread_yield() exists.
|
||||
#undef BOOST_HAS_PTHREAD_YIELD
|
||||
|
||||
// Define if BOOST_HAS_PTHREADS and not BOOST_HAS_PTHREAD_YIELD and
|
||||
// sched_yield() exists.
|
||||
#undef BOOST_HAS_SCHED_YIELD
|
||||
|
||||
// Define if gettimeofday() exists.
|
||||
#undef BOOST_HAS_GETTIMEOFDAY
|
||||
|
||||
// Define if not BOOST_HAS_GETTIMEOFDAY and clock_gettime() exists.
|
||||
#undef BOOST_HAS_CLOCK_GETTIME
|
||||
|
||||
// Define if not BOOST_HAS_GETTIMEOFDAY and not BOOST_HAS_CLOCK_GETTIME and
|
||||
// GetSystemTimeAsFileTime() can be called with an FTIME structure.
|
||||
#undef BOOST_HAS_FTIME
|
||||
|
||||
// Define if pthread_mutexattr_settype and pthread_mutexattr_gettype exist.
|
||||
#undef BOOST_HAS_PTHREAD_MUTEXATTR_SETTYPE
|
||||
|
||||
// Here we'll set up known compiler options.
|
||||
|
||||
#if defined(BOOST_MSVC)
|
||||
# if defined(_MT)
|
||||
# define BOOST_HAS_THREADS
|
||||
# endif
|
||||
# define BOOST_HAS_WINTHREADS // comment out this to test pthreads-win32.
|
||||
# if !defined(BOOST_HAS_WINTHREADS)
|
||||
# define BOOST_HAS_PTHREADS
|
||||
# define BOOST_HAS_PTHREAD_MUTEXATTR_SETTYPE
|
||||
# define PtW32NoCatchWarn
|
||||
# pragma comment(lib, "pthreadVCE.lib")
|
||||
# endif
|
||||
# define BOOST_HAS_FTIME
|
||||
// pdm: this is for linux - is there a better #define to #if on?
|
||||
// wek: not sure how else to do this, but GNU CC on Win32 should probably
|
||||
// use BOOST_HAS_WINTHREADS, and I expect there will be other
|
||||
// platform specific variations for this compiler toolset. Need
|
||||
// to decide how to handle this.
|
||||
#elif defined( __GNUC__ )
|
||||
# define BOOST_HAS_THREADS
|
||||
# define BOOST_HAS_PTHREADS
|
||||
# define BOOST_HAS_NANOSLEEP
|
||||
# define BOOST_HAS_GETTIMEOFDAY
|
||||
// pdm: From the pthread.h header, one of these macros
|
||||
// must be defined for this stuff to exist.
|
||||
// wek: This seems like a harmless enough method to determine these
|
||||
// switches, but one should note that some implementations may not
|
||||
// use these. Notably, pthreads-win32 doesn't define either
|
||||
// __USE_UNIX98 or __USE_GNU.
|
||||
# if defined( __USE_UNIX98 )
|
||||
# define BOOST_HAS_PTHREAD_MUTEXATTR_SETTYPE
|
||||
# elif defined( __USE_GNU )
|
||||
# define BOOST_HAS_PTHREAD_MUTEXATTR_SETTYPE
|
||||
# define BOOST_HAS_PTHREAD_YIELD
|
||||
# endif
|
||||
#endif*/
|
||||
|
||||
#endif // BOOST_THREAD_CONFIG_WEK070601_HPP
|
||||
26
include/boost/thread/cv_status.hpp
Normal file
26
include/boost/thread/cv_status.hpp
Normal file
@@ -0,0 +1,26 @@
|
||||
// cv_status.hpp
|
||||
//
|
||||
// Copyright (C) 2011 Vicente J. Botet Escriba
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See
|
||||
// accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_THREAD_CV_STATUS_HPP
|
||||
#define BOOST_THREAD_CV_STATUS_HPP
|
||||
|
||||
#include <boost/detail/scoped_enum_emulation.hpp>
|
||||
|
||||
namespace boost
|
||||
{
|
||||
|
||||
// enum class cv_status;
|
||||
BOOST_SCOPED_ENUM_DECLARE_BEGIN(cv_status)
|
||||
{
|
||||
no_timeout,
|
||||
timeout
|
||||
}
|
||||
BOOST_SCOPED_ENUM_DECLARE_END(cv_status)
|
||||
}
|
||||
|
||||
#endif // header
|
||||
101
include/boost/thread/detail/async_func.hpp
Normal file
101
include/boost/thread/detail/async_func.hpp
Normal file
@@ -0,0 +1,101 @@
|
||||
// Copyright (C) 2012 Vicente J. Botet Escriba
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
// The async_func code is based on the one from libcxx.
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#ifndef BOOST_THREAD_DETAIL_ASYNC_FUNCT_HPP
|
||||
#define BOOST_THREAD_DETAIL_ASYNC_FUNCT_HPP
|
||||
|
||||
#include <boost/config.hpp>
|
||||
|
||||
#include <boost/utility/result_of.hpp>
|
||||
#include <boost/thread/detail/move.hpp>
|
||||
#include <boost/thread/detail/invoke.hpp>
|
||||
#include <boost/thread/detail/make_tuple_indices.hpp>
|
||||
|
||||
#if ! defined(BOOST_NO_CXX11_HDR_TUPLE)
|
||||
#include <tuple>
|
||||
#endif
|
||||
|
||||
namespace boost
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
#if ! defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) && \
|
||||
! defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && \
|
||||
! defined(BOOST_NO_CXX11_HDR_TUPLE)
|
||||
|
||||
template <class Fp, class... Args>
|
||||
class async_func
|
||||
{
|
||||
std::tuple<Fp, Args...> f_;
|
||||
|
||||
public:
|
||||
//typedef typename invoke_of<_Fp, _Args...>::type Rp;
|
||||
typedef typename result_of<Fp(Args...)>::type result_type;
|
||||
|
||||
BOOST_SYMBOL_VISIBLE
|
||||
explicit async_func(Fp&& f, Args&&... args)
|
||||
: f_(boost::move(f), boost::move(args)...) {}
|
||||
|
||||
BOOST_SYMBOL_VISIBLE
|
||||
async_func(async_func&& f) : f_(boost::move(f.f_)) {}
|
||||
|
||||
result_type operator()()
|
||||
{
|
||||
typedef typename make_tuple_indices<1+sizeof...(Args), 1>::type Index;
|
||||
return execute(Index());
|
||||
}
|
||||
private:
|
||||
template <size_t ...Indices>
|
||||
result_type
|
||||
execute(tuple_indices<Indices...>)
|
||||
{
|
||||
return invoke(boost::move(std::get<0>(f_)), boost::move(std::get<Indices>(f_))...);
|
||||
}
|
||||
};
|
||||
#else
|
||||
template <class Fp>
|
||||
class async_func
|
||||
{
|
||||
Fp f_;
|
||||
|
||||
public:
|
||||
BOOST_THREAD_COPYABLE_AND_MOVABLE(async_func)
|
||||
|
||||
typedef typename result_of<Fp()>::type result_type;
|
||||
|
||||
BOOST_SYMBOL_VISIBLE
|
||||
explicit async_func(BOOST_THREAD_FWD_REF(Fp) f)
|
||||
: f_(boost::move(f)) {}
|
||||
|
||||
BOOST_SYMBOL_VISIBLE
|
||||
async_func(BOOST_THREAD_FWD_REF(async_func) f) : f_(boost::move(f.f_)) {}
|
||||
|
||||
result_type operator()()
|
||||
{
|
||||
return execute();
|
||||
}
|
||||
private:
|
||||
result_type
|
||||
execute()
|
||||
{
|
||||
return f_();
|
||||
}
|
||||
};
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
#endif // header
|
||||
390
include/boost/thread/detail/config.hpp
Normal file
390
include/boost/thread/detail/config.hpp
Normal file
@@ -0,0 +1,390 @@
|
||||
// Copyright (C) 2001-2003
|
||||
// William E. Kempf
|
||||
// Copyright (C) 2011-2012 Vicente J. Botet Escriba
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_THREAD_CONFIG_WEK01032003_HPP
|
||||
#define BOOST_THREAD_CONFIG_WEK01032003_HPP
|
||||
|
||||
// Force SIG_ATOMIC_MAX to be defined
|
||||
//#ifndef __STDC_LIMIT_MACROS
|
||||
//#define __STDC_LIMIT_MACROS
|
||||
//#endif
|
||||
|
||||
#include <boost/config.hpp>
|
||||
#include <boost/detail/workaround.hpp>
|
||||
#include <boost/thread/detail/platform.hpp>
|
||||
|
||||
#if ! defined BOOST_THREAD_NOEXCEPT_OR_THROW
|
||||
#ifdef BOOST_NO_CXX11_NOEXCEPT
|
||||
# define BOOST_THREAD_NOEXCEPT_OR_THROW throw()
|
||||
#else
|
||||
# define BOOST_THREAD_NOEXCEPT_OR_THROW noexcept
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if defined BOOST_THREAD_THROW_IF_PRECONDITION_NOT_SATISFIED
|
||||
#define BOOST_THREAD_ASSERT_PRECONDITION(EXPR, EX) \
|
||||
if (EXPR) {} else boost::throw_exception(EX)
|
||||
#define BOOST_THREAD_VERIFY_PRECONDITION(EXPR, EX) \
|
||||
if (EXPR) {} else boost::throw_exception(EX)
|
||||
#define BOOST_THREAD_THROW_ELSE_RETURN(EX, RET) \
|
||||
boost::throw_exception(EX)
|
||||
#else
|
||||
#define BOOST_THREAD_ASSERT_PRECONDITION(EXPR, EX)
|
||||
#define BOOST_THREAD_VERIFY_PRECONDITION(EXPR, EX) \
|
||||
(void)(EXPR)
|
||||
#define BOOST_THREAD_THROW_ELSE_RETURN(EX, RET) \
|
||||
return (RET)
|
||||
#endif
|
||||
|
||||
// This compiler doesn't support Boost.Chrono
|
||||
#if defined __IBMCPP__ && (__IBMCPP__ < 1100) \
|
||||
&& ! defined BOOST_THREAD_DONT_USE_CHRONO
|
||||
#define BOOST_THREAD_DONT_USE_CHRONO
|
||||
#if ! defined BOOST_THREAD_USE_DATE
|
||||
#define BOOST_THREAD_USE_DATE
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// This compiler doesn't support Boost.Move
|
||||
#if BOOST_WORKAROUND(__SUNPRO_CC, < 0x5100) \
|
||||
&& ! defined BOOST_THREAD_DONT_USE_MOVE
|
||||
#define BOOST_THREAD_DONT_USE_MOVE
|
||||
#endif
|
||||
|
||||
// This compiler doesn't support Boost.Container Allocators files
|
||||
#if defined __SUNPRO_CC \
|
||||
&& ! defined BOOST_THREAD_DONT_PROVIDE_FUTURE_CTOR_ALLOCATORS
|
||||
#define BOOST_THREAD_DONT_PROVIDE_FUTURE_CTOR_ALLOCATORS
|
||||
#endif
|
||||
|
||||
#if defined _WIN32_WCE && _WIN32_WCE==0x501 \
|
||||
&& ! defined BOOST_THREAD_DONT_PROVIDE_FUTURE_CTOR_ALLOCATORS
|
||||
#define BOOST_THREAD_DONT_PROVIDE_FUTURE_CTOR_ALLOCATORS
|
||||
#endif
|
||||
|
||||
|
||||
#if defined BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX || defined BOOST_NO_CXX11_HDR_INITIALIZER_LIST
|
||||
#define BOOST_THREAD_NO_CXX11_HDR_INITIALIZER_LIST
|
||||
#define BOOST_THREAD_NO_MAKE_LOCK_GUARD
|
||||
#define BOOST_THREAD_NO_MAKE_STRICT_LOCK
|
||||
#define BOOST_THREAD_NO_MAKE_NESTED_STRICT_LOCK
|
||||
#endif
|
||||
|
||||
#if defined(BOOST_NO_CXX11_HDR_TUPLE) || defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
|
||||
#define BOOST_THREAD_NO_MAKE_UNIQUE_LOCKS
|
||||
#elif defined _MSC_VER && _MSC_VER <= 1600
|
||||
// C++ features supported by VC++ 10 (aka 2010)
|
||||
#define BOOST_THREAD_NO_MAKE_UNIQUE_LOCKS
|
||||
#endif
|
||||
|
||||
/// BASIC_THREAD_ID
|
||||
// todo to be removed for 1.54
|
||||
#if ! defined BOOST_THREAD_DONT_PROVIDE_BASIC_THREAD_ID \
|
||||
&& ! defined BOOST_THREAD_PROVIDES_BASIC_THREAD_ID
|
||||
#define BOOST_THREAD_PROVIDES_BASIC_THREAD_ID
|
||||
#endif
|
||||
|
||||
/// RVALUE_REFERENCES_DONT_MATCH_FUNTION_PTR
|
||||
#if defined BOOST_NO_CXX11_RVALUE_REFERENCES || defined BOOST_MSVC
|
||||
#define BOOST_THREAD_RVALUE_REFERENCES_DONT_MATCH_FUNTION_PTR
|
||||
#endif
|
||||
|
||||
// Default version
|
||||
#if !defined BOOST_THREAD_VERSION
|
||||
#define BOOST_THREAD_VERSION 2
|
||||
#else
|
||||
#if BOOST_THREAD_VERSION!=2 && BOOST_THREAD_VERSION!=3 && BOOST_THREAD_VERSION!=4
|
||||
#error "BOOST_THREAD_VERSION must be 2, 3 or 4"
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// CHRONO
|
||||
// Uses Boost.Chrono by default if not stated the opposite defining BOOST_THREAD_DONT_USE_CHRONO
|
||||
#if ! defined BOOST_THREAD_DONT_USE_CHRONO \
|
||||
&& ! defined BOOST_THREAD_USES_CHRONO
|
||||
#define BOOST_THREAD_USES_CHRONO
|
||||
#endif
|
||||
|
||||
#if BOOST_THREAD_VERSION==2
|
||||
|
||||
// PROVIDE_PROMISE_LAZY
|
||||
#if ! defined BOOST_THREAD_DONT_PROVIDE_PROMISE_LAZY \
|
||||
&& ! defined BOOST_THREAD_PROVIDES_PROMISE_LAZY
|
||||
#define BOOST_THREAD_PROVIDES_PROMISE_LAZY
|
||||
#endif
|
||||
|
||||
// PROVIDE_THREAD_EQ
|
||||
#if ! defined BOOST_THREAD_DONT_PROVIDE_THREAD_EQ \
|
||||
&& ! defined BOOST_THREAD_PROVIDES_THREAD_EQ
|
||||
#define BOOST_THREAD_PROVIDES_THREAD_EQ
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
#if BOOST_THREAD_VERSION>=3
|
||||
|
||||
// ONCE_CXX11
|
||||
// fixme BOOST_THREAD_PROVIDES_ONCE_CXX11 doesn't works when thread.cpp is compiled BOOST_THREAD_VERSION 3
|
||||
#if ! defined BOOST_THREAD_DONT_PROVIDE_ONCE_CXX11 \
|
||||
&& ! defined BOOST_THREAD_PROVIDES_ONCE_CXX11
|
||||
#define BOOST_THREAD_DONT_PROVIDE_ONCE_CXX11
|
||||
#endif
|
||||
|
||||
// THREAD_DESTRUCTOR_CALLS_TERMINATE_IF_JOINABLE
|
||||
#if ! defined BOOST_THREAD_DONT_PROVIDE_THREAD_DESTRUCTOR_CALLS_TERMINATE_IF_JOINABLE \
|
||||
&& ! defined BOOST_THREAD_PROVIDES_THREAD_DESTRUCTOR_CALLS_TERMINATE_IF_JOINABLE
|
||||
#define BOOST_THREAD_PROVIDES_THREAD_DESTRUCTOR_CALLS_TERMINATE_IF_JOINABLE
|
||||
#endif
|
||||
|
||||
// THREAD_MOVE_ASSIGN_CALLS_TERMINATE_IF_JOINABLE
|
||||
#if ! defined BOOST_THREAD_DONT_PROVIDE_THREAD_MOVE_ASSIGN_CALLS_TERMINATE_IF_JOINABLE \
|
||||
&& ! defined BOOST_THREAD_PROVIDES_THREAD_MOVE_ASSIGN_CALLS_TERMINATE_IF_JOINABLE
|
||||
#define BOOST_THREAD_PROVIDES_THREAD_MOVE_ASSIGN_CALLS_TERMINATE_IF_JOINABLE
|
||||
#endif
|
||||
|
||||
// PROVIDE_FUTURE
|
||||
#if ! defined BOOST_THREAD_DONT_PROVIDE_FUTURE \
|
||||
&& ! defined BOOST_THREAD_PROVIDES_FUTURE
|
||||
#define BOOST_THREAD_PROVIDES_FUTURE
|
||||
#endif
|
||||
|
||||
// FUTURE_CTOR_ALLOCATORS
|
||||
#if ! defined BOOST_THREAD_DONT_PROVIDE_FUTURE_CTOR_ALLOCATORS \
|
||||
&& ! defined BOOST_THREAD_PROVIDES_FUTURE_CTOR_ALLOCATORS
|
||||
#define BOOST_THREAD_PROVIDES_FUTURE_CTOR_ALLOCATORS
|
||||
#endif
|
||||
|
||||
// SHARED_MUTEX_UPWARDS_CONVERSIONS
|
||||
#if ! defined BOOST_THREAD_DONT_PROVIDE_SHARED_MUTEX_UPWARDS_CONVERSIONS \
|
||||
&& ! defined BOOST_THREAD_PROVIDES_SHARED_MUTEX_UPWARDS_CONVERSIONS
|
||||
#define BOOST_THREAD_PROVIDES_SHARED_MUTEX_UPWARDS_CONVERSIONS
|
||||
#endif
|
||||
|
||||
// PROVIDE_EXPLICIT_LOCK_CONVERSION
|
||||
#if ! defined BOOST_THREAD_DONT_PROVIDE_EXPLICIT_LOCK_CONVERSION \
|
||||
&& ! defined BOOST_THREAD_PROVIDES_EXPLICIT_LOCK_CONVERSION
|
||||
#define BOOST_THREAD_PROVIDES_EXPLICIT_LOCK_CONVERSION
|
||||
#endif
|
||||
|
||||
// GENERIC_SHARED_MUTEX_ON_WIN
|
||||
#if ! defined BOOST_THREAD_DONT_PROVIDE_GENERIC_SHARED_MUTEX_ON_WIN \
|
||||
&& ! defined BOOST_THREAD_PROVIDES_GENERIC_SHARED_MUTEX_ON_WIN
|
||||
#define BOOST_THREAD_PROVIDES_GENERIC_SHARED_MUTEX_ON_WIN
|
||||
#endif
|
||||
|
||||
// USE_MOVE
|
||||
#if ! defined BOOST_THREAD_DONT_USE_MOVE \
|
||||
&& ! defined BOOST_THREAD_USES_MOVE
|
||||
#define BOOST_THREAD_USES_MOVE
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
// deprecated since version 4
|
||||
#if BOOST_THREAD_VERSION < 4
|
||||
|
||||
// NESTED_LOCKS
|
||||
#if ! defined BOOST_THREAD_PROVIDES_NESTED_LOCKS \
|
||||
&& ! defined BOOST_THREAD_DONT_PROVIDE_NESTED_LOCKS
|
||||
#define BOOST_THREAD_PROVIDES_NESTED_LOCKS
|
||||
#endif
|
||||
|
||||
// CONDITION
|
||||
#if ! defined BOOST_THREAD_PROVIDES_CONDITION \
|
||||
&& ! defined BOOST_THREAD_DONT_PROVIDE_CONDITION
|
||||
#define BOOST_THREAD_PROVIDES_CONDITION
|
||||
#endif
|
||||
|
||||
// USE_DATETIME
|
||||
#if ! defined BOOST_THREAD_DONT_USE_DATETIME \
|
||||
&& ! defined BOOST_THREAD_USES_DATETIME
|
||||
#define BOOST_THREAD_USES_DATETIME
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if BOOST_THREAD_VERSION>=4
|
||||
|
||||
// SIGNATURE_PACKAGED_TASK
|
||||
#if ! defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK \
|
||||
&& ! defined BOOST_THREAD_DONT_PROVIDE_SIGNATURE_PACKAGED_TASK
|
||||
#define BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK
|
||||
#endif
|
||||
|
||||
// VARIADIC_THREAD
|
||||
#if ! defined BOOST_THREAD_PROVIDES_VARIADIC_THREAD \
|
||||
&& ! defined BOOST_THREAD_DONT_PROVIDE_VARIADIC_THREAD
|
||||
|
||||
#if ! defined(BOOST_NO_SFINAE_EXPR) && \
|
||||
! defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) && \
|
||||
! defined(BOOST_NO_CXX11_DECLTYPE) && \
|
||||
! defined(BOOST_NO_CXX11_DECLTYPE_N3276) && \
|
||||
! defined(BOOST_NO_CXX11_AUTO) && \
|
||||
! defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && \
|
||||
! defined(BOOST_NO_CXX11_HDR_TUPLE)
|
||||
|
||||
#define BOOST_THREAD_PROVIDES_VARIADIC_THREAD
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// FUTURE_CONTINUATION
|
||||
#if ! defined BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION \
|
||||
&& ! defined BOOST_THREAD_DONT_PROVIDE_FUTURE_CONTINUATION
|
||||
#define BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION
|
||||
#endif
|
||||
|
||||
// FUTURE_INVALID_AFTER_GET
|
||||
#if ! defined BOOST_THREAD_PROVIDES_FUTURE_INVALID_AFTER_GET \
|
||||
&& ! defined BOOST_THREAD_DONT_PROVIDE_FUTURE_INVALID_AFTER_GET
|
||||
#define BOOST_THREAD_PROVIDES_FUTURE_INVALID_AFTER_GET
|
||||
#endif
|
||||
|
||||
// NESTED_LOCKS
|
||||
#if ! defined BOOST_THREAD_PROVIDES_NESTED_LOCKS \
|
||||
&& ! defined BOOST_THREAD_DONT_PROVIDE_NESTED_LOCKS
|
||||
#define BOOST_THREAD_DONT_PROVIDE_NESTED_LOCKS
|
||||
#endif
|
||||
|
||||
// CONDITION
|
||||
#if ! defined BOOST_THREAD_PROVIDES_CONDITION \
|
||||
&& ! defined BOOST_THREAD_DONT_PROVIDE_CONDITION
|
||||
#define BOOST_THREAD_DONT_PROVIDE_CONDITION
|
||||
#endif
|
||||
|
||||
#endif // BOOST_THREAD_VERSION>=4
|
||||
|
||||
// INTERRUPTIONS
|
||||
#if ! defined BOOST_THREAD_PROVIDES_INTERRUPTIONS \
|
||||
&& ! defined BOOST_THREAD_DONT_PROVIDE_INTERRUPTIONS
|
||||
#define BOOST_THREAD_PROVIDES_INTERRUPTIONS
|
||||
#endif
|
||||
|
||||
// CORRELATIONS
|
||||
|
||||
// EXPLICIT_LOCK_CONVERSION.
|
||||
#if defined BOOST_THREAD_PROVIDES_EXPLICIT_LOCK_CONVERSION
|
||||
#define BOOST_THREAD_EXPLICIT_LOCK_CONVERSION explicit
|
||||
#else
|
||||
#define BOOST_THREAD_EXPLICIT_LOCK_CONVERSION
|
||||
#endif
|
||||
|
||||
// BOOST_THREAD_PROVIDES_GENERIC_SHARED_MUTEX_ON_WIN is defined if BOOST_THREAD_PROVIDES_SHARED_MUTEX_UPWARDS_CONVERSIONS
|
||||
#if defined BOOST_THREAD_PROVIDES_SHARED_MUTEX_UPWARDS_CONVERSIONS \
|
||||
&& ! defined BOOST_THREAD_PROVIDES_GENERIC_SHARED_MUTEX_ON_WIN
|
||||
#define BOOST_THREAD_PROVIDES_GENERIC_SHARED_MUTEX_ON_WIN
|
||||
#endif
|
||||
|
||||
// BOOST_THREAD_PROVIDES_DEPRECATED_FEATURES_SINCE_V3_0_0 defined by default up to Boost 1.52
|
||||
// BOOST_THREAD_DONT_PROVIDE_DEPRECATED_FEATURES_SINCE_V3_0_0 defined by default up to Boost 1.55
|
||||
#if defined BOOST_THREAD_PROVIDES_DEPRECATED_FEATURES_SINCE_V3_0_0
|
||||
|
||||
#if ! defined BOOST_THREAD_PROVIDES_THREAD_EQ
|
||||
#define BOOST_THREAD_PROVIDES_THREAD_EQ
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
#if BOOST_WORKAROUND(__BORLANDC__, < 0x600)
|
||||
# pragma warn -8008 // Condition always true/false
|
||||
# pragma warn -8080 // Identifier declared but never used
|
||||
# pragma warn -8057 // Parameter never used
|
||||
# pragma warn -8066 // Unreachable code
|
||||
#endif
|
||||
|
||||
#include <boost/thread/detail/platform.hpp>
|
||||
|
||||
#if defined(BOOST_THREAD_PLATFORM_WIN32)
|
||||
#else
|
||||
# if defined(BOOST_HAS_PTHREAD_DELAY_NP) || defined(BOOST_HAS_NANOSLEEP)
|
||||
# define BOOST_THREAD_SLEEP_FOR_IS_STEADY
|
||||
# endif
|
||||
#endif
|
||||
|
||||
// provided for backwards compatibility, since this
|
||||
// macro was used for several releases by mistake.
|
||||
#if defined(BOOST_THREAD_DYN_DLL) && ! defined BOOST_THREAD_DYN_LINK
|
||||
# define BOOST_THREAD_DYN_LINK
|
||||
#endif
|
||||
|
||||
// compatibility with the rest of Boost's auto-linking code:
|
||||
#if defined(BOOST_THREAD_DYN_LINK) || defined(BOOST_ALL_DYN_LINK)
|
||||
# undef BOOST_THREAD_USE_LIB
|
||||
# define BOOST_THREAD_USE_DLL
|
||||
#endif
|
||||
|
||||
#if defined(BOOST_THREAD_BUILD_DLL) //Build dll
|
||||
#elif defined(BOOST_THREAD_BUILD_LIB) //Build lib
|
||||
#elif defined(BOOST_THREAD_USE_DLL) //Use dll
|
||||
#elif defined(BOOST_THREAD_USE_LIB) //Use lib
|
||||
#else //Use default
|
||||
# if defined(BOOST_THREAD_PLATFORM_WIN32)
|
||||
# if defined(BOOST_MSVC) || defined(BOOST_INTEL_WIN)
|
||||
//For compilers supporting auto-tss cleanup
|
||||
//with Boost.Threads lib, use Boost.Threads lib
|
||||
# define BOOST_THREAD_USE_LIB
|
||||
# else
|
||||
//For compilers not yet supporting auto-tss cleanup
|
||||
//with Boost.Threads lib, use Boost.Threads dll
|
||||
# define BOOST_THREAD_USE_DLL
|
||||
# endif
|
||||
# else
|
||||
# define BOOST_THREAD_USE_LIB
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#if defined(BOOST_HAS_DECLSPEC)
|
||||
# if defined(BOOST_THREAD_BUILD_DLL) //Build dll
|
||||
# define BOOST_THREAD_DECL BOOST_SYMBOL_EXPORT
|
||||
//# define BOOST_THREAD_DECL __declspec(dllexport)
|
||||
|
||||
# elif defined(BOOST_THREAD_USE_DLL) //Use dll
|
||||
# define BOOST_THREAD_DECL BOOST_SYMBOL_IMPORT
|
||||
//# define BOOST_THREAD_DECL __declspec(dllimport)
|
||||
# else
|
||||
# define BOOST_THREAD_DECL
|
||||
# endif
|
||||
#elif (__GNUC__ == 4 && __GNUC_MINOR__ >= 1) || (__GNUC__ > 4)
|
||||
# define BOOST_THREAD_DECL BOOST_SYMBOL_VISIBLE
|
||||
|
||||
#else
|
||||
# define BOOST_THREAD_DECL
|
||||
#endif // BOOST_HAS_DECLSPEC
|
||||
|
||||
//
|
||||
// Automatically link to the correct build variant where possible.
|
||||
//
|
||||
#if !defined(BOOST_ALL_NO_LIB) && !defined(BOOST_THREAD_NO_LIB) && !defined(BOOST_THREAD_BUILD_DLL) && !defined(BOOST_THREAD_BUILD_LIB)
|
||||
//
|
||||
// Tell the autolink to link dynamically, this will get undef'ed by auto_link.hpp
|
||||
// once it's done with it:
|
||||
//
|
||||
#if defined(BOOST_THREAD_USE_DLL)
|
||||
# define BOOST_DYN_LINK
|
||||
#endif
|
||||
//
|
||||
// Set the name of our library, this will get undef'ed by auto_link.hpp
|
||||
// once it's done with it:
|
||||
//
|
||||
#if defined(BOOST_THREAD_LIB_NAME)
|
||||
# define BOOST_LIB_NAME BOOST_THREAD_LIB_NAME
|
||||
#else
|
||||
# define BOOST_LIB_NAME boost_thread
|
||||
#endif
|
||||
//
|
||||
// If we're importing code from a dll, then tell auto_link.hpp about it:
|
||||
//
|
||||
// And include the header that does the work:
|
||||
//
|
||||
#include <boost/config/auto_link.hpp>
|
||||
#endif // auto-linking disabled
|
||||
|
||||
#endif // BOOST_THREAD_CONFIG_WEK1032003_HPP
|
||||
|
||||
// Change Log:
|
||||
// 22 Jan 05 Roland Schwarz (speedsnail)
|
||||
// Usage of BOOST_HAS_DECLSPEC macro.
|
||||
// Default again is static lib usage.
|
||||
// BOOST_DYN_LINK only defined when autolink included.
|
||||
45
include/boost/thread/detail/delete.hpp
Normal file
45
include/boost/thread/detail/delete.hpp
Normal file
@@ -0,0 +1,45 @@
|
||||
// Copyright (C) 2012 Vicente J. Botet Escriba
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_THREAD_DETAIL_DELETE_HPP
|
||||
#define BOOST_THREAD_DETAIL_DELETE_HPP
|
||||
|
||||
#include <boost/config.hpp>
|
||||
|
||||
/**
|
||||
* BOOST_THREAD_DELETE_COPY_CTOR deletes the copy constructor when the compiler supports it or
|
||||
* makes it private.
|
||||
*
|
||||
* BOOST_THREAD_DELETE_COPY_ASSIGN deletes the copy assignment when the compiler supports it or
|
||||
* makes it private.
|
||||
*/
|
||||
#ifndef BOOST_NO_CXX11_DELETED_FUNCTIONS
|
||||
#define BOOST_THREAD_DELETE_COPY_CTOR(CLASS) \
|
||||
CLASS(CLASS const&) = delete; \
|
||||
|
||||
#define BOOST_THREAD_DELETE_COPY_ASSIGN(CLASS) \
|
||||
CLASS& operator=(CLASS const&) = delete;
|
||||
|
||||
#else // BOOST_NO_CXX11_DELETED_FUNCTIONS
|
||||
#define BOOST_THREAD_DELETE_COPY_CTOR(CLASS) \
|
||||
private: \
|
||||
CLASS(CLASS&); \
|
||||
public:
|
||||
|
||||
#define BOOST_THREAD_DELETE_COPY_ASSIGN(CLASS) \
|
||||
private: \
|
||||
CLASS& operator=(CLASS&); \
|
||||
public:
|
||||
#endif // BOOST_NO_CXX11_DELETED_FUNCTIONS
|
||||
|
||||
/**
|
||||
* BOOST_THREAD_NO_COPYABLE deletes the copy constructor and assignment when the compiler supports it or
|
||||
* makes them private.
|
||||
*/
|
||||
#define BOOST_THREAD_NO_COPYABLE(CLASS) \
|
||||
BOOST_THREAD_DELETE_COPY_CTOR(CLASS) \
|
||||
BOOST_THREAD_DELETE_COPY_ASSIGN(CLASS)
|
||||
|
||||
#endif // BOOST_THREAD_DETAIL_DELETE_HPP
|
||||
39
include/boost/thread/detail/force_cast.hpp
Normal file
39
include/boost/thread/detail/force_cast.hpp
Normal file
@@ -0,0 +1,39 @@
|
||||
// Copyright (C) 2001-2003
|
||||
// Mac Murrett
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See
|
||||
// accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
// See http://www.boost.org for most recent version including documentation.
|
||||
|
||||
#ifndef BOOST_FORCE_CAST_MJM012402_HPP
|
||||
#define BOOST_FORCE_CAST_MJM012402_HPP
|
||||
|
||||
#include <boost/thread/detail/config.hpp>
|
||||
|
||||
namespace boost {
|
||||
namespace detail {
|
||||
namespace thread {
|
||||
|
||||
// force_cast will convert anything to anything.
|
||||
|
||||
// general case
|
||||
template<class Return_Type, class Argument_Type>
|
||||
inline Return_Type &force_cast(Argument_Type &rSrc)
|
||||
{
|
||||
return(*reinterpret_cast<Return_Type *>(&rSrc));
|
||||
}
|
||||
|
||||
// specialization for const
|
||||
template<class Return_Type, class Argument_Type>
|
||||
inline const Return_Type &force_cast(const Argument_Type &rSrc)
|
||||
{
|
||||
return(*reinterpret_cast<const Return_Type *>(&rSrc));
|
||||
}
|
||||
|
||||
} // namespace thread
|
||||
} // namespace detail
|
||||
} // namespace boost
|
||||
|
||||
#endif // BOOST_FORCE_CAST_MJM012402_HPP
|
||||
88
include/boost/thread/detail/invoke.hpp
Normal file
88
include/boost/thread/detail/invoke.hpp
Normal file
@@ -0,0 +1,88 @@
|
||||
// Copyright (C) 2012 Vicente J. Botet Escriba
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
// The invoke code is based on the one from libcxx.
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#ifndef BOOST_THREAD_DETAIL_INVOKE_HPP
|
||||
#define BOOST_THREAD_DETAIL_INVOKE_HPP
|
||||
|
||||
#include <boost/config.hpp>
|
||||
#include <boost/static_assert.hpp>
|
||||
|
||||
namespace boost
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
#if ! defined(BOOST_NO_SFINAE_EXPR) && \
|
||||
! defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) && \
|
||||
! defined(BOOST_NO_CXX11_DECLTYPE) && \
|
||||
! defined(BOOST_NO_CXX11_DECLTYPE_N3276) && \
|
||||
! defined(BOOST_NO_CXX11_AUTO) && \
|
||||
! defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
|
||||
|
||||
// // bullets 1 and 2
|
||||
|
||||
template <class Fp, class A0, class ...Args>
|
||||
inline
|
||||
auto
|
||||
invoke(Fp&& f, A0&& a0, Args&& ...args)
|
||||
-> decltype((boost::forward<A0>(a0).*f)(boost::forward<Args>(args)...))
|
||||
{
|
||||
return (boost::forward<A0>(a0).*f)(boost::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
template <class Fp, class A0, class ...Args>
|
||||
inline
|
||||
auto
|
||||
invoke(Fp&& f, A0&& a0, Args&& ...args)
|
||||
-> decltype(((*boost::forward<A0>(a0)).*f)(boost::forward<Args>(args)...))
|
||||
{
|
||||
return ((*boost::forward<A0>(a0)).*f)(boost::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
// bullets 3 and 4
|
||||
|
||||
template <class Fp, class A0>
|
||||
inline
|
||||
auto
|
||||
invoke(Fp&& f, A0&& a0)
|
||||
-> decltype(boost::forward<A0>(a0).*f)
|
||||
{
|
||||
return boost::forward<A0>(a0).*f;
|
||||
}
|
||||
|
||||
template <class Fp, class A0>
|
||||
inline
|
||||
auto
|
||||
invoke(Fp&& f, A0&& a0)
|
||||
-> decltype((*boost::forward<A0>(a0)).*f)
|
||||
{
|
||||
return (*boost::forward<A0>(a0)).*f;
|
||||
}
|
||||
|
||||
// bullet 5
|
||||
|
||||
template <class Fp, class ...Args>
|
||||
inline
|
||||
auto invoke(Fp&& f, Args&& ...args)
|
||||
-> decltype(boost::forward<Fp>(f)(boost::forward<Args>(args)...))
|
||||
{
|
||||
return boost::forward<Fp>(f)(boost::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
#endif // header
|
||||
48
include/boost/thread/detail/is_convertible.hpp
Normal file
48
include/boost/thread/detail/is_convertible.hpp
Normal file
@@ -0,0 +1,48 @@
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// (C) Copyright 2011-2012 Vicente J. Botet Escriba
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
// See http://www.boost.org/libs/thread for documentation.
|
||||
//
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifndef BOOST_THREAD_DETAIL_IS_CONVERTIBLE_HPP
|
||||
#define BOOST_THREAD_DETAIL_IS_CONVERTIBLE_HPP
|
||||
|
||||
#include <boost/type_traits/is_convertible.hpp>
|
||||
#include <boost/thread/detail/move.hpp>
|
||||
|
||||
namespace boost
|
||||
{
|
||||
namespace thread_detail
|
||||
{
|
||||
template <typename T1, typename T2>
|
||||
struct is_convertible : boost::is_convertible<T1,T2> {};
|
||||
|
||||
#if defined BOOST_NO_CXX11_RVALUE_REFERENCES
|
||||
|
||||
#if defined(BOOST_INTEL_CXX_VERSION) && (BOOST_INTEL_CXX_VERSION <= 1300)
|
||||
|
||||
#if defined BOOST_THREAD_USES_MOVE
|
||||
template <typename T1, typename T2>
|
||||
struct is_convertible<
|
||||
rv<T1> &,
|
||||
rv<rv<T2> > &
|
||||
> : false_type {};
|
||||
#endif
|
||||
|
||||
#elif defined __GNUC__ && (__GNUC__ < 4 || ( __GNUC__ == 4 && __GNUC_MINOR__ <= 4 ))
|
||||
|
||||
template <typename T1, typename T2>
|
||||
struct is_convertible<T1&, T2&> : boost::is_convertible<T1, T2> {};
|
||||
#endif
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
} // namespace boost
|
||||
|
||||
|
||||
#endif // BOOST_THREAD_DETAIL_MEMORY_HPP
|
||||
@@ -1,207 +0,0 @@
|
||||
// Copyright (C) 2001
|
||||
// William E. Kempf
|
||||
//
|
||||
// Permission to use, copy, modify, distribute and sell this software
|
||||
// and its documentation for any purpose is hereby granted without fee,
|
||||
// provided that the above copyright notice appear in all copies and
|
||||
// that both that copyright notice and this permission notice appear
|
||||
// in supporting documentation. William E. Kempf makes no representations
|
||||
// about the suitability of this software for any purpose.
|
||||
// It is provided "as is" without express or implied warranty.
|
||||
|
||||
#ifndef BOOST_XLOCK_WEK070601_HPP
|
||||
#define BOOST_XLOCK_WEK070601_HPP
|
||||
|
||||
#include <boost/utility.hpp>
|
||||
#include <boost/thread/exceptions.hpp>
|
||||
|
||||
namespace boost {
|
||||
|
||||
class condition;
|
||||
struct xtime;
|
||||
|
||||
namespace detail { namespace thread {
|
||||
|
||||
template <typename Mutex>
|
||||
class lock_ops : private noncopyable
|
||||
{
|
||||
private:
|
||||
lock_ops() { }
|
||||
|
||||
public:
|
||||
typedef typename Mutex::cv_state lock_state;
|
||||
|
||||
static void lock(Mutex& m)
|
||||
{
|
||||
m.do_lock();
|
||||
}
|
||||
static bool trylock(Mutex& m)
|
||||
{
|
||||
return m.do_trylock();
|
||||
}
|
||||
static bool timedlock(Mutex& m, const xtime& xt)
|
||||
{
|
||||
return m.do_timedlock(xt);
|
||||
}
|
||||
static void unlock(Mutex& m)
|
||||
{
|
||||
m.do_unlock();
|
||||
}
|
||||
static void lock(Mutex& m, lock_state& state)
|
||||
{
|
||||
m.do_lock(state);
|
||||
}
|
||||
static void unlock(Mutex& m, lock_state& state)
|
||||
{
|
||||
m.do_unlock(state);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename Mutex>
|
||||
class scoped_lock : private noncopyable
|
||||
{
|
||||
public:
|
||||
typedef Mutex mutex_type;
|
||||
|
||||
explicit scoped_lock(Mutex& mx, bool initially_locked=true)
|
||||
: m_mutex(mx), m_locked(false)
|
||||
{
|
||||
if (initially_locked) lock();
|
||||
}
|
||||
~scoped_lock()
|
||||
{
|
||||
if (m_locked) unlock();
|
||||
}
|
||||
|
||||
void lock()
|
||||
{
|
||||
if (m_locked) throw lock_error();
|
||||
lock_ops<Mutex>::lock(m_mutex);
|
||||
m_locked = true;
|
||||
}
|
||||
void unlock()
|
||||
{
|
||||
if (!m_locked) throw lock_error();
|
||||
lock_ops<Mutex>::unlock(m_mutex);
|
||||
m_locked = false;
|
||||
}
|
||||
|
||||
bool locked() const { return m_locked; }
|
||||
operator const void*() const { return m_locked ? this : 0; }
|
||||
|
||||
private:
|
||||
friend class boost::condition;
|
||||
|
||||
Mutex& m_mutex;
|
||||
bool m_locked;
|
||||
};
|
||||
|
||||
template <typename TryMutex>
|
||||
class scoped_try_lock : private noncopyable
|
||||
{
|
||||
public:
|
||||
typedef TryMutex mutex_type;
|
||||
|
||||
explicit scoped_try_lock(TryMutex& mx)
|
||||
: m_mutex(mx), m_locked(false)
|
||||
{
|
||||
try_lock();
|
||||
}
|
||||
scoped_try_lock(TryMutex& mx, bool initially_locked)
|
||||
: m_mutex(mx), m_locked(false)
|
||||
{
|
||||
if (initially_locked) lock();
|
||||
}
|
||||
~scoped_try_lock()
|
||||
{
|
||||
if (m_locked) unlock();
|
||||
}
|
||||
|
||||
void lock()
|
||||
{
|
||||
if (m_locked) throw lock_error();
|
||||
lock_ops<TryMutex>::lock(m_mutex);
|
||||
m_locked = true;
|
||||
}
|
||||
bool try_lock()
|
||||
{
|
||||
if (m_locked) throw lock_error();
|
||||
return (m_locked = lock_ops<TryMutex>::trylock(m_mutex));
|
||||
}
|
||||
void unlock()
|
||||
{
|
||||
if (!m_locked) throw lock_error();
|
||||
lock_ops<TryMutex>::unlock(m_mutex);
|
||||
m_locked = false;
|
||||
}
|
||||
|
||||
bool locked() const { return m_locked; }
|
||||
operator const void*() const { return m_locked ? this : 0; }
|
||||
|
||||
private:
|
||||
friend class boost::condition;
|
||||
|
||||
TryMutex& m_mutex;
|
||||
bool m_locked;
|
||||
};
|
||||
|
||||
template <typename TimedMutex>
|
||||
class scoped_timed_lock : private noncopyable
|
||||
{
|
||||
public:
|
||||
typedef TimedMutex mutex_type;
|
||||
|
||||
scoped_timed_lock(TimedMutex& mx, const xtime& xt)
|
||||
: m_mutex(mx), m_locked(false)
|
||||
{
|
||||
timed_lock(xt);
|
||||
}
|
||||
scoped_timed_lock(TimedMutex& mx, bool initially_locked)
|
||||
: m_mutex(mx), m_locked(false)
|
||||
{
|
||||
if (initially_locked) lock();
|
||||
}
|
||||
~scoped_timed_lock()
|
||||
{
|
||||
if (m_locked) unlock();
|
||||
}
|
||||
|
||||
void lock()
|
||||
{
|
||||
if (m_locked) throw lock_error();
|
||||
lock_ops<TimedMutex>::lock(m_mutex);
|
||||
m_locked = true;
|
||||
}
|
||||
bool timed_lock(const xtime& xt)
|
||||
{
|
||||
if (m_locked) throw lock_error();
|
||||
return (m_locked = lock_ops<TimedMutex>::timedlock(m_mutex, xt));
|
||||
}
|
||||
void unlock()
|
||||
{
|
||||
if (!m_locked) throw lock_error();
|
||||
lock_ops<TimedMutex>::unlock(m_mutex);
|
||||
m_locked = false;
|
||||
}
|
||||
|
||||
bool locked() const { return m_locked; }
|
||||
operator const void*() const { return m_locked ? this : 0; }
|
||||
|
||||
private:
|
||||
friend class boost::condition;
|
||||
|
||||
TimedMutex& m_mutex;
|
||||
bool m_locked;
|
||||
};
|
||||
|
||||
} // namespace thread
|
||||
} // namespace detail
|
||||
} // namespace boost
|
||||
|
||||
// Change Log:
|
||||
// 8 Feb 01 WEKEMPF Initial version.
|
||||
// 22 May 01 WEKEMPF Modified to use xtime for time outs.
|
||||
// 30 Jul 01 WEKEMPF Moved lock types into boost::detail::thread. Renamed some types.
|
||||
// Added locked() methods.
|
||||
|
||||
#endif // BOOST_XLOCK_WEK070601_HPP
|
||||
45
include/boost/thread/detail/lockable_wrapper.hpp
Normal file
45
include/boost/thread/detail/lockable_wrapper.hpp
Normal file
@@ -0,0 +1,45 @@
|
||||
// Distributed under the Boost Software License, Version 1.0. (See
|
||||
// accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
// (C) Copyright 2012 Vicente J. Botet Escriba
|
||||
|
||||
#ifndef BOOST_THREAD_DETAIL_LOCKABLE_WRAPPER_HPP
|
||||
#define BOOST_THREAD_DETAIL_LOCKABLE_WRAPPER_HPP
|
||||
|
||||
#include <boost/thread/detail/config.hpp>
|
||||
|
||||
#if ! defined BOOST_THREAD_NO_CXX11_HDR_INITIALIZER_LIST
|
||||
#include <initializer_list>
|
||||
#endif
|
||||
#include <boost/config/abi_prefix.hpp>
|
||||
|
||||
namespace boost
|
||||
{
|
||||
|
||||
#if ! defined BOOST_THREAD_NO_CXX11_HDR_INITIALIZER_LIST
|
||||
namespace thread_detail
|
||||
{
|
||||
template <typename Mutex>
|
||||
struct lockable_wrapper
|
||||
{
|
||||
Mutex* m;
|
||||
explicit lockable_wrapper(Mutex& m_) :
|
||||
m(&m_)
|
||||
{}
|
||||
};
|
||||
template <typename Mutex>
|
||||
struct lockable_adopt_wrapper
|
||||
{
|
||||
Mutex* m;
|
||||
explicit lockable_adopt_wrapper(Mutex& m_) :
|
||||
m(&m_)
|
||||
{}
|
||||
};
|
||||
}
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
#include <boost/config/abi_suffix.hpp>
|
||||
|
||||
#endif // header
|
||||
83
include/boost/thread/detail/log.hpp
Normal file
83
include/boost/thread/detail/log.hpp
Normal file
@@ -0,0 +1,83 @@
|
||||
// Copyright (C) 2012 Vicente J. Botet Escriba
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef BOOST_THREAD_DETAIL_LOG_HPP
|
||||
#define BOOST_THREAD_DETAIL_LOG_HPP
|
||||
|
||||
#include <boost/thread/detail/config.hpp>
|
||||
#if defined BOOST_THREAD_USES_LOG
|
||||
#include <boost/thread/recursive_mutex.hpp>
|
||||
#include <boost/thread/lock_guard.hpp>
|
||||
#if defined BOOST_THREAD_USES_LOG_THREAD_ID
|
||||
#include <boost/thread/thread.hpp>
|
||||
#endif
|
||||
#include <iostream>
|
||||
|
||||
namespace boost
|
||||
{
|
||||
namespace thread_detail
|
||||
{
|
||||
inline boost::recursive_mutex& terminal_mutex()
|
||||
{
|
||||
static boost::recursive_mutex mtx;
|
||||
return mtx;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
#if defined BOOST_THREAD_USES_LOG_THREAD_ID
|
||||
|
||||
#define BOOST_THREAD_LOG \
|
||||
{ \
|
||||
boost::lock_guard<boost::recursive_mutex> _lk_(boost::thread_detail::terminal_mutex()); \
|
||||
std::cout << boost::this_thread::get_id() << " - "<<__FILE__<<"["<<__LINE__<<"] " <<std::dec
|
||||
#else
|
||||
|
||||
#define BOOST_THREAD_LOG \
|
||||
{ \
|
||||
boost::lock_guard<boost::recursive_mutex> _lk_(boost::thread_detail::terminal_mutex()); \
|
||||
std::cout << __FILE__<<"["<<__LINE__<<"] " <<std::dec
|
||||
|
||||
#endif
|
||||
#define BOOST_THREAD_END_LOG \
|
||||
std::dec << std::endl; \
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
namespace boost
|
||||
{
|
||||
namespace thread_detail
|
||||
{
|
||||
struct dummy_stream_t
|
||||
{
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
inline dummy_stream_t const& operator<<(dummy_stream_t const& os, T)
|
||||
{
|
||||
return os;
|
||||
}
|
||||
|
||||
inline dummy_stream_t const& operator<<(dummy_stream_t const& os, dummy_stream_t const&)
|
||||
{
|
||||
return os;
|
||||
}
|
||||
|
||||
|
||||
BOOST_CONSTEXPR_OR_CONST dummy_stream_t dummy_stream = {};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#define BOOST_THREAD_LOG if (true) {} else boost::thread_detail::dummy_stream
|
||||
#define BOOST_THREAD_END_LOG boost::thread_detail::dummy_stream
|
||||
|
||||
#endif
|
||||
|
||||
#define BOOST_THREAD_TRACE BOOST_THREAD_LOG << BOOST_THREAD_END_LOG
|
||||
|
||||
|
||||
#endif // header
|
||||
60
include/boost/thread/detail/make_tuple_indices.hpp
Normal file
60
include/boost/thread/detail/make_tuple_indices.hpp
Normal file
@@ -0,0 +1,60 @@
|
||||
// Copyright (C) 2012 Vicente J. Botet Escriba
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
// The make_tuple_indices code is based on the one from libcxx.
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#ifndef BOOST_THREAD_DETAIL_MAKE_TUPLE_INDICES_HPP
|
||||
#define BOOST_THREAD_DETAIL_MAKE_TUPLE_INDICES_HPP
|
||||
|
||||
#include <boost/config.hpp>
|
||||
#include <boost/static_assert.hpp>
|
||||
|
||||
namespace boost
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
|
||||
#if ! defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) && \
|
||||
! defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
|
||||
|
||||
// make_tuple_indices
|
||||
|
||||
template <std::size_t...> struct tuple_indices
|
||||
{};
|
||||
|
||||
template <std::size_t Sp, class IntTuple, std::size_t Ep>
|
||||
struct make_indices_imp;
|
||||
|
||||
template <std::size_t Sp, std::size_t ...Indices, std::size_t Ep>
|
||||
struct make_indices_imp<Sp, tuple_indices<Indices...>, Ep>
|
||||
{
|
||||
typedef typename make_indices_imp<Sp+1, tuple_indices<Indices..., Sp>, Ep>::type type;
|
||||
};
|
||||
|
||||
template <std::size_t Ep, std::size_t ...Indices>
|
||||
struct make_indices_imp<Ep, tuple_indices<Indices...>, Ep>
|
||||
{
|
||||
typedef tuple_indices<Indices...> type;
|
||||
};
|
||||
|
||||
template <std::size_t Ep, std::size_t Sp = 0>
|
||||
struct make_tuple_indices
|
||||
{
|
||||
BOOST_STATIC_ASSERT_MSG(Sp <= Ep, "make_tuple_indices input error");
|
||||
typedef typename make_indices_imp<Sp, tuple_indices<>, Ep>::type type;
|
||||
};
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
#endif // header
|
||||
156
include/boost/thread/detail/memory.hpp
Normal file
156
include/boost/thread/detail/memory.hpp
Normal file
@@ -0,0 +1,156 @@
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// (C) Copyright 2011-2012 Vicente J. Botet Escriba
|
||||
// Software License, Version 1.0. (See accompanying file
|
||||
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
// See http://www.boost.org/libs/thread for documentation.
|
||||
//
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifndef BOOST_THREAD_DETAIL_MEMORY_HPP
|
||||
#define BOOST_THREAD_DETAIL_MEMORY_HPP
|
||||
|
||||
#include <boost/config.hpp>
|
||||
#include <boost/container/allocator_traits.hpp>
|
||||
#include <boost/container/scoped_allocator.hpp>
|
||||
#include <boost/type_traits/remove_cv.hpp>
|
||||
#include <boost/type_traits/is_convertible.hpp>
|
||||
#include <boost/type_traits/is_scalar.hpp>
|
||||
#include <boost/type_traits/is_pointer.hpp>
|
||||
#include <boost/type_traits/is_same.hpp>
|
||||
#include <boost/static_assert.hpp>
|
||||
|
||||
namespace boost
|
||||
{
|
||||
namespace thread_detail
|
||||
{
|
||||
template <class _Alloc>
|
||||
class allocator_destructor
|
||||
{
|
||||
typedef container::allocator_traits<_Alloc> alloc_traits;
|
||||
public:
|
||||
typedef typename alloc_traits::pointer pointer;
|
||||
typedef typename alloc_traits::size_type size_type;
|
||||
private:
|
||||
_Alloc alloc_;
|
||||
size_type s_;
|
||||
public:
|
||||
allocator_destructor(_Alloc& a, size_type s)BOOST_NOEXCEPT
|
||||
: alloc_(a), s_(s)
|
||||
{}
|
||||
void operator()(pointer p)BOOST_NOEXCEPT
|
||||
{
|
||||
alloc_traits::destroy(alloc_, p);
|
||||
alloc_traits::deallocate(alloc_, p, s_);
|
||||
}
|
||||
};
|
||||
} //namespace thread_detail
|
||||
|
||||
typedef container::allocator_arg_t allocator_arg_t;
|
||||
BOOST_CONSTEXPR_OR_CONST allocator_arg_t allocator_arg = {};
|
||||
|
||||
template <class T, class Alloc>
|
||||
struct uses_allocator: public container::uses_allocator<T, Alloc>
|
||||
{
|
||||
};
|
||||
|
||||
template <class Ptr>
|
||||
struct pointer_traits
|
||||
{
|
||||
typedef Ptr pointer;
|
||||
// typedef <details> element_type;
|
||||
// typedef <details> difference_type;
|
||||
|
||||
// template <class U> using rebind = <details>;
|
||||
//
|
||||
// static pointer pointer_to(<details>);
|
||||
};
|
||||
|
||||
template <class T>
|
||||
struct pointer_traits<T*>
|
||||
{
|
||||
typedef T* pointer;
|
||||
typedef T element_type;
|
||||
typedef ptrdiff_t difference_type;
|
||||
|
||||
// template <class U> using rebind = U*;
|
||||
//
|
||||
// static pointer pointer_to(<details>) noexcept;
|
||||
};
|
||||
|
||||
|
||||
namespace thread_detail {
|
||||
template <class _Ptr1, class _Ptr2,
|
||||
bool = is_same<typename remove_cv<typename pointer_traits<_Ptr1>::element_type>::type,
|
||||
typename remove_cv<typename pointer_traits<_Ptr2>::element_type>::type
|
||||
>::value
|
||||
>
|
||||
struct same_or_less_cv_qualified_imp
|
||||
: is_convertible<_Ptr1, _Ptr2> {};
|
||||
|
||||
template <class _Ptr1, class _Ptr2>
|
||||
struct same_or_less_cv_qualified_imp<_Ptr1, _Ptr2, false>
|
||||
: false_type {};
|
||||
|
||||
template <class _Ptr1, class _Ptr2, bool = is_scalar<_Ptr1>::value &&
|
||||
!is_pointer<_Ptr1>::value>
|
||||
struct same_or_less_cv_qualified
|
||||
: same_or_less_cv_qualified_imp<_Ptr1, _Ptr2> {};
|
||||
|
||||
template <class _Ptr1, class _Ptr2>
|
||||
struct same_or_less_cv_qualified<_Ptr1, _Ptr2, true>
|
||||
: false_type {};
|
||||
|
||||
}
|
||||
template <class T>
|
||||
struct BOOST_SYMBOL_VISIBLE default_delete
|
||||
{
|
||||
#ifndef BOOST_NO_CXX11_DEFAULTED_FUNCTIONS
|
||||
BOOST_SYMBOL_VISIBLE
|
||||
BOOST_CONSTEXPR default_delete() = default;
|
||||
#else
|
||||
BOOST_SYMBOL_VISIBLE
|
||||
BOOST_CONSTEXPR default_delete() BOOST_NOEXCEPT {}
|
||||
#endif
|
||||
template <class U>
|
||||
BOOST_SYMBOL_VISIBLE
|
||||
default_delete(const default_delete<U>&,
|
||||
typename enable_if<is_convertible<U*, T*> >::type* = 0) BOOST_NOEXCEPT {}
|
||||
BOOST_SYMBOL_VISIBLE
|
||||
void operator() (T* ptr) const BOOST_NOEXCEPT
|
||||
{
|
||||
BOOST_STATIC_ASSERT_MSG(sizeof(T) > 0, "default_delete can not delete incomplete type");
|
||||
delete ptr;
|
||||
}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
struct BOOST_SYMBOL_VISIBLE default_delete<T[]>
|
||||
{
|
||||
public:
|
||||
#ifndef BOOST_NO_CXX11_DEFAULTED_FUNCTIONS
|
||||
BOOST_SYMBOL_VISIBLE
|
||||
BOOST_CONSTEXPR default_delete() = default;
|
||||
#else
|
||||
BOOST_SYMBOL_VISIBLE
|
||||
BOOST_CONSTEXPR default_delete() BOOST_NOEXCEPT {}
|
||||
#endif
|
||||
template <class U>
|
||||
BOOST_SYMBOL_VISIBLE
|
||||
default_delete(const default_delete<U[]>&,
|
||||
typename enable_if<thread_detail::same_or_less_cv_qualified<U*, T*> >::type* = 0) BOOST_NOEXCEPT {}
|
||||
template <class U>
|
||||
BOOST_SYMBOL_VISIBLE
|
||||
void operator() (U* ptr,
|
||||
typename enable_if<thread_detail::same_or_less_cv_qualified<U*, T*> >::type* = 0) const BOOST_NOEXCEPT
|
||||
{
|
||||
BOOST_STATIC_ASSERT_MSG(sizeof(T) > 0, "default_delete can not delete incomplete type");
|
||||
delete [] ptr;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace boost
|
||||
|
||||
|
||||
#endif // BOOST_THREAD_DETAIL_MEMORY_HPP
|
||||
261
include/boost/thread/detail/move.hpp
Normal file
261
include/boost/thread/detail/move.hpp
Normal file
@@ -0,0 +1,261 @@
|
||||
// Distributed under the Boost Software License, Version 1.0. (See
|
||||
// accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
// (C) Copyright 2007-8 Anthony Williams
|
||||
// (C) Copyright 2011-2012 Vicente J. Botet Escriba
|
||||
|
||||
#ifndef BOOST_THREAD_MOVE_HPP
|
||||
#define BOOST_THREAD_MOVE_HPP
|
||||
|
||||
#include <boost/thread/detail/config.hpp>
|
||||
#ifndef BOOST_NO_SFINAE
|
||||
#include <boost/utility/enable_if.hpp>
|
||||
#include <boost/type_traits/is_convertible.hpp>
|
||||
#include <boost/type_traits/remove_reference.hpp>
|
||||
#include <boost/type_traits/remove_cv.hpp>
|
||||
#include <boost/type_traits/decay.hpp>
|
||||
#endif
|
||||
|
||||
#include <boost/thread/detail/delete.hpp>
|
||||
#include <boost/move/utility.hpp>
|
||||
#include <boost/config/abi_prefix.hpp>
|
||||
|
||||
namespace boost
|
||||
{
|
||||
|
||||
namespace detail
|
||||
{
|
||||
template <typename T>
|
||||
struct enable_move_utility_emulation_dummy_specialization;
|
||||
template<typename T>
|
||||
struct thread_move_t
|
||||
{
|
||||
T& t;
|
||||
explicit thread_move_t(T& t_):
|
||||
t(t_)
|
||||
{}
|
||||
|
||||
T& operator*() const
|
||||
{
|
||||
return t;
|
||||
}
|
||||
|
||||
T* operator->() const
|
||||
{
|
||||
return &t;
|
||||
}
|
||||
private:
|
||||
void operator=(thread_move_t&);
|
||||
};
|
||||
}
|
||||
|
||||
#if !defined BOOST_THREAD_USES_MOVE
|
||||
|
||||
#ifndef BOOST_NO_SFINAE
|
||||
template<typename T>
|
||||
typename enable_if<boost::is_convertible<T&,boost::detail::thread_move_t<T> >, boost::detail::thread_move_t<T> >::type move(T& t)
|
||||
{
|
||||
return boost::detail::thread_move_t<T>(t);
|
||||
}
|
||||
#endif
|
||||
|
||||
template<typename T>
|
||||
boost::detail::thread_move_t<T> move(boost::detail::thread_move_t<T> t)
|
||||
{
|
||||
return t;
|
||||
}
|
||||
|
||||
#endif //#if !defined BOOST_THREAD_USES_MOVE
|
||||
}
|
||||
|
||||
#if ! defined BOOST_NO_CXX11_RVALUE_REFERENCES
|
||||
|
||||
#define BOOST_THREAD_RV_REF(TYPE) BOOST_RV_REF(TYPE)
|
||||
#define BOOST_THREAD_RV_REF_2_TEMPL_ARGS(TYPE) BOOST_RV_REF_2_TEMPL_ARGS(TYPE)
|
||||
#define BOOST_THREAD_RV_REF_BEG BOOST_RV_REF_BEG
|
||||
#define BOOST_THREAD_RV_REF_END BOOST_RV_REF_END
|
||||
#define BOOST_THREAD_RV(V) V
|
||||
#define BOOST_THREAD_MAKE_RV_REF(RVALUE) RVALUE
|
||||
#define BOOST_THREAD_FWD_REF(TYPE) BOOST_FWD_REF(TYPE)
|
||||
#define BOOST_THREAD_DCL_MOVABLE(TYPE)
|
||||
#define BOOST_THREAD_DCL_MOVABLE_BEG(T) \
|
||||
namespace detail { \
|
||||
template <typename T> \
|
||||
struct enable_move_utility_emulation_dummy_specialization<
|
||||
|
||||
#define BOOST_THREAD_DCL_MOVABLE_END > \
|
||||
: integral_constant<bool, false> \
|
||||
{}; \
|
||||
}
|
||||
|
||||
#elif ! defined BOOST_NO_CXX11_RVALUE_REFERENCES && defined BOOST_MSVC
|
||||
|
||||
#define BOOST_THREAD_RV_REF(TYPE) BOOST_RV_REF(TYPE)
|
||||
#define BOOST_THREAD_RV_REF_2_TEMPL_ARGS(TYPE) BOOST_RV_REF_2_TEMPL_ARGS(TYPE)
|
||||
#define BOOST_THREAD_RV_REF_BEG BOOST_RV_REF_BEG
|
||||
#define BOOST_THREAD_RV_REF_END BOOST_RV_REF_END
|
||||
#define BOOST_THREAD_RV(V) V
|
||||
#define BOOST_THREAD_MAKE_RV_REF(RVALUE) RVALUE
|
||||
#define BOOST_THREAD_FWD_REF(TYPE) BOOST_FWD_REF(TYPE)
|
||||
#define BOOST_THREAD_DCL_MOVABLE(TYPE)
|
||||
#define BOOST_THREAD_DCL_MOVABLE_BEG(T) \
|
||||
namespace detail { \
|
||||
template <typename T> \
|
||||
struct enable_move_utility_emulation_dummy_specialization<
|
||||
|
||||
#define BOOST_THREAD_DCL_MOVABLE_END > \
|
||||
: integral_constant<bool, false> \
|
||||
{}; \
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
#if defined BOOST_THREAD_USES_MOVE
|
||||
#define BOOST_THREAD_RV_REF(TYPE) BOOST_RV_REF(TYPE)
|
||||
#define BOOST_THREAD_RV_REF_2_TEMPL_ARGS(TYPE) BOOST_RV_REF_2_TEMPL_ARGS(TYPE)
|
||||
#define BOOST_THREAD_RV_REF_BEG BOOST_RV_REF_BEG
|
||||
#define BOOST_THREAD_RV_REF_END BOOST_RV_REF_END
|
||||
#define BOOST_THREAD_RV(V) V
|
||||
#define BOOST_THREAD_FWD_REF(TYPE) BOOST_FWD_REF(TYPE)
|
||||
#define BOOST_THREAD_DCL_MOVABLE(TYPE)
|
||||
#define BOOST_THREAD_DCL_MOVABLE_BEG(T) \
|
||||
namespace detail { \
|
||||
template <typename T> \
|
||||
struct enable_move_utility_emulation_dummy_specialization<
|
||||
|
||||
#define BOOST_THREAD_DCL_MOVABLE_END > \
|
||||
: integral_constant<bool, false> \
|
||||
{}; \
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
#define BOOST_THREAD_RV_REF(TYPE) boost::detail::thread_move_t< TYPE >
|
||||
#define BOOST_THREAD_RV_REF_BEG boost::detail::thread_move_t<
|
||||
#define BOOST_THREAD_RV_REF_END >
|
||||
#define BOOST_THREAD_RV(V) (*V)
|
||||
#define BOOST_THREAD_FWD_REF(TYPE) BOOST_FWD_REF(TYPE)
|
||||
|
||||
#define BOOST_THREAD_DCL_MOVABLE(TYPE) \
|
||||
template <> \
|
||||
struct enable_move_utility_emulation< TYPE > \
|
||||
{ \
|
||||
static const bool value = false; \
|
||||
};
|
||||
|
||||
#define BOOST_THREAD_DCL_MOVABLE_BEG(T) \
|
||||
template <typename T> \
|
||||
struct enable_move_utility_emulation<
|
||||
|
||||
#define BOOST_THREAD_DCL_MOVABLE_END > \
|
||||
{ \
|
||||
static const bool value = false; \
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
namespace boost
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
template <typename T>
|
||||
BOOST_THREAD_RV_REF(typename ::boost::remove_cv<typename ::boost::remove_reference<T>::type>::type)
|
||||
make_rv_ref(T v) BOOST_NOEXCEPT
|
||||
{
|
||||
return (BOOST_THREAD_RV_REF(typename ::boost::remove_cv<typename ::boost::remove_reference<T>::type>::type))(v);
|
||||
}
|
||||
// template <typename T>
|
||||
// BOOST_THREAD_RV_REF(typename ::boost::remove_cv<typename ::boost::remove_reference<T>::type>::type)
|
||||
// make_rv_ref(T &v) BOOST_NOEXCEPT
|
||||
// {
|
||||
// return (BOOST_THREAD_RV_REF(typename ::boost::remove_cv<typename ::boost::remove_reference<T>::type>::type))(v);
|
||||
// }
|
||||
// template <typename T>
|
||||
// const BOOST_THREAD_RV_REF(typename ::boost::remove_cv<typename ::boost::remove_reference<T>::type>::type)
|
||||
// make_rv_ref(T const&v) BOOST_NOEXCEPT
|
||||
// {
|
||||
// return (const BOOST_THREAD_RV_REF(typename ::boost::remove_cv<typename ::boost::remove_reference<T>::type>::type))(v);
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
#define BOOST_THREAD_MAKE_RV_REF(RVALUE) RVALUE.move()
|
||||
//#define BOOST_THREAD_MAKE_RV_REF(RVALUE) boost::detail::make_rv_ref(RVALUE)
|
||||
#endif
|
||||
|
||||
|
||||
#if ! defined BOOST_NO_CXX11_RVALUE_REFERENCES
|
||||
|
||||
#define BOOST_THREAD_MOVABLE(TYPE)
|
||||
|
||||
#else
|
||||
|
||||
#if defined BOOST_THREAD_USES_MOVE
|
||||
|
||||
#define BOOST_THREAD_MOVABLE(TYPE) \
|
||||
::boost::rv<TYPE>& move() BOOST_NOEXCEPT \
|
||||
{ \
|
||||
return *static_cast< ::boost::rv<TYPE>* >(this); \
|
||||
} \
|
||||
const ::boost::rv<TYPE>& move() const BOOST_NOEXCEPT \
|
||||
{ \
|
||||
return *static_cast<const ::boost::rv<TYPE>* >(this); \
|
||||
} \
|
||||
operator ::boost::rv<TYPE>&() \
|
||||
{ \
|
||||
return *static_cast< ::boost::rv<TYPE>* >(this); \
|
||||
} \
|
||||
operator const ::boost::rv<TYPE>&() const \
|
||||
{ \
|
||||
return *static_cast<const ::boost::rv<TYPE>* >(this); \
|
||||
}\
|
||||
|
||||
#else
|
||||
|
||||
#define BOOST_THREAD_MOVABLE(TYPE) \
|
||||
operator ::boost::detail::thread_move_t<TYPE>() BOOST_NOEXCEPT \
|
||||
{ \
|
||||
return move(); \
|
||||
} \
|
||||
::boost::detail::thread_move_t<TYPE> move() BOOST_NOEXCEPT \
|
||||
{ \
|
||||
::boost::detail::thread_move_t<TYPE> x(*this); \
|
||||
return x; \
|
||||
} \
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#define BOOST_THREAD_MOVABLE_ONLY(TYPE) \
|
||||
BOOST_THREAD_NO_COPYABLE(TYPE) \
|
||||
BOOST_THREAD_MOVABLE(TYPE) \
|
||||
|
||||
#define BOOST_THREAD_COPYABLE_AND_MOVABLE(TYPE) \
|
||||
BOOST_THREAD_MOVABLE(TYPE) \
|
||||
|
||||
|
||||
|
||||
namespace boost
|
||||
{ namespace thread_detail
|
||||
{
|
||||
#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
|
||||
template <class T>
|
||||
typename decay<T>::type
|
||||
decay_copy(T&& t)
|
||||
{
|
||||
return boost::forward<T>(t);
|
||||
}
|
||||
#else
|
||||
template <class T>
|
||||
typename decay<T>::type
|
||||
decay_copy(BOOST_THREAD_FWD_REF(T) t)
|
||||
{
|
||||
return boost::forward<T>(t);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
#include <boost/config/abi_suffix.hpp>
|
||||
|
||||
#endif
|
||||
73
include/boost/thread/detail/platform.hpp
Normal file
73
include/boost/thread/detail/platform.hpp
Normal file
@@ -0,0 +1,73 @@
|
||||
// Copyright 2006 Roland Schwarz.
|
||||
// (C) Copyright 2007 Anthony Williams
|
||||
// Distributed under the Boost Software License, Version 1.0. (See
|
||||
// accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
// This work is a reimplementation along the design and ideas
|
||||
// of William E. Kempf.
|
||||
|
||||
#ifndef BOOST_THREAD_RS06040501_HPP
|
||||
#define BOOST_THREAD_RS06040501_HPP
|
||||
|
||||
// fetch compiler and platform configuration
|
||||
#include <boost/config.hpp>
|
||||
|
||||
// insist on threading support being available:
|
||||
#include <boost/config/requires_threads.hpp>
|
||||
|
||||
// choose platform
|
||||
#if defined(linux) || defined(__linux) || defined(__linux__)
|
||||
# define BOOST_THREAD_LINUX
|
||||
//# define BOOST_THREAD_WAIT_BUG boost::posix_time::microseconds(100000)
|
||||
#elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
|
||||
# define BOOST_THREAD_BSD
|
||||
#elif defined(sun) || defined(__sun)
|
||||
# define BOOST_THREAD_SOLARIS
|
||||
#elif defined(__sgi)
|
||||
# define BOOST_THREAD_IRIX
|
||||
#elif defined(__hpux)
|
||||
# define BOOST_THREAD_HPUX
|
||||
#elif defined(__CYGWIN__)
|
||||
# define BOOST_THREAD_CYGWIN
|
||||
#elif (defined(_WIN32) || defined(__WIN32__) || defined(WIN32)) && !defined(BOOST_DISABLE_WIN32)
|
||||
# define BOOST_THREAD_WIN32
|
||||
#elif defined(__BEOS__)
|
||||
# define BOOST_THREAD_BEOS
|
||||
#elif defined(macintosh) || defined(__APPLE__) || defined(__APPLE_CC__)
|
||||
# define BOOST_THREAD_MACOS
|
||||
//# define BOOST_THREAD_WAIT_BUG boost::posix_time::microseconds(1000)
|
||||
#elif defined(__IBMCPP__) || defined(_AIX)
|
||||
# define BOOST_THREAD_AIX
|
||||
#elif defined(__amigaos__)
|
||||
# define BOOST_THREAD_AMIGAOS
|
||||
#elif defined(__QNXNTO__)
|
||||
# define BOOST_THREAD_QNXNTO
|
||||
#elif defined(unix) || defined(__unix) || defined(_XOPEN_SOURCE) || defined(_POSIX_SOURCE)
|
||||
# if defined(BOOST_HAS_PTHREADS) && !defined(BOOST_THREAD_POSIX)
|
||||
# define BOOST_THREAD_POSIX
|
||||
# endif
|
||||
#endif
|
||||
|
||||
// For every supported platform add a new entry into the dispatch table below.
|
||||
// BOOST_THREAD_POSIX is tested first, so on platforms where posix and native
|
||||
// threading is available, the user may choose, by defining BOOST_THREAD_POSIX
|
||||
// in her source. If a platform is known to support pthreads and no native
|
||||
// port of boost_thread is available just specify "pthread" in the
|
||||
// dispatcher table. If there is no entry for a platform but pthreads is
|
||||
// available on the platform, pthread is choosen as default. If nothing is
|
||||
// available the preprocessor will fail with a diagnostic message.
|
||||
|
||||
#if defined(BOOST_THREAD_POSIX)
|
||||
# define BOOST_THREAD_PLATFORM_PTHREAD
|
||||
#else
|
||||
# if defined(BOOST_THREAD_WIN32)
|
||||
# define BOOST_THREAD_PLATFORM_WIN32
|
||||
# elif defined(BOOST_HAS_PTHREADS)
|
||||
# define BOOST_THREAD_PLATFORM_PTHREAD
|
||||
# else
|
||||
# error "Sorry, no boost threads are available for this platform."
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#endif // BOOST_THREAD_RS06040501_HPP
|
||||
59
include/boost/thread/detail/singleton.hpp
Normal file
59
include/boost/thread/detail/singleton.hpp
Normal file
@@ -0,0 +1,59 @@
|
||||
// Copyright (C) 2001-2003
|
||||
// Mac Murrett
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See
|
||||
// accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
// See http://www.boost.org for most recent version including documentation.
|
||||
|
||||
#ifndef BOOST_SINGLETON_MJM012402_HPP
|
||||
#define BOOST_SINGLETON_MJM012402_HPP
|
||||
|
||||
#include <boost/thread/detail/config.hpp>
|
||||
|
||||
namespace boost {
|
||||
namespace detail {
|
||||
namespace thread {
|
||||
|
||||
// class singleton has the same goal as all singletons: create one instance of
|
||||
// a class on demand, then dish it out as requested.
|
||||
|
||||
template <class T>
|
||||
class singleton : private T
|
||||
{
|
||||
private:
|
||||
singleton();
|
||||
~singleton();
|
||||
|
||||
public:
|
||||
static T &instance();
|
||||
};
|
||||
|
||||
|
||||
template <class T>
|
||||
inline singleton<T>::singleton()
|
||||
{
|
||||
/* no-op */
|
||||
}
|
||||
|
||||
template <class T>
|
||||
inline singleton<T>::~singleton()
|
||||
{
|
||||
/* no-op */
|
||||
}
|
||||
|
||||
template <class T>
|
||||
/*static*/ T &singleton<T>::instance()
|
||||
{
|
||||
// function-local static to force this to work correctly at static
|
||||
// initialization time.
|
||||
static singleton<T> s_oT;
|
||||
return(s_oT);
|
||||
}
|
||||
|
||||
} // namespace thread
|
||||
} // namespace detail
|
||||
} // namespace boost
|
||||
|
||||
#endif // BOOST_SINGLETON_MJM012402_HPP
|
||||
843
include/boost/thread/detail/thread.hpp
Normal file
843
include/boost/thread/detail/thread.hpp
Normal file
@@ -0,0 +1,843 @@
|
||||
#ifndef BOOST_THREAD_THREAD_COMMON_HPP
|
||||
#define BOOST_THREAD_THREAD_COMMON_HPP
|
||||
// Distributed under the Boost Software License, Version 1.0. (See
|
||||
// accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
// (C) Copyright 2007-2010 Anthony Williams
|
||||
// (C) Copyright 20011-2012 Vicente J. Botet Escriba
|
||||
|
||||
#include <boost/thread/detail/config.hpp>
|
||||
|
||||
#include <boost/thread/exceptions.hpp>
|
||||
#ifndef BOOST_NO_IOSTREAM
|
||||
#include <ostream>
|
||||
#endif
|
||||
#include <boost/thread/detail/move.hpp>
|
||||
#include <boost/thread/mutex.hpp>
|
||||
#if defined BOOST_THREAD_USES_DATETIME
|
||||
#include <boost/thread/xtime.hpp>
|
||||
#endif
|
||||
#include <boost/thread/detail/thread_heap_alloc.hpp>
|
||||
#include <boost/thread/detail/make_tuple_indices.hpp>
|
||||
#include <boost/thread/detail/invoke.hpp>
|
||||
#include <boost/thread/detail/is_convertible.hpp>
|
||||
#include <boost/assert.hpp>
|
||||
#include <list>
|
||||
#include <algorithm>
|
||||
#include <boost/ref.hpp>
|
||||
#include <boost/cstdint.hpp>
|
||||
#include <boost/bind.hpp>
|
||||
#include <stdlib.h>
|
||||
#include <memory>
|
||||
#include <boost/utility/enable_if.hpp>
|
||||
#include <boost/type_traits/remove_reference.hpp>
|
||||
#include <boost/io/ios_state.hpp>
|
||||
#include <boost/type_traits/is_same.hpp>
|
||||
#include <boost/type_traits/decay.hpp>
|
||||
#include <boost/functional/hash.hpp>
|
||||
#ifdef BOOST_THREAD_USES_CHRONO
|
||||
#include <boost/chrono/system_clocks.hpp>
|
||||
#include <boost/chrono/ceil.hpp>
|
||||
#endif
|
||||
|
||||
#if defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
|
||||
#include <tuple>
|
||||
#endif
|
||||
#include <boost/config/abi_prefix.hpp>
|
||||
|
||||
#ifdef BOOST_MSVC
|
||||
#pragma warning(push)
|
||||
#pragma warning(disable:4251)
|
||||
#endif
|
||||
|
||||
namespace boost
|
||||
{
|
||||
|
||||
namespace detail
|
||||
{
|
||||
|
||||
#if defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
|
||||
|
||||
template<typename F, class ...ArgTypes>
|
||||
class thread_data:
|
||||
public detail::thread_data_base
|
||||
{
|
||||
public:
|
||||
BOOST_THREAD_NO_COPYABLE(thread_data)
|
||||
#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
|
||||
thread_data(BOOST_THREAD_RV_REF(F) f_, BOOST_THREAD_RV_REF(ArgTypes)... args_):
|
||||
fp(boost::forward<F>(f_), boost::forward<ArgTypes>(args_)...)
|
||||
{}
|
||||
#endif
|
||||
template <std::size_t ...Indices>
|
||||
void run2(tuple_indices<Indices...>)
|
||||
{
|
||||
|
||||
invoke(std::move(std::get<0>(fp)), std::move(std::get<Indices>(fp))...);
|
||||
}
|
||||
void run()
|
||||
{
|
||||
typedef typename make_tuple_indices<std::tuple_size<std::tuple<F, ArgTypes...> >::value, 1>::type index_type;
|
||||
|
||||
run2(index_type());
|
||||
}
|
||||
|
||||
private:
|
||||
std::tuple<typename decay<F>::type, typename decay<ArgTypes>::type...> fp;
|
||||
};
|
||||
#else // defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
|
||||
|
||||
template<typename F>
|
||||
class thread_data:
|
||||
public detail::thread_data_base
|
||||
{
|
||||
public:
|
||||
BOOST_THREAD_NO_COPYABLE(thread_data)
|
||||
#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
|
||||
thread_data(BOOST_THREAD_RV_REF(F) f_):
|
||||
f(boost::forward<F>(f_))
|
||||
{}
|
||||
// This overloading must be removed if we want the packaged_task's tests to pass.
|
||||
// thread_data(F& f_):
|
||||
// f(f_)
|
||||
// {}
|
||||
#else
|
||||
|
||||
thread_data(BOOST_THREAD_RV_REF(F) f_):
|
||||
f(f_)
|
||||
{}
|
||||
thread_data(F f_):
|
||||
f(f_)
|
||||
{}
|
||||
#endif
|
||||
//thread_data() {}
|
||||
|
||||
void run()
|
||||
{
|
||||
f();
|
||||
}
|
||||
|
||||
private:
|
||||
F f;
|
||||
};
|
||||
|
||||
template<typename F>
|
||||
class thread_data<boost::reference_wrapper<F> >:
|
||||
public detail::thread_data_base
|
||||
{
|
||||
private:
|
||||
F& f;
|
||||
public:
|
||||
BOOST_THREAD_NO_COPYABLE(thread_data)
|
||||
thread_data(boost::reference_wrapper<F> f_):
|
||||
f(f_)
|
||||
{}
|
||||
void run()
|
||||
{
|
||||
f();
|
||||
}
|
||||
};
|
||||
|
||||
template<typename F>
|
||||
class thread_data<const boost::reference_wrapper<F> >:
|
||||
public detail::thread_data_base
|
||||
{
|
||||
private:
|
||||
F& f;
|
||||
public:
|
||||
BOOST_THREAD_NO_COPYABLE(thread_data)
|
||||
thread_data(const boost::reference_wrapper<F> f_):
|
||||
f(f_)
|
||||
{}
|
||||
void run()
|
||||
{
|
||||
f();
|
||||
}
|
||||
};
|
||||
#endif
|
||||
}
|
||||
|
||||
class BOOST_THREAD_DECL thread
|
||||
{
|
||||
public:
|
||||
typedef thread_attributes attributes;
|
||||
|
||||
BOOST_THREAD_MOVABLE_ONLY(thread)
|
||||
private:
|
||||
|
||||
struct dummy;
|
||||
|
||||
void release_handle();
|
||||
|
||||
detail::thread_data_ptr thread_info;
|
||||
|
||||
private:
|
||||
bool start_thread_noexcept();
|
||||
bool start_thread_noexcept(const attributes& attr);
|
||||
public:
|
||||
void start_thread()
|
||||
{
|
||||
if (!start_thread_noexcept())
|
||||
{
|
||||
boost::throw_exception(thread_resource_error());
|
||||
}
|
||||
}
|
||||
void start_thread(const attributes& attr)
|
||||
{
|
||||
if (!start_thread_noexcept(attr))
|
||||
{
|
||||
boost::throw_exception(thread_resource_error());
|
||||
}
|
||||
}
|
||||
|
||||
explicit thread(detail::thread_data_ptr data);
|
||||
|
||||
detail::thread_data_ptr get_thread_info BOOST_PREVENT_MACRO_SUBSTITUTION () const;
|
||||
|
||||
#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
|
||||
#if defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
|
||||
template<typename F, class ...ArgTypes>
|
||||
static inline detail::thread_data_ptr make_thread_info(BOOST_THREAD_RV_REF(F) f, BOOST_THREAD_RV_REF(ArgTypes)... args)
|
||||
{
|
||||
return detail::thread_data_ptr(detail::heap_new<
|
||||
detail::thread_data<typename boost::remove_reference<F>::type, ArgTypes...>
|
||||
>(
|
||||
boost::forward<F>(f), boost::forward<ArgTypes>(args)...
|
||||
)
|
||||
);
|
||||
}
|
||||
#else
|
||||
template<typename F>
|
||||
static inline detail::thread_data_ptr make_thread_info(BOOST_THREAD_RV_REF(F) f)
|
||||
{
|
||||
return detail::thread_data_ptr(detail::heap_new<detail::thread_data<typename boost::remove_reference<F>::type> >(
|
||||
boost::forward<F>(f)));
|
||||
}
|
||||
#endif
|
||||
static inline detail::thread_data_ptr make_thread_info(void (*f)())
|
||||
{
|
||||
return detail::thread_data_ptr(detail::heap_new<detail::thread_data<void(*)()> >(
|
||||
boost::forward<void(*)()>(f)));
|
||||
}
|
||||
#else
|
||||
template<typename F>
|
||||
static inline detail::thread_data_ptr make_thread_info(F f
|
||||
, typename disable_if_c<
|
||||
//boost::thread_detail::is_convertible<F&,BOOST_THREAD_RV_REF(F)>::value ||
|
||||
is_same<typename decay<F>::type, thread>::value,
|
||||
dummy* >::type=0
|
||||
)
|
||||
{
|
||||
return detail::thread_data_ptr(detail::heap_new<detail::thread_data<F> >(f));
|
||||
}
|
||||
template<typename F>
|
||||
static inline detail::thread_data_ptr make_thread_info(BOOST_THREAD_RV_REF(F) f)
|
||||
{
|
||||
return detail::thread_data_ptr(detail::heap_new<detail::thread_data<F> >(f));
|
||||
}
|
||||
|
||||
#endif
|
||||
public:
|
||||
#if 0 // This should not be needed anymore. Use instead BOOST_THREAD_MAKE_RV_REF.
|
||||
#if BOOST_WORKAROUND(__SUNPRO_CC, < 0x5100)
|
||||
thread(const volatile thread&);
|
||||
#endif
|
||||
#endif
|
||||
thread() BOOST_NOEXCEPT;
|
||||
~thread()
|
||||
{
|
||||
|
||||
#if defined BOOST_THREAD_PROVIDES_THREAD_DESTRUCTOR_CALLS_TERMINATE_IF_JOINABLE
|
||||
if (joinable()) {
|
||||
std::terminate();
|
||||
}
|
||||
#else
|
||||
detach();
|
||||
#endif
|
||||
}
|
||||
#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
|
||||
template <
|
||||
class F
|
||||
>
|
||||
explicit thread(BOOST_THREAD_RV_REF(F) f
|
||||
//, typename disable_if<is_same<typename decay<F>::type, thread>, dummy* >::type=0
|
||||
):
|
||||
thread_info(make_thread_info(thread_detail::decay_copy(boost::forward<F>(f))))
|
||||
{
|
||||
start_thread();
|
||||
}
|
||||
template <
|
||||
class F
|
||||
>
|
||||
thread(attributes const& attrs, BOOST_THREAD_RV_REF(F) f):
|
||||
thread_info(make_thread_info(thread_detail::decay_copy(boost::forward<F>(f))))
|
||||
{
|
||||
start_thread(attrs);
|
||||
}
|
||||
|
||||
#else
|
||||
#ifdef BOOST_NO_SFINAE
|
||||
template <class F>
|
||||
explicit thread(F f):
|
||||
thread_info(make_thread_info(f))
|
||||
{
|
||||
start_thread();
|
||||
}
|
||||
template <class F>
|
||||
thread(attributes const& attrs, F f):
|
||||
thread_info(make_thread_info(f))
|
||||
{
|
||||
start_thread(attrs);
|
||||
}
|
||||
#else
|
||||
template <class F>
|
||||
explicit thread(F f
|
||||
, typename disable_if_c<
|
||||
boost::thread_detail::is_convertible<F&,BOOST_THREAD_RV_REF(F)>::value
|
||||
//|| is_same<typename decay<F>::type, thread>::value
|
||||
, dummy* >::type=0
|
||||
):
|
||||
thread_info(make_thread_info(f))
|
||||
{
|
||||
start_thread();
|
||||
}
|
||||
template <class F>
|
||||
thread(attributes const& attrs, F f
|
||||
, typename disable_if<boost::thread_detail::is_convertible<F&,BOOST_THREAD_RV_REF(F) >, dummy* >::type=0
|
||||
):
|
||||
thread_info(make_thread_info(f))
|
||||
{
|
||||
start_thread(attrs);
|
||||
}
|
||||
#endif
|
||||
template <class F>
|
||||
explicit thread(BOOST_THREAD_RV_REF(F) f
|
||||
, typename disable_if<is_same<typename decay<F>::type, thread>, dummy* >::type=0
|
||||
):
|
||||
#ifdef BOOST_THREAD_USES_MOVE
|
||||
thread_info(make_thread_info(boost::move<F>(f))) // todo : Add forward
|
||||
#else
|
||||
thread_info(make_thread_info(f)) // todo : Add forward
|
||||
#endif
|
||||
{
|
||||
start_thread();
|
||||
}
|
||||
|
||||
template <class F>
|
||||
thread(attributes const& attrs, BOOST_THREAD_RV_REF(F) f):
|
||||
#ifdef BOOST_THREAD_USES_MOVE
|
||||
thread_info(make_thread_info(boost::move<F>(f))) // todo : Add forward
|
||||
#else
|
||||
thread_info(make_thread_info(f)) // todo : Add forward
|
||||
#endif
|
||||
{
|
||||
start_thread(attrs);
|
||||
}
|
||||
#endif
|
||||
thread(BOOST_THREAD_RV_REF(thread) x)
|
||||
{
|
||||
thread_info=BOOST_THREAD_RV(x).thread_info;
|
||||
BOOST_THREAD_RV(x).thread_info.reset();
|
||||
}
|
||||
#if 0 // This should not be needed anymore. Use instead BOOST_THREAD_MAKE_RV_REF.
|
||||
#if BOOST_WORKAROUND(__SUNPRO_CC, < 0x5100)
|
||||
thread& operator=(thread x)
|
||||
{
|
||||
swap(x);
|
||||
return *this;
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
thread& operator=(BOOST_THREAD_RV_REF(thread) other) BOOST_NOEXCEPT
|
||||
{
|
||||
|
||||
#if defined BOOST_THREAD_PROVIDES_THREAD_MOVE_ASSIGN_CALLS_TERMINATE_IF_JOINABLE
|
||||
if (joinable()) std::terminate();
|
||||
#endif
|
||||
thread_info=BOOST_THREAD_RV(other).thread_info;
|
||||
BOOST_THREAD_RV(other).thread_info.reset();
|
||||
return *this;
|
||||
}
|
||||
|
||||
#if defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
|
||||
template <class F, class Arg, class ...Args>
|
||||
thread(F&& f, Arg&& arg, Args&&... args) :
|
||||
thread_info(make_thread_info(
|
||||
thread_detail::decay_copy(boost::forward<F>(f)),
|
||||
thread_detail::decay_copy(boost::forward<Arg>(arg)),
|
||||
thread_detail::decay_copy(boost::forward<Args>(args))...)
|
||||
)
|
||||
|
||||
{
|
||||
start_thread();
|
||||
}
|
||||
template <class F, class Arg, class ...Args>
|
||||
thread(attributes const& attrs, F&& f, Arg&& arg, Args&&... args) :
|
||||
thread_info(make_thread_info(
|
||||
thread_detail::decay_copy(boost::forward<F>(f)),
|
||||
thread_detail::decay_copy(boost::forward<Arg>(arg)),
|
||||
thread_detail::decay_copy(boost::forward<Args>(args))...)
|
||||
)
|
||||
|
||||
{
|
||||
start_thread(attrs);
|
||||
}
|
||||
#else
|
||||
template <class F,class A1>
|
||||
thread(F f,A1 a1,typename disable_if<boost::thread_detail::is_convertible<F&,thread_attributes >, dummy* >::type=0):
|
||||
thread_info(make_thread_info(boost::bind(boost::type<void>(),f,a1)))
|
||||
{
|
||||
start_thread();
|
||||
}
|
||||
template <class F,class A1,class A2>
|
||||
thread(F f,A1 a1,A2 a2):
|
||||
thread_info(make_thread_info(boost::bind(boost::type<void>(),f,a1,a2)))
|
||||
{
|
||||
start_thread();
|
||||
}
|
||||
|
||||
template <class F,class A1,class A2,class A3>
|
||||
thread(F f,A1 a1,A2 a2,A3 a3):
|
||||
thread_info(make_thread_info(boost::bind(boost::type<void>(),f,a1,a2,a3)))
|
||||
{
|
||||
start_thread();
|
||||
}
|
||||
|
||||
template <class F,class A1,class A2,class A3,class A4>
|
||||
thread(F f,A1 a1,A2 a2,A3 a3,A4 a4):
|
||||
thread_info(make_thread_info(boost::bind(boost::type<void>(),f,a1,a2,a3,a4)))
|
||||
{
|
||||
start_thread();
|
||||
}
|
||||
|
||||
template <class F,class A1,class A2,class A3,class A4,class A5>
|
||||
thread(F f,A1 a1,A2 a2,A3 a3,A4 a4,A5 a5):
|
||||
thread_info(make_thread_info(boost::bind(boost::type<void>(),f,a1,a2,a3,a4,a5)))
|
||||
{
|
||||
start_thread();
|
||||
}
|
||||
|
||||
template <class F,class A1,class A2,class A3,class A4,class A5,class A6>
|
||||
thread(F f,A1 a1,A2 a2,A3 a3,A4 a4,A5 a5,A6 a6):
|
||||
thread_info(make_thread_info(boost::bind(boost::type<void>(),f,a1,a2,a3,a4,a5,a6)))
|
||||
{
|
||||
start_thread();
|
||||
}
|
||||
|
||||
template <class F,class A1,class A2,class A3,class A4,class A5,class A6,class A7>
|
||||
thread(F f,A1 a1,A2 a2,A3 a3,A4 a4,A5 a5,A6 a6,A7 a7):
|
||||
thread_info(make_thread_info(boost::bind(boost::type<void>(),f,a1,a2,a3,a4,a5,a6,a7)))
|
||||
{
|
||||
start_thread();
|
||||
}
|
||||
|
||||
template <class F,class A1,class A2,class A3,class A4,class A5,class A6,class A7,class A8>
|
||||
thread(F f,A1 a1,A2 a2,A3 a3,A4 a4,A5 a5,A6 a6,A7 a7,A8 a8):
|
||||
thread_info(make_thread_info(boost::bind(boost::type<void>(),f,a1,a2,a3,a4,a5,a6,a7,a8)))
|
||||
{
|
||||
start_thread();
|
||||
}
|
||||
|
||||
template <class F,class A1,class A2,class A3,class A4,class A5,class A6,class A7,class A8,class A9>
|
||||
thread(F f,A1 a1,A2 a2,A3 a3,A4 a4,A5 a5,A6 a6,A7 a7,A8 a8,A9 a9):
|
||||
thread_info(make_thread_info(boost::bind(boost::type<void>(),f,a1,a2,a3,a4,a5,a6,a7,a8,a9)))
|
||||
{
|
||||
start_thread();
|
||||
}
|
||||
#endif
|
||||
void swap(thread& x) BOOST_NOEXCEPT
|
||||
{
|
||||
thread_info.swap(x.thread_info);
|
||||
}
|
||||
|
||||
class id;
|
||||
#ifdef BOOST_THREAD_PLATFORM_PTHREAD
|
||||
inline id get_id() const BOOST_NOEXCEPT;
|
||||
#else
|
||||
id get_id() const BOOST_NOEXCEPT;
|
||||
#endif
|
||||
|
||||
|
||||
bool joinable() const BOOST_NOEXCEPT;
|
||||
private:
|
||||
bool join_noexcept();
|
||||
public:
|
||||
inline void join();
|
||||
|
||||
#ifdef BOOST_THREAD_USES_CHRONO
|
||||
template <class Rep, class Period>
|
||||
bool try_join_for(const chrono::duration<Rep, Period>& rel_time)
|
||||
{
|
||||
return try_join_until(chrono::steady_clock::now() + rel_time);
|
||||
}
|
||||
template <class Clock, class Duration>
|
||||
bool try_join_until(const chrono::time_point<Clock, Duration>& t)
|
||||
{
|
||||
using namespace chrono;
|
||||
system_clock::time_point s_now = system_clock::now();
|
||||
typename Clock::time_point c_now = Clock::now();
|
||||
return try_join_until(s_now + ceil<nanoseconds>(t - c_now));
|
||||
}
|
||||
template <class Duration>
|
||||
bool try_join_until(const chrono::time_point<chrono::system_clock, Duration>& t)
|
||||
{
|
||||
using namespace chrono;
|
||||
typedef time_point<system_clock, nanoseconds> nano_sys_tmpt;
|
||||
return try_join_until(nano_sys_tmpt(ceil<nanoseconds>(t.time_since_epoch())));
|
||||
}
|
||||
#endif
|
||||
#if defined(BOOST_THREAD_PLATFORM_WIN32)
|
||||
private:
|
||||
bool do_try_join_until_noexcept(uintmax_t milli, bool& res);
|
||||
inline bool do_try_join_until(uintmax_t milli);
|
||||
public:
|
||||
bool timed_join(const system_time& abs_time);
|
||||
//{
|
||||
// return do_try_join_until(get_milliseconds_until(wait_until));
|
||||
//}
|
||||
|
||||
#ifdef BOOST_THREAD_USES_CHRONO
|
||||
bool try_join_until(const chrono::time_point<chrono::system_clock, chrono::nanoseconds>& tp)
|
||||
{
|
||||
chrono::milliseconds rel_time= chrono::ceil<chrono::milliseconds>(tp-chrono::system_clock::now());
|
||||
return do_try_join_until(rel_time.count());
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#else
|
||||
private:
|
||||
bool do_try_join_until_noexcept(struct timespec const &timeout, bool& res);
|
||||
inline bool do_try_join_until(struct timespec const &timeout);
|
||||
public:
|
||||
#if defined BOOST_THREAD_USES_DATETIME
|
||||
bool timed_join(const system_time& abs_time)
|
||||
{
|
||||
struct timespec const ts=detail::to_timespec(abs_time);
|
||||
return do_try_join_until(ts);
|
||||
}
|
||||
#endif
|
||||
#ifdef BOOST_THREAD_USES_CHRONO
|
||||
bool try_join_until(const chrono::time_point<chrono::system_clock, chrono::nanoseconds>& tp)
|
||||
{
|
||||
using namespace chrono;
|
||||
nanoseconds d = tp.time_since_epoch();
|
||||
timespec ts = boost::detail::to_timespec(d);
|
||||
return do_try_join_until(ts);
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
public:
|
||||
|
||||
#if defined BOOST_THREAD_USES_DATETIME
|
||||
template<typename TimeDuration>
|
||||
inline bool timed_join(TimeDuration const& rel_time)
|
||||
{
|
||||
return timed_join(get_system_time()+rel_time);
|
||||
}
|
||||
#endif
|
||||
void detach();
|
||||
|
||||
static unsigned hardware_concurrency() BOOST_NOEXCEPT;
|
||||
|
||||
#define BOOST_THREAD_DEFINES_THREAD_NATIVE_HANDLE
|
||||
typedef detail::thread_data_base::native_handle_type native_handle_type;
|
||||
native_handle_type native_handle();
|
||||
|
||||
#if defined BOOST_THREAD_PROVIDES_THREAD_EQ
|
||||
// Use thread::id when comparisions are needed
|
||||
// backwards compatibility
|
||||
bool operator==(const thread& other) const;
|
||||
bool operator!=(const thread& other) const;
|
||||
#endif
|
||||
#if defined BOOST_THREAD_USES_DATETIME
|
||||
static inline void yield() BOOST_NOEXCEPT
|
||||
{
|
||||
this_thread::yield();
|
||||
}
|
||||
|
||||
static inline void sleep(const system_time& xt)
|
||||
{
|
||||
this_thread::sleep(xt);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
|
||||
// extensions
|
||||
void interrupt();
|
||||
bool interruption_requested() const BOOST_NOEXCEPT;
|
||||
#endif
|
||||
};
|
||||
|
||||
inline void swap(thread& lhs,thread& rhs) BOOST_NOEXCEPT
|
||||
{
|
||||
return lhs.swap(rhs);
|
||||
}
|
||||
|
||||
#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
|
||||
inline thread&& move(thread& t) BOOST_NOEXCEPT
|
||||
{
|
||||
return static_cast<thread&&>(t);
|
||||
}
|
||||
#endif
|
||||
|
||||
BOOST_THREAD_DCL_MOVABLE(thread)
|
||||
|
||||
namespace this_thread
|
||||
{
|
||||
#ifdef BOOST_THREAD_PLATFORM_PTHREAD
|
||||
inline thread::id get_id() BOOST_NOEXCEPT;
|
||||
#else
|
||||
thread::id BOOST_THREAD_DECL get_id() BOOST_NOEXCEPT;
|
||||
#endif
|
||||
|
||||
#if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
|
||||
void BOOST_THREAD_DECL interruption_point();
|
||||
bool BOOST_THREAD_DECL interruption_enabled() BOOST_NOEXCEPT;
|
||||
bool BOOST_THREAD_DECL interruption_requested() BOOST_NOEXCEPT;
|
||||
#endif
|
||||
|
||||
#if defined BOOST_THREAD_USES_DATETIME
|
||||
inline BOOST_SYMBOL_VISIBLE void sleep(xtime const& abs_time)
|
||||
{
|
||||
sleep(system_time(abs_time));
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
class BOOST_SYMBOL_VISIBLE thread::id
|
||||
{
|
||||
private:
|
||||
friend inline
|
||||
std::size_t
|
||||
hash_value(const thread::id &v)
|
||||
{
|
||||
#if defined BOOST_THREAD_PROVIDES_BASIC_THREAD_ID
|
||||
return hash_value(v.thread_data);
|
||||
#else
|
||||
return hash_value(v.thread_data.get());
|
||||
#endif
|
||||
}
|
||||
|
||||
#if defined BOOST_THREAD_PROVIDES_BASIC_THREAD_ID
|
||||
#if defined(BOOST_THREAD_PLATFORM_WIN32)
|
||||
typedef unsigned int data;
|
||||
#else
|
||||
typedef thread::native_handle_type data;
|
||||
#endif
|
||||
#else
|
||||
typedef detail::thread_data_ptr data;
|
||||
#endif
|
||||
data thread_data;
|
||||
|
||||
id(data thread_data_):
|
||||
thread_data(thread_data_)
|
||||
{}
|
||||
friend class thread;
|
||||
friend id BOOST_THREAD_DECL this_thread::get_id() BOOST_NOEXCEPT;
|
||||
public:
|
||||
id() BOOST_NOEXCEPT:
|
||||
#if defined BOOST_THREAD_PROVIDES_BASIC_THREAD_ID
|
||||
thread_data(0)
|
||||
#else
|
||||
thread_data()
|
||||
#endif
|
||||
{}
|
||||
|
||||
id(const id& other) BOOST_NOEXCEPT :
|
||||
thread_data(other.thread_data)
|
||||
{}
|
||||
|
||||
bool operator==(const id& y) const BOOST_NOEXCEPT
|
||||
{
|
||||
return thread_data==y.thread_data;
|
||||
}
|
||||
|
||||
bool operator!=(const id& y) const BOOST_NOEXCEPT
|
||||
{
|
||||
return thread_data!=y.thread_data;
|
||||
}
|
||||
|
||||
bool operator<(const id& y) const BOOST_NOEXCEPT
|
||||
{
|
||||
return thread_data<y.thread_data;
|
||||
}
|
||||
|
||||
bool operator>(const id& y) const BOOST_NOEXCEPT
|
||||
{
|
||||
return y.thread_data<thread_data;
|
||||
}
|
||||
|
||||
bool operator<=(const id& y) const BOOST_NOEXCEPT
|
||||
{
|
||||
return !(y.thread_data<thread_data);
|
||||
}
|
||||
|
||||
bool operator>=(const id& y) const BOOST_NOEXCEPT
|
||||
{
|
||||
return !(thread_data<y.thread_data);
|
||||
}
|
||||
|
||||
#ifndef BOOST_NO_IOSTREAM
|
||||
#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
|
||||
template<class charT, class traits>
|
||||
friend BOOST_SYMBOL_VISIBLE
|
||||
std::basic_ostream<charT, traits>&
|
||||
operator<<(std::basic_ostream<charT, traits>& os, const id& x)
|
||||
{
|
||||
if(x.thread_data)
|
||||
{
|
||||
io::ios_flags_saver ifs( os );
|
||||
return os<< std::hex << x.thread_data;
|
||||
}
|
||||
else
|
||||
{
|
||||
return os<<"{Not-any-thread}";
|
||||
}
|
||||
}
|
||||
#else
|
||||
template<class charT, class traits>
|
||||
BOOST_SYMBOL_VISIBLE
|
||||
std::basic_ostream<charT, traits>&
|
||||
print(std::basic_ostream<charT, traits>& os) const
|
||||
{
|
||||
if(thread_data)
|
||||
{
|
||||
io::ios_flags_saver ifs( os );
|
||||
return os<< std::hex << thread_data;
|
||||
}
|
||||
else
|
||||
{
|
||||
return os<<"{Not-any-thread}";
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
#endif
|
||||
};
|
||||
|
||||
#ifdef BOOST_THREAD_PLATFORM_PTHREAD
|
||||
thread::id thread::get_id() const BOOST_NOEXCEPT
|
||||
{
|
||||
#if defined BOOST_THREAD_PROVIDES_BASIC_THREAD_ID
|
||||
return const_cast<thread*>(this)->native_handle();
|
||||
#else
|
||||
detail::thread_data_ptr const local_thread_info=(get_thread_info)();
|
||||
return (local_thread_info? id(local_thread_info) : id());
|
||||
#endif
|
||||
}
|
||||
|
||||
namespace this_thread
|
||||
{
|
||||
inline thread::id get_id() BOOST_NOEXCEPT
|
||||
{
|
||||
#if defined BOOST_THREAD_PROVIDES_BASIC_THREAD_ID
|
||||
return pthread_self();
|
||||
#else
|
||||
boost::detail::thread_data_base* const thread_info=get_or_make_current_thread_data();
|
||||
return (thread_info?thread::id(thread_info->shared_from_this()):thread::id());
|
||||
#endif
|
||||
}
|
||||
}
|
||||
#endif
|
||||
void thread::join() {
|
||||
if (this_thread::get_id() == get_id())
|
||||
boost::throw_exception(thread_resource_error(system::errc::resource_deadlock_would_occur, "boost thread: trying joining itself"));
|
||||
|
||||
BOOST_THREAD_VERIFY_PRECONDITION( join_noexcept(),
|
||||
thread_resource_error(system::errc::invalid_argument, "boost thread: thread not joinable")
|
||||
);
|
||||
}
|
||||
|
||||
#ifdef BOOST_THREAD_PLATFORM_PTHREAD
|
||||
bool thread::do_try_join_until(struct timespec const &timeout)
|
||||
#else
|
||||
bool thread::do_try_join_until(uintmax_t timeout)
|
||||
#endif
|
||||
{
|
||||
if (this_thread::get_id() == get_id())
|
||||
boost::throw_exception(thread_resource_error(system::errc::resource_deadlock_would_occur, "boost thread: trying joining itself"));
|
||||
bool res;
|
||||
if (do_try_join_until_noexcept(timeout, res))
|
||||
{
|
||||
return res;
|
||||
}
|
||||
else
|
||||
{
|
||||
BOOST_THREAD_THROW_ELSE_RETURN(
|
||||
(thread_resource_error(system::errc::invalid_argument, "boost thread: thread not joinable")),
|
||||
false
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#if !defined(BOOST_NO_IOSTREAM) && defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
|
||||
template<class charT, class traits>
|
||||
BOOST_SYMBOL_VISIBLE
|
||||
std::basic_ostream<charT, traits>&
|
||||
operator<<(std::basic_ostream<charT, traits>& os, const thread::id& x)
|
||||
{
|
||||
return x.print(os);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined BOOST_THREAD_PROVIDES_THREAD_EQ
|
||||
inline bool thread::operator==(const thread& other) const
|
||||
{
|
||||
return get_id()==other.get_id();
|
||||
}
|
||||
|
||||
inline bool thread::operator!=(const thread& other) const
|
||||
{
|
||||
return get_id()!=other.get_id();
|
||||
}
|
||||
#endif
|
||||
|
||||
namespace detail
|
||||
{
|
||||
struct thread_exit_function_base
|
||||
{
|
||||
virtual ~thread_exit_function_base()
|
||||
{}
|
||||
virtual void operator()()=0;
|
||||
};
|
||||
|
||||
template<typename F>
|
||||
struct thread_exit_function:
|
||||
thread_exit_function_base
|
||||
{
|
||||
F f;
|
||||
|
||||
thread_exit_function(F f_):
|
||||
f(f_)
|
||||
{}
|
||||
|
||||
void operator()()
|
||||
{
|
||||
f();
|
||||
}
|
||||
};
|
||||
|
||||
void BOOST_THREAD_DECL add_thread_exit_function(thread_exit_function_base*);
|
||||
}
|
||||
|
||||
namespace this_thread
|
||||
{
|
||||
template<typename F>
|
||||
void at_thread_exit(F f)
|
||||
{
|
||||
detail::thread_exit_function_base* const thread_exit_func=detail::heap_new<detail::thread_exit_function<F> >(f);
|
||||
detail::add_thread_exit_function(thread_exit_func);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef BOOST_MSVC
|
||||
#pragma warning(pop)
|
||||
#endif
|
||||
|
||||
#include <boost/config/abi_suffix.hpp>
|
||||
|
||||
#endif
|
||||
154
include/boost/thread/detail/thread_group.hpp
Normal file
154
include/boost/thread/detail/thread_group.hpp
Normal file
@@ -0,0 +1,154 @@
|
||||
#ifndef BOOST_THREAD_DETAIL_THREAD_GROUP_HPP
|
||||
#define BOOST_THREAD_DETAIL_THREAD_GROUP_HPP
|
||||
// Distributed under the Boost Software License, Version 1.0. (See
|
||||
// accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
// (C) Copyright 2007-9 Anthony Williams
|
||||
|
||||
#include <list>
|
||||
#include <boost/thread/shared_mutex.hpp>
|
||||
#include <boost/thread/mutex.hpp>
|
||||
#include <boost/thread/lock_guard.hpp>
|
||||
|
||||
#include <boost/config/abi_prefix.hpp>
|
||||
|
||||
#ifdef BOOST_MSVC
|
||||
#pragma warning(push)
|
||||
#pragma warning(disable:4251)
|
||||
#endif
|
||||
|
||||
namespace boost
|
||||
{
|
||||
class thread_group
|
||||
{
|
||||
private:
|
||||
thread_group(thread_group const&);
|
||||
thread_group& operator=(thread_group const&);
|
||||
public:
|
||||
thread_group() {}
|
||||
~thread_group()
|
||||
{
|
||||
for(std::list<thread*>::iterator it=threads.begin(),end=threads.end();
|
||||
it!=end;
|
||||
++it)
|
||||
{
|
||||
delete *it;
|
||||
}
|
||||
}
|
||||
|
||||
bool is_this_thread_in()
|
||||
{
|
||||
thread::id id = this_thread::get_id();
|
||||
boost::shared_lock<shared_mutex> guard(m);
|
||||
for(std::list<thread*>::iterator it=threads.begin(),end=threads.end();
|
||||
it!=end;
|
||||
++it)
|
||||
{
|
||||
if ((*it)->get_id() == id)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool is_thread_in(thread* thrd)
|
||||
{
|
||||
if(thrd)
|
||||
{
|
||||
thread::id id = thrd->get_id();
|
||||
boost::shared_lock<shared_mutex> guard(m);
|
||||
for(std::list<thread*>::iterator it=threads.begin(),end=threads.end();
|
||||
it!=end;
|
||||
++it)
|
||||
{
|
||||
if ((*it)->get_id() == id)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
template<typename F>
|
||||
thread* create_thread(F threadfunc)
|
||||
{
|
||||
boost::lock_guard<shared_mutex> guard(m);
|
||||
std::auto_ptr<thread> new_thread(new thread(threadfunc));
|
||||
threads.push_back(new_thread.get());
|
||||
return new_thread.release();
|
||||
}
|
||||
|
||||
void add_thread(thread* thrd)
|
||||
{
|
||||
if(thrd)
|
||||
{
|
||||
BOOST_THREAD_ASSERT_PRECONDITION( ! is_thread_in(thrd) ,
|
||||
thread_resource_error(system::errc::resource_deadlock_would_occur, "boost::thread_group: trying to add a duplicated thread")
|
||||
);
|
||||
|
||||
boost::lock_guard<shared_mutex> guard(m);
|
||||
threads.push_back(thrd);
|
||||
}
|
||||
}
|
||||
|
||||
void remove_thread(thread* thrd)
|
||||
{
|
||||
boost::lock_guard<shared_mutex> guard(m);
|
||||
std::list<thread*>::iterator const it=std::find(threads.begin(),threads.end(),thrd);
|
||||
if(it!=threads.end())
|
||||
{
|
||||
threads.erase(it);
|
||||
}
|
||||
}
|
||||
|
||||
void join_all()
|
||||
{
|
||||
BOOST_THREAD_ASSERT_PRECONDITION( ! is_this_thread_in() ,
|
||||
thread_resource_error(system::errc::resource_deadlock_would_occur, "boost::thread_group: trying joining itself")
|
||||
);
|
||||
boost::shared_lock<shared_mutex> guard(m);
|
||||
|
||||
for(std::list<thread*>::iterator it=threads.begin(),end=threads.end();
|
||||
it!=end;
|
||||
++it)
|
||||
{
|
||||
if ((*it)->joinable())
|
||||
(*it)->join();
|
||||
}
|
||||
}
|
||||
|
||||
#if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
|
||||
void interrupt_all()
|
||||
{
|
||||
boost::shared_lock<shared_mutex> guard(m);
|
||||
|
||||
for(std::list<thread*>::iterator it=threads.begin(),end=threads.end();
|
||||
it!=end;
|
||||
++it)
|
||||
{
|
||||
(*it)->interrupt();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
size_t size() const
|
||||
{
|
||||
boost::shared_lock<shared_mutex> guard(m);
|
||||
return threads.size();
|
||||
}
|
||||
|
||||
private:
|
||||
std::list<thread*> threads;
|
||||
mutable shared_mutex m;
|
||||
};
|
||||
}
|
||||
|
||||
#ifdef BOOST_MSVC
|
||||
#pragma warning(pop)
|
||||
#endif
|
||||
|
||||
#include <boost/config/abi_suffix.hpp>
|
||||
|
||||
#endif
|
||||
23
include/boost/thread/detail/thread_heap_alloc.hpp
Normal file
23
include/boost/thread/detail/thread_heap_alloc.hpp
Normal file
@@ -0,0 +1,23 @@
|
||||
#ifndef BOOST_THREAD_THREAD_HEAP_ALLOC_HPP
|
||||
#define BOOST_THREAD_THREAD_HEAP_ALLOC_HPP
|
||||
|
||||
// thread_heap_alloc.hpp
|
||||
//
|
||||
// (C) Copyright 2008 Anthony Williams
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See
|
||||
// accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#include <boost/thread/detail/platform.hpp>
|
||||
|
||||
#if defined(BOOST_THREAD_PLATFORM_WIN32)
|
||||
#include <boost/thread/win32/thread_heap_alloc.hpp>
|
||||
#elif defined(BOOST_THREAD_PLATFORM_PTHREAD)
|
||||
#include <boost/thread/pthread/thread_heap_alloc.hpp>
|
||||
#else
|
||||
#error "Boost threads unavailable on this platform"
|
||||
#endif
|
||||
|
||||
|
||||
#endif
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user