Javascript允许将缓冲区从源线程转移到Worker线程.否则,复制ArrayBuffer,然后传递给worker.传输缓冲区在源线程[1]中不可访问("neutered"):
// create data that can be transfered
var arr = new Uint8Array(5);
// outputs: 5
console.log(arr.buffer.byteLength);
var worker = new Worker("some_worker.js");
// transfer the buffer
worker.postMessage({arr: arr}, [arr.buff]);
// the buffer vanishes. is "Neutered"
// outputs: 0
console.log(arr.buffer.byteLength);
Run Code Online (Sandbox Code Playgroud)
我想知道机制是如何工作的.然而,我很好奇它为何被引入.为什么工作线程之间没有共享数据,就像传统的线程模型一样,它允许多个线程访问同一个内存区域?
同一问题的其他措辞澄清:
为什么缓冲区在转移时会被阉割?/这种机制背后的原因是什么?/为什么介绍它?为什么工人之间不能共享内存区域?
我正在寻找可信和/或官方消息来源的答案.
[1] https://developer.mozilla.org/en/docs/Web/API/Worker/postMessage
当我尝试使用emcc将C代码编译为Javascript时,我收到以下错误:
emcc tests/hello_world.c
CRITICAL root: fastcomp in use, but LLVM has not been built with the JavaScript backend as a target, llc reports:
===========================================================================
LLVM (http://llvm.org/):
LLVM version 3.5.1
Optimized build with assertions.
Built Feb 22 2015 (00:08:56).
Default target: x86_64-apple-darwin13.4.0
Host CPU: corei7-avx
Registered Targets:
x86 - 32-bit X86: Pentium-Pro and above
x86-64 - 64-bit X86: EM64T and AMD64
===========================================================================
CRITICAL root: you can fall back to the older (pre-fastcomp) compiler core, although that is not recommended, see https://github.com/kripken/emscripten/wiki/LLVM-Backend …Run Code Online (Sandbox Code Playgroud)