pub struct ThreadPool {
pub threads_share: Vec<TaskExecutor>,
pub threads_fast: SyncVec<TaskExecutor>,
pub switch_thread_index: Mutex<i32>,
pub threads_fast_idx: StockPool<AtomicCell<Option<usize>>>,
pub stock_lock: StockPool<Arc<Mutex<()>>>,
}Expand description
Thread pool Manage threads through a thread pool The default number of threads in this thread pool is 1/4 of the CPU cores; tasks are submitted to the default thread pool by default; Enable exclusive high-performance mode through an API; In exclusive high-performance mode, the number of threads in the thread pool is at most 1/5 of the CPU cores. For example, for a 128-core CPU, the thread pool has at most 25 threads; threads_high_performance_mode: By checking the number of tasks in the current thread, if the number of tasks within 10 milliseconds is the least among other threads, the task is assigned to that thread; 线程池 通过线程池来管理线程 此线程池的线程个数默认为cpu核心数的4分之一; 任务默认提交在默认线程池; 通过一个api开启独享高性能模式; 独享高性能模式下,线程池的线程个数最多为cpu核心数的5分之1, 比如128核的cpu, 线程池的线程个数最多为25个; theads_高性能模式: 通过查看当前线程的任务数, 如果任务数10毫秒内任务数是其它线程中最少的, 则将任务分配给该线程; 使用 core_affinity 获得cpu核心数 如果已经有一个股票的任务在一个线程中执行, 则将任务分配给该线程; 如果该股票的任务全部执行完毕, 则将任务分配给任务数最少的线程;
Fields§
§threads_fast: SyncVec<TaskExecutor>§switch_thread_index: Mutex<i32>§threads_fast_idx: StockPool<AtomicCell<Option<usize>>>记录高性能模式下, 当前股票代码在哪个线程中执行
stock_lock: StockPool<Arc<Mutex<()>>>记录高性能模式下, 当前股票代码正在执行的任务数 防止同一支票并行运行
Implementations§
Source§impl ThreadPool
impl ThreadPool
pub fn new(cpu_fraction_fast: usize, cpu_fraction_share: usize) -> ThreadPool
Sourcepub fn count_task_min(&self, i7: i32) -> IdxStatus
pub fn count_task_min(&self, i7: i32) -> IdxStatus
获取拥有最少任务的线程索引
本函数遍历快速线程池中的线程,寻找任务计数最少的线程。 如果找到一个任务计数为0的线程,它将立即返回该线程的索引, 因为这表示该线程目前没有任务。否则,函数将返回任务计数最少的线程索引。 这个信息用于调度新任务到拥有最少任务的线程,以平衡线程间的工作负载。 获取下一个要切换到的线程的索引,以实现线程之间的任务平衡。
这个方法通过循环的方式选择下一个线程索引,以确保任务能够均匀地分配给每个线程。 它使用了一个互斥锁来保证在多线程环境下对索引的访问是安全的。
§返回值
根据当前线程池的状态,计算并返回下一个应该执行任务的线程索引。 这个方法旨在平衡线程间的任务分配,避免某个线程过载而其他线程闲置的情况。
参数 i7 作为一个辅助的计算参数,用于在无法立即获得锁时决定返回哪个线程索引。
返回值是一个枚举 IdxStatus,它可以指示线程应该记住当前计算出的索引(Remember),
或者由于锁的竞争失败而丢弃当前的计算并使用另一个索引(Discard)。
返回当前选择的线程索引。