title: Synchronous and ASynchronous APIs
body: <p>I am developing a library, which provides some time consuming services. I need to have two versions of each API, one for synchronous function call and the other for asynchronous.</p>
  
  <p>Library user should decide which version to use, a service result might be crucial for continue of system operation (synch call). The same operation might be needed to be done in different worker thread as it result is not needed to continue (asynch call).</p>
  
  <p><strong>What are the problems of this approach?</strong></p>
  
  <p><strong>Is there any better way?</strong></p>
  
  <p><strong>Are there popular libraries which provides both sync/async for the same API (Without using external events or threads) ?</strong></p>
  
  <p>Here is an example of what I am going to provide:</p>
  
  <pre><code>enum StuffStatus
  {
      SUCCEED,
      FAILED,
      STILL_RUNNING
  };
  class IServiceCallback
  {
  public:
      void lengthyStuffCallback(StuffStatus status);
  };
  
  class MyServiceClass
  {
  public:
      StuffStatus doSomeLengthStuff(IServiceCallback* callback)
      {
          if( callback == NULL ) // user wants sync. call
          {
              // do all operations in caller context
              return SUCCEED;
          }else{
              // save the callback, queue the request in a separate worker thread. 
              // and after the worker thread finishes the job it calls callback-&gt;lengthyStuffCallback(SUCCEED) from its context.
              return STILL_RUNNING;
          }
      }
  };
  </code></pre>
  
  <p><strong>EDIT</strong>: 
  As ' Matthieu M.' mentioned, In my service I need asynchronous with Continuation Passing Style (callback after API finish).</p>