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
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
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.
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;
MIT