Which operations in node are thread safe?

后端 未结 2 1404
难免孤独
难免孤独 2021-02-07 20:19

I\'m using this approach to store data in a global array hosting an http server where certain requests will manipulate the global array.

I\'m kind of worried about runni

2条回答
  •  忘掉有多难
    2021-02-07 21:06

    Raynos is mostly correct. But just because JavaScript is single threaded doesn't mean you can let your guards down. You still need to be aware of threads. A good example to illustrate this is how socket.io is maintaining connection with varies clients and still be able to keep track of these clients.

    JavaScript's functional programming part works by variable binding (like...A LOT). Not being aware of threads makes you falsely assumes that your variables are the correctly bound. Like the socket.io example, how can you be sure the connection you're getting is the client you think you're getting? What if the reference binding went wrong and the connection actually references a different client?

    This is the type of things you need to be careful of.

提交回复
热议问题