mirror of
https://github.com/c3lang/c3c.git
synced 2026-02-27 12:01:16 +00:00
* add join for ThreadPool without destroying the threads * Make the main Thread block waiting for the worker threads to finish instead of buzy looping and do proper initialization and freeing of all variables. * Updated test to use `atomic_store` and take into account the maximum queue size of the threadpool. * - Add `ThreadPool` join function to wait for all threads to finish in the pool without destroying the threads. - Return of Thread/Mutex/CondVar `destroy()` is now "@maydiscard" and should be ignored. It will return void in 0.8.0. - Return of Mutex `unlock()` and `lock()` is now "@maydiscard" and should be ignored. They will return void in 0.8.0. - Return of ConditionVariable `signal()` `broadcast()` and `wait()` are now "@maydiscard". They will return void in 0.8.0. - Return of Thread `detatch()` is now "@maydiscard". It will return void in 0.8.0. - Buffered/UnbufferedChannel, and both ThreadPools have `@maydiscard` on a set of functions. They will retunr void in 0.8.0. - Pthread bindings correctly return Errno instead of CInt. - Return of Thread `join()` is now "@maydiscard". --------- Co-authored-by: Christoffer Lerno <christoffer@aegik.com>
73 lines
1.2 KiB
Plaintext
73 lines
1.2 KiB
Plaintext
module thread_pool_test;
|
|
import std::io, std::thread, std::time;
|
|
|
|
alias 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 FOO: (usz i = 0; i < 20; i++)
|
|
{
|
|
@atomic_store(x, 0);
|
|
int y = 20;
|
|
Pool pool;
|
|
pool.init()!!;
|
|
defer pool.destroy();
|
|
pool.push(&do_work, &y);
|
|
thread::sleep(time::ms(50));
|
|
test::eq(@atomic_load(x), @atomic_load(y));
|
|
}
|
|
}
|
|
|
|
fn void push_stop() @test
|
|
{
|
|
for (usz i = 0; i < 20; i++)
|
|
{
|
|
@atomic_store(x, 0);
|
|
int y = 20;
|
|
Pool pool;
|
|
pool.init()!!;
|
|
pool.push(&do_work, &y);
|
|
pool.stop_and_destroy();
|
|
test::eq(@atomic_load(x), @atomic_load(y));
|
|
}
|
|
}
|
|
|
|
fn void join() @test
|
|
{
|
|
@atomic_store(x, 0);
|
|
Pool pool;
|
|
pool.init()!!;
|
|
defer pool.stop_and_destroy();
|
|
for (usz i = 0; i < 4; i++)
|
|
{
|
|
pool.push(&do_wait, (void*)i);
|
|
}
|
|
pool.join();
|
|
test::eq(x, 6);
|
|
}
|
|
|
|
int x;
|
|
|
|
fn int do_work(void* arg)
|
|
{
|
|
@atomic_store(x, @atomic_load(*(int*)arg));
|
|
return 0;
|
|
}
|
|
|
|
fn int do_wait(void* arg)
|
|
{
|
|
usz value = (iptr)arg;
|
|
for (usz i = 0; i < value; i++) thread::sleep(time::ms(50));
|
|
@atomic_store(x, @atomic_load(x) + (int)value);
|
|
return 0;
|
|
} |