10

I will be grateful if you answer my question about WebAssembly multithreading. I want to implement code with 2 threads (the main thread and a helper one), such that there is a global variable that is used as a counter variable in the helper thread and it increments it in a loop. and the main thread, read the counter variable amount, once before running instruction and once after that (to measure the time that takes for this instruction to be completed). I have implemented this code:


#include "pthread.h"
#include <stdio.h>
#include <unistd.h>
#include<chrono>

int i;
int counter;

void* timerfunction( void *ptr)
{
  printf ("Thread Timer!\n");
  //cout<<"Thread Timer!"<<endl;
  while(1)
  {
    counter=counter+1;
  }
  pthread_exit("The thread was exited!");
}




int main()
{
    pthread_t thread_id;
    void *thread_result;
    int c=0;
    int l=pthread_create(&thread_id,NULL,timerfunction,&c);
    int t1= counter;//reading the counter for the first one

    //intended instruction that we want to measure its execution time   

    int t2= counter;//reading the counter for the second one
    int t3 = t2 - t1;//computing the time
    printf ("value in the counter is: %d \n", t3);
    return 0;
}

What I comprehended is that the supporting of Wasm from multithreading is not complete, because it does not run the main thread and other ones simultaneously and it needs something like sleep to switch between threads. So we cannot use multithreaded Wasm for some goals like increasing a counter in one thread and reading it simultaneously in another one. My question is that either my inference is true or not? And if true, what is the problem? From C or compile process or ...? And is there any alternative method for using complete multithreading? Thanks a lot.

I'm the man.
  • 207
  • 2
  • 13
  • what is the `counter` variable? (it should be `g` I guess) And it is not clear how counting with `g` gives an information on the time spent... (even without wasm) – prog-fh Jan 01 '20 at 09:07
  • Thanks for your hint, the code has been corrected and global variable is counter. In fact I want to use this method as an implicit timer, and measure the time by reading the value of this counter –  Jan 01 '20 at 09:39

1 Answers1

7

You're in luck, Emscripten has implemented PThreads with shared memory.

With a few caveats

As of Sep 2019, some browsers have disabled SharedArrayBuffer due to the Spectre set of vulnerabilities. Until it is restored you can still experiment with it if you flip a pref in those browsers. In other browsers (like Chrome on desktop), SharedArrayBuffer is fully enabled by default and you don’t need to flip any flags.

Its a mechanism to create timers used in Specter/Meltdown attacks

Note that SharedArrayBuffer was disabled by default in all major browsers on 5 January, 2018 in response to Spectre. Chrome re-enabled it in v67 on platforms where its site-isolation feature is enabled to protect against Spectre-style vulnerabilities.

I've not tested it, but the following might work

# Assuming you have a makefile, the following might work
sudo docker run --rm -v $(pwd):/src trzeci/emscripten emmake make
sudo docker run --rm -v $(pwd):/src trzeci/emscripten emcc \
src/main.o \
-s ALLOW_MEMORY_GROWTH=1 \
-s ERROR_ON_UNDEFINED_SYMBOLS=0 \
-s USE_PTHREADS=1 \
Marvin Irwin
  • 938
  • 7
  • 17
  • 3
    On a sidenote, Firefox, too, has re-enabled [SharedArrayBuffers](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer) in 2020 – davidanderle Sep 20 '20 at 08:02
  • But now requires headers for cross Origin Policies on the server https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer – eri0o Oct 23 '20 at 22:01