0

For example:

var i = 0;
while(true)
 http.request('a url', callback_f);
function **callback_f**(){
 **i++**;
}

In my simple imaginary example, some request might try to increase i's value in the same time, How can I write a code which is thread safe in NodeJS?

asked Mar 22, 2014 at 6:45
1

1 Answer 1

1

You do not have to worry about threads in Node.js. Node.js handles all calls within a single threaded environment.

answered Mar 22, 2014 at 6:47
Sign up to request clarification or add additional context in comments.

5 Comments

Sounds awesome! Then I don't need to write something like delegation(in .netframeowrk) in NodeJS?
I do not know .NET delegation, but in Node.js almost all IO functions are implemented asynchronously which allows the system to call the callbacks in a single threaded manner. This is one of the most powerful properties of Node.js actually.
@HakanSerce Not almost all, all
@thefortheye There are actually several synchronous APIs in Node.js. Check this for instance, nodejs.org/docs/v0.3.1/api/fs.html#fs.readSync
@HakanSerce What I meant is, there is no Node.js function which does only synchronous IO (it may have Sync counterparts)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.