开发者社区> 镜像站> NPM> async-worker.h
async-worker.h
Light C++ wrapper around libuv's uv_queue_work.
Last updated 3 years ago by thlorenz .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install async-worker.h 
SYNC missed versions from official npm registry.

async_worker.h

Light C++ wrapper around libuv's uv_queue_work.

class FileStreamAsyncWorker : public AsyncWorker<int, const char> {
  public:
    FileStreamAsyncWorker(
        uv_loop_t* loop,
        const char& input,
      ) : AsyncWorker(loop, input) {}

  private:
    int* onwork(const char& file) {
      int* chunks = count_chunks(file, CHUNK_SIZE);
      return chunks;
    }

    void ondone(int* result, int status) {
      ASSERT(status == 0);
      fprintf(stderr, "read %d chunks\n", *result);
      delete result;
    }
};

int main(int argc, char *argv[]) {
  uv_loop_t* loop = uv_default_loop();
  const char& file = *argv[0];

  FileStreamAsyncWorker worker(loop, file);
  int r = worker.work();
  ASSERT(r == 0);

  uv_run(loop, UV_RUN_DEFAULT);

  uv_loop_close(loop);
}

Find more examples at the thread communication with libuv playground.

API

To use the async worker just extend from the below class and implement its virtual methods, as shown in the above example.

class AsyncWorker {
  AsyncWorker(uv_loop_t* loop, U& input);

  // Implemented by inheritor, called on worker thread
  virtual T* onwork(U& input) = 0;

  // Implemented by inheritor,  called on loop thread
  virtual void ondone(T* result, int status) = 0;
}

LICENSE

MIT

Current Tags

  • 1.0.2                                ...           latest (3 years ago)

3 Versions

  • 1.0.2                                ...           3 years ago
  • 1.0.1                                ...           3 years ago
  • 1.0.0                                ...           3 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (0)
None
Dev Dependencies (0)
None
Dependents (0)
None