简体   繁体   English

Array.forEach线程安全

[英]Array.forEach thread safety

I am using an array to store a list of connected clients. 我正在使用数组存储已连接客户端的列表。

Whenever I wish to iterate through the client list I do: clients.forEach(...) . 每当我希望遍历客户端列表时,我都会执行: clients.forEach(...)

My question is, is this thread safe, as what if a client disconnects (and is therefore removed from clients ) during the a clients.forEach(...) statement? 我的问题是,此线程安全吗?如果在clients.forEach(...)语句期间客户端断开连接(并因此从clients删除),该怎么办?

Short answer is yes. 简短的回答是。

forEach is a synchronous process, meaning that the array would not be modified until the process has finished executing the forEach loop on the array. forEach是一个同步过程,这意味着只有在该过程完成对数组的forEach循环执行之后,才能修改该数组。

This is my understanding from reading: Are nodejs data-structures thread-safe by design? 通过阅读,我的理解是: nodejs数据结构在设计上是否是线程安全的?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM