如果您能回答有关WebAssembly多线程的问题,我将不胜感激。
我想使用两个线程(主线程和辅助线程)实现代码,使得在辅助线程中使用一个全局变量作为计数器,在循环中对其进行增量操作。主线程会在执行指令之前和之后各读取一次计数器变量的值(以测量完成该指令所需的时间)。
我已经实现了以下代码:
#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;
}
我理解的是,Wasm对于多线程的支持不完整,因为它不能同时运行主线程和其他线程,需要类似sleep的东西来在线程之间切换。因此,我们不能在一个线程中增加计数器并在另一个线程中同时读取它。我的问题是,我的推断是否正确?如果正确,问题是什么?是来自C语言还是编译过程等?是否有任何替代方法可以使用完整的多线程?
非常感谢。
counter
变量是什么?(我猜应该是g
)而且不清楚如何使用g
计数可以提供关于花费时间的信息...(即使没有wasm) - prog-fh