Pthread emulation in WebAssembly - chung-leong/zigar GitHub Wiki

When the option usePthreadEmulation is set the true, Zigar links in code that emulates pthread functions using Zig's standard library. std.Thread is used to create threads, std.Thread.Mutex handles functionalities provided by pthread_mutex_xxx, and so forth. While the implementation is meant to be complete, there're certain behaviors that simply cannot be mimicked in a web environment:

  • It's not possible to set the priority of a thread. Related functions will report success but nothing actually happens.
  • There is no support for robust mutex.
  • Async thread cancellation is relatively slow, since a new web worker needs to be spawned to perform the clean up.

The emulation layer accounts for the possibility that you'd want to use pthread functions on threads not created through pthread. You can use pthread_exit() to exit from a thread created using std.Thread.spawn, for instance.

pthread emulation in Windows

The Zig compiler itself provides pthread emulation for Windows through the pthread-win32 library. It's entirely unrelated to pthread in WebAssembly. Zigar is designed, however, to work with it. In an other word, pthread should work across all supported platforms.