summaryrefslogtreecommitdiff
path: root/docs/tutorials/017/Barrier_i.h
blob: 49c862cfd3d74f5b9e566f750dd665712d39ce1a (plain)
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

// $Id$

#ifndef BARRIER_H
#define BARRIER_H

#include "ace/Synch.h"

/* Barrier is a simple wrapper for the ACE_Barrier synchronization
   class.  The ACE_Barrier is already pretty easy to use but I thought 
   I'd wrap it up to create just a bit more abstraction at the
   application level.
 */
class Barrier
{
public:
        // Basic constructor and destructor.  If you only need to
        // synch the start of your threads, you can safely delete your 
        // Barrier object after invoking done().  Of course, you
        // should be careful to only delete the object once!
    Barrier(void);
    ~Barrier(void);

        // Set and get the number of threads that the barrier will
        // manage.  If you add or remove threads to your application
        // at run-time you can use the mutator to reflect that
        // change.  Note, however, that you can only do that from the
        // thread which first created the Barrier.  (This is a
        // limitation of my Barrier object, not the ACE_Barrier.)
        // The optional _wait parameter will cause wait() to be
        // invoked if there is already a valid threads value.
    int threads( u_int _threads, int _wait = 0);
    u_int threads(void);

        // Wait for all threads to reach the point where this is
        // invoked.  Because of the snappy way in which ACE_Barrier is 
        // implemented, you can invoke these back-to-back with no ill-effects.
    int wait(void);

        // done() will invoke wait().  Before returning though, it
        // will delete the barrier_ pointer below to reclaim some memory.
    int done(void);
    
protected:
        // The number of threads we're synching
    ACE_Atomic_Op<ACE_Mutex,u_int> threads_;

        // The ACE_Barrier that does all of the work
    ACE_Barrier * barrier_;

        // The thread which created the Barrier in the first place.
        // Only this thread can change the threads_ value.
    ACE_thread_t  owner_;

        // An internal method that constructs the barrier_ as needed.
    int make_barrier( int _wait );
};
    
#endif // BARRIER_H