Multithreading in WebAssembly

51 views
Skip to first unread message

yous oren

unread,
Jan 1, 2020, 4:43:40 AM1/1/20
to emscripten-discuss
Hi,
I will be grateful if you answer my question about WebAssembly multithreading.
I want to implement a code with 2 thread (a 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 increment it in a loop. and the main thread, read the counter variable amount, once before running an 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 exececution 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 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 to use complete multithreading?
Thanks a lot.

Alon Zakai

unread,
Jan 6, 2020, 12:58:00 PM1/6/20
to emscripte...@googlegroups.com
> What I comprehended is that the supporting of Wasm from multithreading is not complete, because it does not run main thread and other ones simultaneously and it needs something like sleep to switch between threads.

That's not accurate - while there are some limitations, they are much more small than that. The main thread and other threads *do* run simultaneously, using multiple CPU cores as expected. And yes, incrementing a counter on one thread and reading it in another can work - see tests/pthread/test_pthread_atomics.cpp and related tests which I believe test those things.

The main issues that do exist are blocking on the main thread and related things, see https://emscripten.org/docs/porting/pthreads.html#proxying Basically, the main thread on the Web is special, and unlike other threads, which means you can't block on it. However, you can avoid most of the issues with that using PROXY_TO_PTHREAD or other techniques, see details in the link.


On Wed, Jan 1, 2020 at 12:13 AM yous oren <sh.sa...@gmail.com> wrote:
Hi,
I will be grateful if you answer my question about WebAssembly multithreading.
I want to implement a code with 2 thread (a 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 increment it in a loop. and the main thread, read the counter variable amount, once before running an 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 g;


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= g;//reading the counter for the first one
   
    //intended instruction that we want to measure its exececution time   

    int t2= g;//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 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 to use complete multithreading?
Thanks a lot.

--
You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/emscripten-discuss/e464542c-dfa0-4f39-85f4-b70010ccf178%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages