nodejs C++ shared objects

69 views
Skip to first unread message

korniak

unread,
Aug 6, 2014, 9:05:29 AM8/6/14
to nod...@googlegroups.com

Overview:

I have a NodeJS server with a few C++ modules in it, executing one overall "job". Some objects (C++ objects, let's say "singletons") in these modules are common and their states after initialization should be shared between each module. The initialization of those objects has to be done once during server startup.

Example:

A, B - separate C++ modules which should be executed as one job

x, y, z - shared C++ objects (possibly a lot of them)

  1. Server receives a (http) request and processes it in A using xy and z.
  2. The (http) response goes from A to the client.
  3. Server receives another (http) request and processes it in B also using xy and z.
  4. The (http) response goes from B to the client.

Questions:

Can you tell me if there is some known best practice of initialization and sharing of these objects between all C++ modules?

What is the lifecycle of a particular C++ module in NodeJS? When are they removed from the memory?

Liam

unread,
Aug 6, 2014, 2:15:40 PM8/6/14
to nod...@googlegroups.com
Whereas JS code is executed in a single thread, C++ module code may execute in parallel (with itself and JS) if queued to the thread pool -- which is common if the code makes blocking calls. Using the thread pool for C++ is optional, but if so, your shared C++ objects would have to be protected with mutexes etc.

Note that coding to the V8 api to move data to/from C++ code is error-prone. I've tried to address this with a project called Lode, which creates an external thread pool for C/C++ logic, and links it to Node via JSON messages. https://github.com/networkimprov/lode
Reply all
Reply to author
Forward
0 new messages