Multithreading in WebAssembly

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

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.

The html5 multi threading work was progressing well, but due to the Meltdown vulnerability in January 2018, that work stopped.
Recently, there has been more progress on this front, and hopefully, in 2020, we might get to see some first implementations that we can try out.

As for workarounds, you might try WebWorkers (I haven’t used those at all).
What is your use case?

1 Like

Thanks for your answer,
but as stated in this link, “the Chrome 70 release supports threads for WebAssembly and we encourage interested developers to start using them and give us feedback”.
yes, this work is possible by web workers, but i want to use multithreading not workers.
thanks.

Since it’s only one browser, and they’re asking for feedback, I’d say the feature is highly experimental at this stage. We’ll need more support across the browsers for it to be a more mature feature.

3 Likes

That article also says

Note: WebAssembly threads are enabled by default in Chrome 74 for desktop. The origin trial mentioned in this article ended in Chrome 75.

I am using Version 79.0.3945.88 (Official Build) (64-bit).

Apparently Chrome has it enabled but other browser still don’t? It should be fixed everywhere eventually, it’s still too early.