On Sat, Jun 16, 2012 at 3:08 AM, wtfux <[email protected]> wrote: > Hey guys, > I'm quite a beginner at C++ but I still try to create a node addon in order > to use a c++ library in node. > I got it working partially but I have a few problems and questions: > > 1. The library I'm using will stay during the whole life of the node process > and needs to run in an own thread. Is there a "native" way to create > threads? I'm using pthreads but I guess there is a better way. I looked into > libuv and in the last git master there is a function `uv_thread_create`. Is > this what I'm looking for? I'm currently building with latest node.js stable > and this function is not yet implemented there. I could probably also use > `uv_queue_work` to create a worker in Node's thread pool but I believe this > is not a good idea since the worker will never complete and will take a > thread from the pool for the whole runtime.
uv_thread_create() is your best option but, as you mention, it's not available in v0.6. > 2. I have trouble using libuv's ref counter in order to keep the main Node > thread stay alive. Node should stay until the thread from question 1 exists > and should be able to handle callbacks in the meantime. (I didn't implement > callbacks yet) > In order to do this I called `uv_ref (uv_default_loop ());` and Node will > stay open. However here is my problem: > When the thread is about to exit I call `uv_unref(uv_default_loop());` but > Node will not terminate. `uv_loop_refcount(uv_default_loop())` shows 1 > before that call and 0 after. Shouldn't Node terminate when the count is 0? > Is it because I'm calling from a different thread? Or is this even a > complete wrong way to keep Node stay open? Without any good documentation or > tutorial I find libuv hard to understand. Don't do that. libuv is *not* thread-safe with the sole exception of uv_async_send(). Set up an async handle before you create the worker thread. When the thread is about to quit, have it wake up the main thread with uv_async_send(). Unref the loop and close the handle in your callback. (That's for v0.6. In v0.8, you only need to close the handle.) I idle in #libuv on freenode.org, feel free to pop in and ask questions. -- Job Board: http://jobs.nodejs.org/ Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines You received this message because you are subscribed to the Google Groups "nodejs" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/nodejs?hl=en?hl=en
