C++ GPC API Reference

The gpc::qumex<class DataType> and gpc::qumex<class KeyType, class DataType> classes as well as the gpc::fork<class Function> function constitute Generic Parallel Computing (GPC) API. See also Reduction/MapReduce.

template<class F>
void gpc::fork(F&& function);
Submit the function to the thread pool. A thread from the pool will call the function. Submitted functions are ordered FIFO.
template<class F>
void gpc::qumex::join(F&& function);
Submit the function to the queue of mutual exclusion. A thread from the thread pool will call the function. At any moment of time only one thread can process a queue of mutual exclusion, but at different times different threads can process the same queue. Submitted functions are ordered FIFO.
template<class F>
void gpc::qumex::join(const KeyType& key, F&& function);
Submit the function to the queue of mutual exclusion identified by the key. A thread from the thread pool will call the function. At any moment of time only one thread can process a queue of mutual exclusion, but at different times different threads can process the same queue. Submitted functions are ordered FIFO.
gpc::mutex<class DataType>
gpc::mutex<class DataType, class KeyType>
Arguments of the join callback functions.

Example #1 (parallel database queries):

...

// an HTTP entry
void SomeHttpHandler::Process(tef::RequestContext& http) const
{
    gpc::qumex<Foo*> qmx;

    while( /* condition */ ) 
    {
        // do parallel database queries
        // while an HTTP context is referenced, the response is not sent.
        gpc::fork([http, qmx]() 
        {
            try 
            {
                struct Record
                {
                    tef::db::type<__int64> uid;
                    tef::db::type<wchar_t, 30> usr;
                    ...
                };
                tef::Db<Record> db(L"ms");
                db << L"select UID, USR from ROOT where ...";
                db >> db->uid >> db->usr ... ;
                
                Foo* data;
                while( db.Fetch() ) 
                {
                    // prepare some data
                }

                // merge the results and respond when it is done
                qmx.join([http, data](gpc::mutex<Foo*>& state) mutable 
                {
                    // exclusive access to the state, so let's merge and set
                    ...
                    state.setData(data);
                    data.count++;
                    
                    // exclusive access to the HTTP context also
                    if( data.count == /* condition */ ) 
                    {
                        http.Output << state.getData();
                        http.Output.End(200);
                    }
                }
            } catch(std::exception& e) 
            {
                qmx.join([http, e](gpc::mutex<Foo*>& state) mutable 
                {
                    // exclusive access to the HTTP context
                    http.Output << e.what();
                    http.Output.End(500); // it sends the response when there are no references to the HTTP context.
                }                
            }
        });
    }
}
        

Example #2 (concurrency control):

// in the global scope
gpc::qumex<int, Foo*> gQmx;

// an HTTP entry
void SomeHttpHandler::Process(tef::RequestContext& http) const
{
    int someId = http.Input["someId"];

    gQmx.join(someId, [http](gpc::mutex<Foo*, int>& state) mutable 
    {
        // exclusive access to the state identified by the someId
        ...
                    
        // then
        http.Output << 123;
        // it sends the response (200) automatically when there are no references to the HTTP context.
    }
}
        

See Also:

C++ API Reference

Table of Contents