Stream: C#/.net-collaboration

Topic: wasmtime-dotnet and wasi threads


view this post on Zulip William Mortl (Oct 28 2023 at 01:37):

Question about wasmtime-dotnet. Please let me know if this isn't the right place to ask (I apologize in advance).

I have a multithreaded C program:

#include <pthread.h>
#include <stdio.h>
#include <string.h>

#define NUM_THREADS 10

void *thread_entry_point(void *ctx) {
  int id = (int) ctx;
  printf(" in thread %d\n", id);
  return 0;
}

extern void launch_threads() {
  pthread_t threads[10];
  for (int i = 0; i < NUM_THREADS; i++) {
    int ret = pthread_create(&threads[i], NULL, &thread_entry_point, (void *) i);
    if (ret) {
      printf("failed to spawn thread: %s", strerror(ret));
    }
  }
  for (int i = 0; i < NUM_THREADS; i++) {
    pthread_join(threads[i], NULL);
  }
}

int main(int argc, char **argv) {
  launch_threads();
  return 0;
}

I can run it without any issue on the command line: /mnt/e/Code/wasmtime/target/release/wasmtime --wasm threads --wasi threads ./threads.wasm

I am trying to write some C# code using wasmtime-dotnet to run the program:

using Wasmtime;

var wasi = new WasiConfiguration()
    .WithInheritedStandardInput()
    .WithInheritedStandardOutput()
    .WithInheritedStandardError()
    .WithArg("--wasi threads");
var config = new Config()
    .WithWasmThreads(true)
    .WithBulkMemory(true)
    .WithMultiMemory(true);
using var engine = new Engine(config);
using var store = new Store(engine);
store.SetWasiConfiguration(wasi);
using var linker = new Linker(engine);
linker.DefineWasi();

using var module = Module.FromFile(engine, "threads.wasm");

var instance = linker.Instantiate(store, module);
var run = instance.GetFunction("_start").Invoke();

When I run it I get an error on the linker.Instantiate(store, module) line of: Wasmtime.WasmtimeException: 'unknown import: 'env::memory' has not been defined'

Could anyone give me any pointers?

Thanks so much! :smile:

view this post on Zulip bjorn3 (Dec 17 2023 at 20:26):

For me wasmtime run -Sthreads=y -Spreview2=n worked. The -Spreview2=n switches back to the old wasi-preview1 implementation that isn't layered on top of wasi-preview2 as the latter is incompatible with thread support.

view this post on Zulip Timmy Silesmo (Dec 19 2023 at 12:22):

bjorn3 said:

For me wasmtime run -Sthreads=y -Spreview2=n worked. The -Spreview2=n switches back to the old wasi-preview1 implementation that isn't layered on top of wasi-preview2 as the latter is incompatible with thread support.

Ah cool. I wasn't aware. Thanks!


Last updated: Dec 13 2025 at 20:04 UTC