Skip to content

Instantly share code, notes, and snippets.

@adamscott
Last active July 12, 2025 15:31
Show Gist options
  • Save adamscott/b6f06d378bdd299f1369c8fa4723da0e to your computer and use it in GitHub Desktop.
Save adamscott/b6f06d378bdd299f1369c8fa4723da0e to your computer and use it in GitHub Desktop.
Safe flag test (emscripten)
#include <atomic>
#include <cstdbool>
#include <cstdio>
#include <thread>
std::thread thread;
std::atomic_bool value;
void thread_callback() {
printf("Hello\n");
printf("thread_callback %s\n",
value.load(std::memory_order_acquire) ? "true" : "false");
}
void thread_callback_dummy() {}
int main() {
printf("Hello, World! a\n");
value.store(true, std::memory_order_release);
printf("After store\n");
printf("main thread_callback %s\n",
value.load(std::memory_order_acquire) ? "true" : "false");
for (int i = 0; i < 10; i++) {
std::thread _thread(thread_callback_dummy);
_thread.join();
}
value.store(false, std::memory_order_release);
value.store(true, std::memory_order_release);
thread = std::thread(thread_callback);
thread.join();
printf("End\n");
return 0;
}
import os
from SCons import Environment
env = Environment.Environment(tools=["cc", "c++", "ar", "link", "textfile", "zip"])
env["ENV"] = os.environ
env["CC"] = "emcc"
env["CXX"] = "em++"
env["AR"] = "emar"
env["RANLIB"] = "emranlib"
# Use TempFileMunge since some AR invocations are too long for cmd.exe.
# Use POSIX-style paths, required with TempFileMunge.
env["ARCOM_POSIX"] = env["ARCOM"].replace("$TARGET", "$TARGET.posix").replace("$SOURCES", "$SOURCES.posix")
env["ARCOM"] = "${TEMPFILE('$ARCOM_POSIX','$ARCOMSTR')}"
# All intermediate files are just object files.
env["OBJPREFIX"] = ""
env["OBJSUFFIX"] = ".o"
env["PROGPREFIX"] = ""
# Program() output consists of multiple files, so specify suffixes manually at builder.
env["PROGSUFFIX"] = ""
env["LIBPREFIX"] = "lib"
env["LIBSUFFIX"] = ".a"
env["LIBPREFIXES"] = ["$LIBPREFIX"]
env["LIBSUFFIXES"] = ["$LIBSUFFIX"]
env.Append(CPPFLAGS=["-sUSE_PTHREADS=1"])
env.Append(LINKFLAGS=["-sUSE_PTHREADS=1"])
env.Append(LINKFLAGS=["-sPTHREAD_POOL_SIZE=8"])
env.Append(CPPFLAGS=["-sSHARED_MEMORY=1"])
env.Append(LINKFLAGS=["-sSHARED_MEMORY=1"])
env.Append(CPPFLAGS=["--std=c++17"])
env.Append(LINKFLAGS=["--std=c++17"])
env.Program(["index.html", "index.js", "index.wasm"], ["main.cpp"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment