mirror of
https://github.com/c3lang/c3c.git
synced 2026-02-27 12:01:16 +00:00
add ThreadPool (#926)
* lib/std/collections: fix tab indentation Signed-off-by: Pierre Curto <pierre.curto@gmail.com> * lib/std/threads: add ThreadPool Signed-off-by: Pierre Curto <pierre.curto@gmail.com> * ats/lib/threads: add num_cpu() Signed-off-by: Pierre Curto <pierre.curto@gmail.com> --------- Signed-off-by: Pierre Curto <pierre.curto@gmail.com>
This commit is contained in:
67
test/unit/stdlib/threads/pool.c3
Normal file
67
test/unit/stdlib/threads/pool.c3
Normal file
@@ -0,0 +1,67 @@
|
||||
module thread_pool_test;
|
||||
import std::io;
|
||||
import std::thread;
|
||||
import std::thread::pool;
|
||||
|
||||
def Pool = ThreadPool(<4>);
|
||||
|
||||
fn void! init_destroy() @test
|
||||
{
|
||||
for (usz i = 0; i < 20; i++)
|
||||
{
|
||||
Pool pool;
|
||||
pool.init()!;
|
||||
pool.destroy()!;
|
||||
}
|
||||
}
|
||||
|
||||
fn void! push_destroy() @test
|
||||
{
|
||||
for (usz i = 0; i < 20; i++)
|
||||
{
|
||||
x = 0;
|
||||
int y = 20;
|
||||
Pool pool;
|
||||
pool.init()!;
|
||||
defer pool.destroy()!!;
|
||||
work_done.lock()!!;
|
||||
pool.push(&do_work, &y)!;
|
||||
work_done.lock()!!;
|
||||
assert(x == y, "%d: %d != %d", i, x, y);
|
||||
work_done.unlock()!!;
|
||||
}
|
||||
}
|
||||
|
||||
fn void! push_stop() @test
|
||||
{
|
||||
for (usz i = 0; i < 20; i++)
|
||||
{
|
||||
x = 0;
|
||||
int y = 20;
|
||||
Pool pool;
|
||||
pool.init()!;
|
||||
work_done.lock()!!;
|
||||
pool.push(&do_work, &y)!;
|
||||
pool.stop_and_destroy()!!;
|
||||
assert(x == y, "%d: %d != %d", i, x, y);
|
||||
}
|
||||
}
|
||||
|
||||
int x;
|
||||
|
||||
Mutex work_done;
|
||||
|
||||
static initialize {
|
||||
work_done.init()!!;
|
||||
}
|
||||
|
||||
static finalize {
|
||||
work_done.destroy()!!;
|
||||
}
|
||||
|
||||
fn int do_work(void* arg)
|
||||
{
|
||||
x = *(int*)arg;
|
||||
work_done.unlock()!!;
|
||||
return 0;
|
||||
}
|
||||
@@ -63,10 +63,7 @@ fn void! testrun_mutex_timeout() @test
|
||||
Mutex m;
|
||||
m.init()!;
|
||||
m.lock()!;
|
||||
if (catch m.lock_timeout(100))
|
||||
{
|
||||
}
|
||||
else
|
||||
if (try m.lock_timeout(100))
|
||||
{
|
||||
assert(false, "lock_timeout should fail");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user