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.

I would suggest you post to https://stackoverflow.com/ (use the “webassembly” tag).

This forum is for HDF5 and related technologies.

1 Like