{
  "schemaVersion": "0.11.0",
  "canonical": "https://www.pystone.net/notes/webgl-multithreading-issues/",
  "atlas": "https://www.pystone.net/?node=webgl-multithreading-issues#knowledge-atlas",
  "markdown": "https://www.pystone.net/notes/webgl-multithreading-issues.md",
  "context": "https://www.pystone.net/notes/webgl-multithreading-issues.context.json",
  "knowledgeVersion": "224c990773de.5fa8af6e39fa",
  "build": {
    "siteCommit": "224c990773de166d23a886306577dd90379529ce",
    "notesCommit": "5fa8af6e39fa3891d1b9b4832bfa6c4e0ecaaf0a",
    "builtAt": "1970-01-01T00:00:00.000Z",
    "version": "224c990773de.5fa8af6e39fa"
  },
  "id": "note:webgl-multithreading-issues",
  "slug": "webgl-multithreading-issues",
  "title": "关于多线程问题",
  "type": "note",
  "visibility": "public",
  "idStability": "rename-stable",
  "author": {
    "name": "Perrin Yong",
    "profile": "https://www.pystone.net/profile/"
  },
  "publisher": {
    "name": "Perrin Yong",
    "profile": "https://www.pystone.net/profile/"
  },
  "aliases": [],
  "summary": "关于多线程问题",
  "contentRole": "unspecified",
  "isMoc": false,
  "mocRecognition": "none",
  "generated": false,
  "attribution": "unspecified",
  "domain": "10-计算机、信息技术与工程",
  "tags": [],
  "mocs": [],
  "contentHash": "62f99d59bc33b96be450488c23d1fc46c229b6c3a6276dd24d27eaa6890f9bc1",
  "assets": [],
  "headings": [
    {
      "depth": 1,
      "text": "关于多线程问题",
      "anchor": "关于多线程问题",
      "citation": "https://www.pystone.net/notes/webgl-multithreading-issues/#%E5%85%B3%E4%BA%8E%E5%A4%9A%E7%BA%BF%E7%A8%8B%E9%97%AE%E9%A2%98"
    },
    {
      "depth": 3,
      "text": "1. 浏览器端：真·多线程 WebGL + WASM",
      "anchor": "1-浏览器端真多线程-webgl-wasm",
      "citation": "https://www.pystone.net/notes/webgl-multithreading-issues/#1-%E6%B5%8F%E8%A7%88%E5%99%A8%E7%AB%AF%E7%9C%9F%E5%A4%9A%E7%BA%BF%E7%A8%8B-webgl-wasm"
    },
    {
      "depth": 3,
      "text": "2. 微信 / 抖音小程序：只有「逻辑线程」，没有「共享内存」",
      "anchor": "2-微信-抖音小程序只有逻辑线程没有共享内存",
      "citation": "https://www.pystone.net/notes/webgl-multithreading-issues/#2-%E5%BE%AE%E4%BF%A1-%E6%8A%96%E9%9F%B3%E5%B0%8F%E7%A8%8B%E5%BA%8F%E5%8F%AA%E6%9C%89%E9%80%BB%E8%BE%91%E7%BA%BF%E7%A8%8B%E6%B2%A1%E6%9C%89%E5%85%B1%E4%BA%AB%E5%86%85%E5%AD%98"
    }
  ],
  "claims": [],
  "outgoing": [],
  "incoming": [
    {
      "id": "note:game-graphics-and-runtime",
      "title": "游戏图形与运行时",
      "url": "https://www.pystone.net/notes/game-graphics-and-runtime/",
      "atlas": "https://www.pystone.net/?node=game-graphics-and-runtime#knowledge-atlas",
      "label": "游戏图形与运行时",
      "origin": "explicit",
      "humanReviewed": true,
      "context": "WebGL中的“关于多线程问题”导航项",
      "citation": "https://www.pystone.net/notes/game-graphics-and-runtime/#webgl"
    }
  ],
  "contentMarkdown": "# 关于多线程问题\n\n﻿# 关于多线程问题\n\n\n| 场景          | WebGL 渲染线程                                                                       | C++/WASM 多线程                                                                                            | 说明                                                                       |\n| ----------- | -------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------ |\n| 桌面／移动浏览器    | **支持**：使用 `Web Worker + OffscreenCanvas` 渲染线程、`SharedArrayBuffer` & WASM Threads | **支持**：`emcc -s USE_PTHREADS=1 …` 生成 pthreads-WASM，浏览器把每个 `std::thread` 映射到 Worker                      | 需启用 COOP/COEP 响应头以开放 SharedArrayBuffer([caniuse.com][1], [juejin.cn][2]) |\n| 微信小程序       | JS Worker & `wx.createOffscreenCanvas()` 可用，但 **仅 2D / 部分 WebGL**                | **不支持**：运行环境禁用了 SharedArrayBuffer，无法启用 WASM Threads([developer.open-douyin.com][3], [blog.csdn.net][4]) |                                                                          |\n| 抖音/飞书/字节小游戏 | `tt.createCanvas()` 第二次返回 OffscreenCanvas，可在 Worker 内绘制 WebGL，但仍是 **单工内存**       | **不支持**：与微信相同，没有跨源隔离 API，可用 JS Worker 分工渲染([developer.open-douyin.com][3])                              |                                                                          |\n\n### 1. 浏览器端：真·多线程 WebGL + WASM\n\n1. **开启浏览器线程能力**\n\n```bash\nemcc main.cpp \\\n\t-sUSE_PTHREADS=1           # 打开 pthreads → WASM Threads\n\t-sPTHREAD_POOL_SIZE=8      # 预建 8 个 Worker\n\t-sOFFSCREENCANVAS_SUPPORT=1\\\n\t-sPROXY_TO_PTHREAD=1       # 把主线程逻辑也放到 Worker\n\t-O3 -o index.html\n```\n\n2. **服务器需送两头**（缺一不可）\n\n```yaml\nCross-Origin-Opener-Policy: same-origin\nCross-Origin-Embedder-Policy: require-corp\n```\n\n   这样浏览器会解锁 `SharedArrayBuffer`，WASM Threads 就能共享线性内存。\n\n3. **典型线程代码**\n\n```cpp\n#include <thread>\nvoid worker(int id){ /* heavy job */ }\nint main(){\n   std::thread t(worker, 0);\n   t.join();\n   return 0;\n}\n```\n\n每条 `std::thread` 会被 Emscripten 映射成一个 JavaScript Worker，渲染可通过 `emscripten_set_canvas_element_size` 或 `eglMakeCurrent` 与 OffscreenCanvas 交互。\n\n---\n\n### 2. 微信 / 抖音小程序：只有「逻辑线程」，没有「共享内存」\n\n微信不支持wasm层面启用多线程。\nWorker框架多线程文档： https://developers.weixin.qq.com/minigame/dev/guide/framework/worker/tutorial.html\n\n[1]: https://caniuse.com/wasm-threads?utm_source=chatgpt.com \"WebAssembly Threads and Atomics | Can I use... Support tables for HTML5 ...\"\n[2]: https://juejin.cn/post/7508968054875308043?utm_source=chatgpt.com \"Web Worker + OffscreenCanvas，实现真正多线程渲染体验\"\n[3]: https://developer.open-douyin.com/forum/question/post/66d72de7372f131f6bd89b76 \"〖提问〗小游戏中OffscreenCanvas正确的使用姿势应该是怎样？ - 抖音开放平台\"\n[4]: https://blog.csdn.net/weixin_44892327/article/details/135712532 \"微信小程序 页面跳转失败 SharedArrayBuffer will require cross-origin isolation as of M92, around July 2021. See-CSDN博客\"\n"
}
