csp::ScopedForking Class Reference
[Running ProcessesScoped Classes]

Inheritance diagram for csp::ScopedForking:

boost::noncopyable List of all members.

Detailed Description

A class used to fork processes.

Whereas the Run() call runs some processes and waits for them to complete, forking starts some processes but lets the original process continue. Synchronization between the original process and the forked processes finishing is carried out later on. One of the main reasons for this synchronization is that the forked processes usually use channels or other resources provided by the original (parent) process. Therefore the original process should not finish (which would destroy its resources) until the forked processes have themselves completed.

ScopedForking ensures that this final synchronization is always done, even in the case of an exception being thrown. It is used as follows:

        {
            ScopedForking forking;
            
            forking.forkProcess(new ProcessA);
            
            // ... some other code ...
            
            forking.forkProcess(new ProcessB);
            
            // ... some other code ...
        } //end of block

At the end of the above block of code, ScopedForking goes out of scope. At this point, the object will be destroyed, which will cause the synchronization with the sub-processes (ProcessA and ProcessB) to be completed.

This method has advantages and disadvantages. The advantage is that it ensures that this synchronization will happen. The disadvantage is that it hides the occurrence of this synchronization. Looking at that sole brace at the end of the block, it is not clear that it involves the process making a blocking call.

You must make sure that all resources (particularly channels and barriers) remain in scoped until the ScopedForking object has been destroyed. Consider the following:

    {
        ScopedForking forking;
        One2OneChannel<int> c,d; //BAD!
        // ... some other code ...
        forking.fork(new Id<int>(c.reader(),d.writer());
        // ... some other code ...
    }

The above code is very dangerous. Objects in C++ are destroyed in the reverse order of their construction. Therefore in the above code, the channels will be destroyed before the ScopedForking object. So at the end of the block the channels (which are being used by the sub-processes) are destroyed, and then we wait for the (now endangered) sub-processes to finish. Make sure you declare all used channels and barriers before the ScopedForking object.

This process should always be used as a stack variable. Using it as a class member variable may have hazardous consequences - if the destructor of the class is called by a process other than the original process, undefined behaviour will ensue. In particular, never use this as a member variable in a CSProcess implementation. Similarly, if you ever find a piece of code that holds a pointer to this class, you are almost certainly using it wrongly or inappropriately.

More information can be found on the Scoped Classes page, the Running Processes page and in the Forking Processes section of the guide.


Public Member Functions

void fork (ThreadCSProcessPtr)
 Variants of fork() are provided such that fork() can be used in any manner that Run() can be.
void forkInThisThread (CSProcessPtr)
 Variants of forkInThisThread() are provided such that forkInThisThread() can be used in any manner that RunInThisThread() can be.
 ScopedForking ()
 The default constructor.
 ~ScopedForking ()
 The destructor.


Constructor & Destructor Documentation

csp::ScopedForking::ScopedForking (  )  [inline]

The default constructor.

csp::ScopedForking::~ScopedForking (  )  [inline]

The destructor.

The destruction of this object will cause a synchronization with all the forked processes. That is, the destruction of this object will block the process until all its forked processes have finished.


Member Function Documentation

void csp::ScopedForking::forkInThisThread ( CSProcessPtr   ) 

Variants of forkInThisThread() are provided such that forkInThisThread() can be used in any manner that RunInThisThread() can be.

However, whereas RunInThisThread() does not return until the processes have all finished, forkInThisThread() returns after starting the processes.

void csp::ScopedForking::fork ( ThreadCSProcessPtr   ) 

Variants of fork() are provided such that fork() can be used in any manner that Run() can be.

However, whereas Run() does not return until the processes have all finished, fork() returns after starting the processes.


Generated on Mon Aug 20 12:24:29 2007 for C++CSP2 by  doxygen 1.4.7