Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F130187
thread_pool.rs
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Size
1 KB
Referenced Files
None
Subscribers
None
thread_pool.rs
View Options
use
std
::
sync
::
mpsc
;
use
std
::
sync
::
Arc
;
use
std
::
sync
::
Mutex
;
use
std
::
thread
;
enum
Message
{
NewJob
(
Job
),
Terminate
,
}
pub
struct
ThreadPool
{
jobs
:
mpsc
::
Sender
<
Message
>
,
threads
:
Vec
<
Worker
>
,
}
trait
FnBox
{
fn
call_box
(
self
:
Box
<
Self
>
);
}
impl
<
F
:
FnOnce
()
>
FnBox
for
F
{
fn
call_box
(
self
:
Box
<
F
>
)
{
(
*
self
)()
}
}
type
Job
=
Box
<
dyn
FnBox
+
Send
+
'
static
>
;
impl
ThreadPool
{
pub
fn
new
(
thread_count
:
usize
)
->
Self
{
let
(
jobs
,
receiver
)
=
mpsc
::
channel
();
let
receiver
=
Arc
::
new
(
Mutex
::
new
(
receiver
));
let
mut
threads
:
Vec
<
Worker
>
=
Vec
::
new
();
for
id
in
1
..
thread_count
{
threads
.
push
(
Worker
::
new
(
id
,
Arc
::
clone
(
&
receiver
)));
}
ThreadPool
{
jobs
,
threads
}
}
pub
fn
execute
<
F
>
(
&
self
,
f
:
F
)
where
F
:
FnOnce
()
+
Send
+
'
static
,
{
let
job
=
Box
::
new
(
f
);
self
.
jobs
.
send
(
Message
::
NewJob
(
job
)).
unwrap
();
}
}
impl
Drop
for
ThreadPool
{
fn
drop
(
&
mut
self
)
{
for
_
in
&
mut
self
.
threads
{
self
.
jobs
.
send
(
Message
::
Terminate
).
unwrap
();
}
for
worker
in
&
mut
self
.
threads
{
if
let
Some
(
thread
)
=
worker
.
thread
.
take
()
{
thread
.
join
().
unwrap
();
}
}
}
}
struct
Worker
{
_id
:
usize
,
thread
:
Option
<
thread
::
JoinHandle
<
()
>>
,
}
impl
Worker
{
fn
new
(
id
:
usize
,
receiver
:
Arc
<
Mutex
<
mpsc
::
Receiver
<
Message
>>>
)
->
Worker
{
let
thread
=
thread
::
spawn
(
move
||
loop
{
let
message
=
receiver
.
lock
().
unwrap
().
recv
().
unwrap
();
match
message
{
Message
::
NewJob
(
job
)
=>
job
.
call_box
(),
Message
::
Terminate
=>
{
break
;
}
}
});
Worker
{
_id
:
id
,
thread
:
Some
(
thread
),
}
}
}
File Metadata
Details
Attached
Mime Type
text/x-c
Expires
Apr 6 2026, 3:00 PM (5 w, 6 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
16022
Default Alt Text
thread_pool.rs (1 KB)
Attached To
Mode
R1 ivy.nvim
Attached
Detach File
Event Timeline
Log In to Comment