Add thread support

This commit is contained in:
2024-04-14 22:32:58 +03:00
parent d6c396231f
commit a645a201d8
9 changed files with 837 additions and 2 deletions

45
main.c
View File

@@ -1,5 +1,6 @@
#include <bh/queue.h>
#include <bh/hashmap.h>
#include <bh/thread.h>
#include <stdio.h>
#include <stdint.h>
@@ -46,6 +47,46 @@ void foo()
bh_hashmap_free(hashmap);
}
int factor(int x)
{
if (x < 2)
return 1;
return factor(x - 1) + factor(x - 2);
}
void factor_task(void *arg)
{
printf("Task start\n");
fflush(stdout);
printf("Factor: %d\n", factor(48));
fflush(stdout);
}
void bar()
{
bh_thread_pool_t *pool;
bh_task_t *task;
size_t i;
printf("Pool create\n");
fflush(stdout);
pool = bh_thread_pool_new(16);
printf("Prepare\n");
fflush(stdout);
for (i = 0; i < 32; i++)
{
printf("Task create\n");
fflush(stdout);
task = bh_task_new(factor_task, NULL, BH_THREAD_CLEANUP);
bh_thread_pool_add(pool, task);
}
bh_thread_pool_wait(pool);
bh_thread_pool_free(pool);
}
int main()
{
bh_queue_t *queue;
@@ -54,6 +95,10 @@ int main()
foo();
printf("Thread?\n");
fflush(stdout);
bar();
queue = bh_queue_new();
for (j = 0; j < 32; j++)