Cette version semble bien marcher
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59 class semaphore { public: typedef semaphore self; typedef scoped_lock<self> scoped_lock; typedef condition_mutex mutex_type; private: int count; int waiting; mutex_type cond; semaphore(const self &); const self &operator=(const self &); public: inline semaphore( ) { count=0; waiting=0; } inline semaphore(int n) { count=n; waiting=0; } inline ~semaphore() {} inline void release() { mutex_type::scoped_lock lock(cond); ++count; if (waiting>0) cond.signal(); } inline void release(int n) { for (int i=0; i<n; ++i) release(); } inline void acquire() { mutex_type::scoped_lock lock(cond); ++waiting; while (count==0) cond.wait(); --waiting; --count; } inline void acquire(int n) { mutex_type::scoped_lock lock(cond); waiting+=n; while (count< n) cond.wait(); waiting-=n; count-=n; } inline int try_acquire() { mutex_type::scoped_lock lock(cond); if (count>0) { --count; return 1; } else return 0; } inline int try_acquire(int n) { mutex_type::scoped_lock lock(cond); if (count>=n) count-=n; else { n=count; count=0; } return n; } };
Partager