Add dynamic threads example

This commit is contained in:
Yota Toyama
2019-04-14 06:17:54 +00:00
parent 61d712fbdf
commit 06505aa49c
3 changed files with 60 additions and 0 deletions

View File

@@ -0,0 +1,28 @@
extern crate bdwgc_allocator;
use bdwgc_allocator::Allocator;
use std::alloc::{GlobalAlloc, Layout};
#[global_allocator]
static GLOBAL_ALLOCATOR: Allocator = Allocator;
fn main() {
unsafe { Allocator::initialize() }
loop {
let handle = std::thread::spawn(move || {
Allocator::register_current_thread().unwrap();
let mut _n =
unsafe { GLOBAL_ALLOCATOR.alloc(Layout::from_size_align(2 ^ 8, 8).unwrap()) };
for _ in 0..100 {
_n = unsafe { GLOBAL_ALLOCATOR.alloc(Layout::from_size_align(2 ^ 8, 8).unwrap()) }
}
Allocator::unregister_current_thread();
});
handle.join().unwrap();
}
}