Threading examples

This commit is contained in:
root 2022-06-23 19:20:54 +00:00
parent 20f72ebeae
commit b2c71bdda8
25 changed files with 58 additions and 14 deletions

View File

@ -10,4 +10,6 @@ void call_wasm_function();
void deInit_wasm();
void thread_function();
#endif

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -45,5 +45,5 @@ void call_lua_function(lua_State* L)
lua_pop(L,1);
printf("sum: %d\n", sum);
total_t=(double)(stop_t-start_t)/ CLOCKS_PER_SEC;
printf("Total time = %f\n", total_t);
printf("Lua Total time = %f\n", total_t);
}

View File

@ -18,6 +18,11 @@
#include "luaModule.h"
#include "wasm.h"
struct arg_struct {
int arg1;
int arg2;
};
int
main(int argc, char *argv[])
{
@ -38,9 +43,34 @@ main(int argc, char *argv[])
stop_t= clock();
printf("C sum: %d\n", test);
total_t=(double)(stop_t-start_t)/ CLOCKS_PER_SEC;
printf("Total time = %f\n", total_t);
printf("Native total time = %f\n\n", total_t);
return 0;
printf("Starting thread example \n");
pthread_t thread1, thread2, thread3;
int iret1, iret2, iret3;
struct arg_struct args;
args.arg1 = 2;
args.arg2 = 3;
/* Create independent threads each of which will execute function */
iret1 = pthread_create( &thread1, NULL, call_wasm_function, NULL);
iret2 = pthread_create( &thread2, NULL, call_lua_function, L);
iret3 = pthread_create( &thread3, NULL, sum, (void*) &args);
/* Wait till threads are complete before main continues. Unless we */
/* wait we run the risk of executing an exit which will terminate */
/* the process and all threads before the threads have completed. */
pthread_join( thread1, NULL);
pthread_join( thread2, NULL);
start_t= clock();
pthread_join( thread3, NULL);
stop_t= clock();
printf("C sum: %d\n", test);
total_t=(double)(stop_t-start_t)/ CLOCKS_PER_SEC;
printf("Native Thread Total time = %f\n", total_t);
wasm_thread_function();
exit(0);
}
int sum(int start, int length)
@ -53,4 +83,4 @@ int sum(int start, int length)
}
}
return sum;
}
}

View File

@ -1,4 +0,0 @@
def sum (start, length):
for x in range(0,10000000):
sum(x for x in range (start,length))
sum(2,3)

View File

@ -20,10 +20,11 @@ wasm_exec_env_t exec_env = NULL;
RuntimeInitArgs init_args;
ThreadArgs thread_arg[THREAD_NUM];
pthread_t tid[THREAD_NUM];
wasm_thread_t wasm_tid[THREAD_NUM];
uint32 result[THREAD_NUM], sum;
wasm_thread_t wasm_tid;
uint32 result, sum;
wasm_function_inst_t func;
char error_buf[128] = { 0 };
pthread_t wasm_thread;
void init_wasm()
{
@ -35,7 +36,6 @@ void init_wasm()
init_args.mem_alloc_option.allocator.realloc_func = realloc;
init_args.mem_alloc_option.allocator.free_func = free;
init_args.max_thread_num = THREAD_NUM;
/* initialize runtime environment */
if (!wasm_runtime_full_init(&init_args)) {
@ -68,7 +68,6 @@ void init_wasm()
printf("failed to create exec_env\n");
return -1;
}
func = wasm_runtime_lookup_function(wasm_module_inst, "sum", NULL);
if (!func) {
printf("failed to lookup function sum");
@ -105,6 +104,23 @@ void call_wasm_function()
stop_t= clock();
printf("expect result: %d\n", wasm_argv[0]);
total_t=(double)(stop_t-start_t)/ CLOCKS_PER_SEC;
printf("Total time = %f\n", total_t);
printf("WASM Total time = %f\n", total_t);
return NULL;
}
}
void wasm_thread_function()
{
clock_t start_t, stop_t;
double total_t;
wasm_runtime_spawn_thread(exec_env,&wasm_tid, call_wasm_function, NULL);
start_t= clock();
wasm_runtime_join_thread(wasm_tid, NULL);
stop_t= clock();
total_t=(double)(stop_t-start_t)/ CLOCKS_PER_SEC;
printf("WASM Thread Total time = %f\n", total_t);
}
// void thread_function(){
// char *message = "WASM thread";
// //printf("test /n");
// pthread_create(&wasm_thread, NULL, call_wasm_function, (void*) message);
// }