mirror of
https://github.com/bdwgc/bdwgc-rust.git
synced 2026-05-29 09:58:55 -06:00
Add dynamic threads example
This commit is contained in:
23
examples/dynamic_threads/Cargo.lock
generated
Normal file
23
examples/dynamic_threads/Cargo.lock
generated
Normal file
@@ -0,0 +1,23 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
[[package]]
|
||||
name = "bdwgc-allocator"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"libc 0.2.51 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "dynamic_threads"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"bdwgc-allocator 0.1.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.51"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[metadata]
|
||||
"checksum libc 0.2.51 (registry+https://github.com/rust-lang/crates.io-index)" = "bedcc7a809076656486ffe045abeeac163da1b558e963a31e29fbfbeba916917"
|
||||
9
examples/dynamic_threads/Cargo.toml
Normal file
9
examples/dynamic_threads/Cargo.toml
Normal file
@@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "dynamic_threads"
|
||||
version = "0.1.0"
|
||||
authors = ["Yota Toyama <raviqqe@gmail.com>"]
|
||||
edition = "2018"
|
||||
publish = false
|
||||
|
||||
[dependencies]
|
||||
bdwgc-allocator = { path = "../.." }
|
||||
28
examples/dynamic_threads/src/main.rs
Normal file
28
examples/dynamic_threads/src/main.rs
Normal 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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user