Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can AsyncWorker declared in another thread? #526

Closed
XadillaX opened this issue Dec 18, 2015 · 5 comments
Closed

Can AsyncWorker declared in another thread? #526

XadillaX opened this issue Dec 18, 2015 · 5 comments

Comments

@XadillaX
Copy link

For some reason (closed source SDK), I must run some code in another thread because that thread is packaged.

And here's my code:

Action ONSListenerV8::consume(Message message, ConsumeContext context)
{
    Message* m = new Message(message);
    ThreadConsumeWorker* worker = new ThreadConsumeWorker(
            parent->GetListenerFunc(),
            *parent,
            m);

    // Nan::AsyncQueueWorker(new ThreadConsumeWorker(
    //             parent->GetListenerFunc(),
    //             *parent,
    //             new Message(message)));

    return CommitMessage;
}

consume is called in a new thread via the SDK, and this code throwed a segmentfault.

So I'm confused can AsyncWorker run in another thread?

@bnoordhuis
Copy link
Member

No, Nan::AsyncQueueWorker() is supposed to be called from the main thread.

If you want to wake up the main thread from another thread, you'll have to jury-rig something using uv_async_send(). Note that the uv_async_t handle should be initialized on the main thread first. Good luck.

@XadillaX
Copy link
Author

@bnoordhuis So how can I get the loop? Use uv_default_loop? Or via other function of Node.js?

@fivdi
Copy link

fivdi commented Dec 18, 2015

@XadillaX although it's probably not perfect maybe you can get some ideas from https://github.com/fivdi/epoll/blob/v0.1.16/src/epoll.cc. Search for all occurrences of async_g to see what's going on.

@bnoordhuis
Copy link
Member

@XadillaX uv_default_loop() yes, from the main thread.

@XadillaX
Copy link
Author

@bnoordhuis Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants