| 56 | 4. Next, define a new event type for you in your {{{.cpp}}} file. This event will be scheduled into the !SystemQueue: |
| 57 | |
| 58 | {{{ |
| 59 | #!cpp |
| 60 | SystemEventType MyEventType("My new event type!"); |
| 61 | }}} |
| 62 | |
| 63 | 5. In your functionality where you want to synchronized yourself into the main Ariba thread using the !SystemQueue, create the event and insert it. You can attach additional information as a {{{void*}}} to the event that you can extract (here, called {{{myInfo}}}), once in the event handler function: |
| 64 | |
| 65 | {{{ |
| 66 | #!cpp |
| 67 | SystemQueue::instance().scheduleEvent( |
| 68 | SystemEvent( this, MyEventType, myInfo)); |
| 69 | }}} |
| 70 | |
| 71 | 6. Your event handler will be called asynchronously and in sync with the Ariba main thread. You can also extract the additional information ({{{myInfo}}}) from the event. In this handler function you are the only event that is currently executed. Therefore, you can be sure that no other code in executing in parallel and need not to take any care of threading issues or mutexes. |
| 72 | |
| 73 | {{{ |
| 74 | #!cpp |
| 75 | void MyClass::handleSystemEvent(const SystemEvent& event){ |
| 76 | MyInfo* info = event.getData<MyInfo>(); |
| 77 | .... |
| 78 | } |
| 79 | }}} |
| 90 | 1. Include the header and using directives for the {{{BlockingMethod}}} in your {{{.h}}} file: |
| 91 | |
| 92 | {{{ |
| 93 | #!cpp |
| 94 | #include spovnet/common/BlockingMethod.h" |
| 95 | using spovnet::common::BlockingMethod; |
| 96 | }}} |
| 97 | |
| 98 | 2. Derive a new class from {{{BlockingMethod}}}: |
| 99 | |
| 100 | {{{ |
| 101 | #!cpp |
| 102 | class MyClass : public BlockingMethod { |
| 103 | }}} |
| 104 | |
| 105 | 3. Declare the pure virtual function from {{{BlockingMethod}}} in your new class: |
| 106 | |
| 107 | {{{ |
| 108 | #!cpp |
| 109 | virtual void dispatchFunction(); |
| 110 | virtual void blockingFunction(); |
| 111 | }}} |
| 112 | |
| 113 | 4. Finally, implement those two functions in your {{{.cpp}}} file |
| 114 | |
| 115 | {{{ |
| 116 | #!cpp |
| 117 | void MyClass::dispatchFunction(){ |
| 118 | ... |
| 119 | } |
| 120 | |
| 121 | void MyClass::blockingFunction(){ |
| 122 | ... |
| 123 | } |
| 124 | }}} |
| 125 | |
| 126 | 5. Write the code that blocks execution into the {{{blockingFunction}}}. Implement code that you want to execute once synchronized with the main thread after the blocking into the {{{dispatchFunction}}}. In the {{{dispatchFunction}}} you can safely call other modules etc. from Ariba, because this function will be executed by the !SystemQueue once in sync with the main thread! |
| 127 | |
| 128 | 6. From your {{{blockingFunction}}} you can then call {{{MyClass::dispatch}}} to get synchronized back into the main thread once your blocking functionality is done. This will result in {{{MyClass::dispatchFunction}}} being called asynchronously and in sync with the main thread. |
| 129 | |
| 130 | The mechanism works by running the blocking functionality automatically in a new thread, therefore not blocking execution of the main Ariba thread. Once you call {{{dispatch}}} an event is generated that results in calling {{{dispatchFunction}}} from the main thread. |