nodesource/async-worker.h

Name: async-worker.h

Owner: NodeSource

Description: Light C++ wrapper around libuv's uv_queue_work.

Created: 2017-12-13 16:44:41.0

Updated: 2017-12-13 16:54:50.0

Pushed: 2017-12-21 19:23:11.0

Homepage: null

Size: 5

Language: C++

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

async_worker.h

Light C++ wrapper around libuv's uv_queue_work.

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

ivate:
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;
}


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

leStreamAsyncWorker worker(loop, file);
t r = worker.work();
SERT(r == 0);

_run(loop, UV_RUN_DEFAULT);

_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.

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

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

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

LICENSE

MIT


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.