一、emscripten_run_script()
最直接但较慢的方法是使用 emscripten_run_script()
。这样可以有效地从C / C ++运行指定的JavaScript代码
test.cpp
#include <pthread.h>
#include <emscripten.h>
#include <stdio.h>
extern "C"
{
EMSCRIPTEN_KEEPALIVE
void *decodeHEVC(void* time)
{
int *iter=(int*) time;
for(int i=0;i<20;i++)
{
if(i%5==0)
{
printf("5 de beishu\n");
}
}
printf("decodeHEVC\n");
emscripten_run_script("callRender(3,4,56)");//回调JS上层方法
return time;
}
EMSCRIPTEN_KEEPALIVE
int test() {
pthread_t timer_thread_sub;
int times = 40;
int timerCreateRess=pthread_create(&timer_thread_sub,NULL,decodeHEVC,(void*)×);
printf("sun pthread_create:%d\n",timerCreateRess);
if(timerCreateRess)
{
perror("sub Thread create failed");
return NULL;
}
int timerJoinRess=pthread_join(timer_thread_sub,NULL);
printf("sun pthread_join:%d\n",timerJoinRess);
if(timerJoinRess)
{
perror("sub Thread join failed");
return NULL;
}
printf("sub fibonacci 40 : %d\n",times);
//emscripten_run_script("callRender(3,4,56)");
return 0;
}
}
使用emcc编译成html
emcc test.cpp -o test.js
test.html
<!doctype html>
<html>
<head>
</head>
<body>
<div class="items">
<button type="button" onClick="start()">start</button>
</div>
<script async type="text/javascript