-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
26 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
created: 20250106070943385 | ||
creator: Gezi-lzq | ||
modified: 20250106071059706 | ||
modifier: Gezi-lzq | ||
tags: 并发 | ||
title: Work Stealing | ||
|
||
! Work Stealing | ||
|
||
Work Stealing 是一种负载均衡技术,用于多线程并行计算,以便更有效地利用处理器资源,最大化系统吞吐量。 | ||
|
||
!! 基本概念 | ||
|
||
Work Stealing 的主要思想是:当某个处理器或线程完成了自己的任务,而其它处理器或线程仍有大量任务未完成时,前者会从后者的任务队列中“偷走”一些任务,从而保持所有处理器或线程都尽可能忙碌。 | ||
|
||
!! 工作原理 | ||
|
||
* 任务队列 :每个线程维护一个双端队列(deque),用来存储需要执行的任务。 | ||
* 任务分配 :线程从自己的 deque 中取任务,通常是从队头(front)取任务并执行。 | ||
* 任务偷取 :如果一个线程的任务完成了,它会从其它线程的 deque 的队尾(tail)偷取任务,以减少空闲时间,提高资源利用率。 | ||
|
||
!! 优点 | ||
* 提高资源利用率 :确保各个线程都尽量繁忙,提升系统的整体吞吐量。 | ||
* 减少锁争用 :通过双端队列,线程间偷取任务只需在队尾操作,减少了加锁导致的开销。 | ||
* 简化负载均衡 :动态地把任务从繁忙的线程搬移到空闲的线程,无需预先指定任务分配方案。 | ||
|