3.7 KB


Threads are great, coding it is not so much fun. Well, at least not writing it by hand. So by doing a YASnippet that do the boring work it is possible to focus on more fun things.

This snippet will create a skeleton class for a message pump. A message pump is a thread-based system that together with a queue receives a call and adds a (function) object to the queue, when the thread is ready it pulls the message and executes the object. This means that every call to the interface will be asynchronous, and returns immediately. The execution of the call will be based on when and where the thread is located in the queue. Its possible to add priority to the queue, but this is not done in this implementation. As mentioned before it is just a skeleton, its up to the user to actually do the fun stuff of creating what is needed.

Extra functionality

Since the msgPump snippet creates a skeleton for the -hpp file it would be nice to actually not doing the implementation by hand. for that reason there is a function called (M-x)col/implement-functions that will create a .cpp file with all .hpp defined functions. Though one needs to include the hpp file by hand. Here is en example

1  # emacs MsgPumpTest.hpp
2  msgPump<TAB>

Will produce the skeleton

 1  class MsgPumpTest
 2  {
 3  public:
 4    MsgPumpTest(){}
 5    ~MsgPumpTest(){}
 7    typedef std::function< bool () > FunT;
 8    typedef Util::Queue< FunT > FunctionMsgQueueT;
 9    typedef std::shared_ptr< std::thread > ThreadPtrT;
11    void startSystem();
13    void stopSystem();
15    ThreadPtrT getThread() const;
18  private:
19    void run();
20    ThreadPtrT itsThread_;
21    FunctionMsgQueueT itsQueue_;
22  };

This example is excluding the doxygen comments for readability. We can now add a new function e.g

 1  .
 2  .
 3  .
 5    void printOut(const string& str);
 6  private:
 7    void handlePrintOut( const string& str) const;
 8  .
 9  .
10  .

and use the function (M-x)col/implement-functions. This will produce the .cpp file skeleton (don't forget to include MsgPumptest.hpp). There is a small bug!! In the getThread method, the return is a ThreadPrtT. This should actually be MsgPumpTest::ThreadPrtT.

Using more msgPump-snippets

Now we can start with the implementation.

Lets start with the startSystem go inside the implementation area and type


This will create the implementation for Start method. The same thing can be done for run and stop. Following snippets exists.

  • msgPump: Create skeleton for Message pump class
  • msgPumpStart: Creates implementation for start method
  • msgPumpRun: Creates implementation for Run method
  • msgPumpStop: Creates implemntation for stop method
  • msgPumpMsg: a versatile implemntation of message to the queue

One note, if the default names for the queue and thread are not used, these needs to be changed by hand during the above snippets.

Getting the Queue

Since the queue is basically the same for all, i've added the file in Queue. Feel free to use it.