2014-09-08 3 views
1

Чтобы использовать модель резьбы M:N в Rust, я бы получил пул и начал запускать нерестовые задачи, как обычно. The Green Documentation приводит следующий пример:Управление зеленой нитью

#![feature(phase)] 
#[phase(plugin)] extern crate green; 

green_start!(main) 

fn main() { 
    // Running inside a green pool 

    // Spawn more green threads? 
    for x in some_thing.iter() { 
     spawn(proc() { 
      some_task() 
     }); 
    } 
} 

Если вы хотите, чтобы динамически добавить еще одну OS нить, можно было бы сделать что-то вроде этого:

extern crate green; 
extern crate rustuv; 

use std::task::TaskBuilder; 
use green::{SchedPool, PoolConfig, GreenTaskBuilder}; 

let mut config = PoolConfig::new(); 

// Optional: Set the event loop to be rustuv's to allow I/O to work 
config.event_loop_factory = rustuv::event_loop; 

let mut pool = SchedPool::new(config); 

// Spawn tasks into the pool of schedulers 
TaskBuilder::new().green(&mut pool).spawn(proc() { 
    // this code is running inside the pool of schedulers 

    spawn(proc() { 
     // this code is also running inside the same scheduler pool 
    }); 
}); 

// Dynamically add a new scheduler to the scheduler pool. This adds another 
// OS thread that green threads can be multiplexed on to. 
let mut handle = pool.spawn_sched(); 

// Pin a task to the spawned scheduler 
TaskBuilder::new().green_pinned(&mut pool, &mut handle).spawn(proc() { 
    /* ... */ 
}); 

// Handles keep schedulers alive, so be sure to drop all handles before 
// destroying the sched pool 
drop(handle); 

// Required to shut down this scheduler pool. 
// The task will fail if `shutdown` is not called. 
pool.shutdown(); 

Есть ли способ сказать, использование х Num потоков ОС, или они должны быть созданы и управляться в коде?

ответ

1

Согласно документации PoolConfig, число потоков ОС можно указать при создании нового пула:

let mut config = PoolConfig::new(); 
config.threads = 42u; 
let mut pool = SchedPool::new(config); 
// use your pool with 42 OS threads as you want 

Значение по умолчанию config.threads задается std::rt::default_sched_threads()

Смежные вопросы