Skip to content

Commit 136ec4d

Browse files
committed
Add more interfaces.
1. Adds a ThreadBuilderExt trait containing helpful methods for spawning a thread with priority. The trait is seamlessly implemented for the std::thread::Builder so that it is easy to use. 2. Adds a struct ThreadBuilder which can be used to set the priority and stuff much more easily than just calling functions, before starting a thread.
1 parent 2db9082 commit 136ec4d

File tree

8 files changed

+798
-57
lines changed

8 files changed

+798
-57
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ jobs:
6262

6363
- name: Test all features (Linux)
6464
if: matrix.features == '' && runner.os == 'Linux'
65-
run: sudo -E /usr/share/rust/.cargo/bin/cargo test --all-features
65+
run: sudo -E /home/runner/.cargo/bin/cargo test --all-features
66+
# run: sudo -E /usr/share/rust/.cargo/bin/cargo test --all-features
6667

6768
clippy:
6869
name: Run clippy

.github/workflows/nightly.yml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,14 @@ jobs:
5858
if: matrix.features == ''
5959
run: cargo build --all-features
6060

61-
- name: Test all features
62-
if: matrix.features == ''
63-
run: cargo test --all-features
61+
- name: Test all features (other)
62+
if: matrix.features == '' && runner.os != 'Linux'
63+
run: cargo test --all-features -- --skip set_deadline_policy
64+
65+
- name: Test all features (Linux)
66+
if: matrix.features == '' && runner.os == 'Linux'
67+
run: sudo -E /home/runner/.cargo/bin/cargo test --all-features
68+
# run: sudo -E /usr/share/rust/.cargo/bin/cargo test --all-features
6469

6570
rustfmt:
6671
name: Format

.travis.yml

Lines changed: 0 additions & 29 deletions
This file was deleted.

Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@ keywords = ["thread", "schedule", "priority", "pthread"]
1111
categories = ["concurrency", "asynchronous", "os"]
1212
edition = "2018"
1313

14+
[dependencies]
15+
log = "0.4"
16+
1417
[target.'cfg(unix)'.dependencies]
1518
libc = "0.2"
1619

1720
[target.'cfg(windows)'.dependencies]
1821
libc = "0.2"
19-
winapi = { version = "0.3", features = ["errhandlingapi", "processthreadsapi", "winnt", "minwindef", "winbase"] }
22+
winapi = { version = "0.3", features = ["errhandlingapi", "processthreadsapi", "winnt", "minwindef", "winbase"] }

README.md

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ so feel free to contribute!
1414
- Linux
1515
- Windows
1616

17-
## Example
17+
## Examples
18+
19+
### Minimal cross-platform examples
1820
Setting current thread's priority to minimum:
1921

2022
```rust,no_run
@@ -25,5 +27,94 @@ fn main() {
2527
}
2628
```
2729

30+
The same as above but using a specific value:
31+
32+
```rust,no_run
33+
use thread_priority::*;
34+
use std::convert::TryInto;
35+
36+
fn main() {
37+
// The lower the number the lower the priority.
38+
assert!(set_current_thread_priority(ThreadPriority::Crossplatform(0.try_into().unwrap())).is_ok());
39+
}
40+
```
41+
42+
### Windows-specific examples
43+
Set the thread priority to the lowest possible value:
44+
45+
```rust,no_run
46+
use thread_priority::*;
47+
48+
fn main() {
49+
// The lower the number the lower the priority.
50+
assert!(set_current_thread_priority(ThreadPriority::Os(WinAPIThreadPriority::Lowest.into())).is_ok());
51+
}
52+
```
53+
54+
Set the ideal processor for the new thread:
55+
56+
```rust,no_run
57+
use thread_priority::*;
58+
59+
fn main() {
60+
std::thread::spawn(|| {
61+
set_thread_ideal_processor(thread_native_id(), 0);
62+
println!("Hello world!");
63+
});
64+
}
65+
```
66+
67+
68+
### Building a thread using the ThreadBuilderExt trait
69+
70+
```rust,no_run
71+
use thread_priority::*;
72+
use thread_priority::ThreadBuilderExt;
73+
74+
let thread = std::thread::Builder::new()
75+
.name("MyNewThread".to_owned())
76+
.spawn_with_priority(ThreadPriority::Max, |result| {
77+
// This is printed out from within the spawned thread.
78+
println!("Set priority result: {:?}", result);
79+
assert!(result.is_ok());
80+
}).unwrap();
81+
thread.join();
82+
```
83+
84+
### Building a thread using the ThreadBuilder.
85+
86+
```rust,no_run
87+
use thread_priority::*;
88+
89+
let thread = ThreadBuilder::default()
90+
.name("MyThread")
91+
.priority(ThreadPriority::Max)
92+
.spawn(|result| {
93+
// This is printed out from within the spawned thread.
94+
println!("Set priority result: {:?}", result);
95+
assert!(result.is_ok());
96+
}).unwrap();
97+
thread.join();
98+
99+
// Another example where we don't care about the priority having been set.
100+
let thread = ThreadBuilder::default()
101+
.name("MyThread")
102+
.priority(ThreadPriority::Max)
103+
.spawn_careless(|| {
104+
// This is printed out from within the spawned thread.
105+
println!("We don't care about the priority result.");
106+
}).unwrap();
107+
thread.join();
108+
```
109+
110+
### Using ThreadExt trait on the current thread
111+
112+
```rust,no_run
113+
use thread_priority::*;
114+
115+
assert!(std::thread::current().get_priority().is_ok());
116+
println!("This thread's native id is: {:?}", std::thread::current().get_native_id());
117+
```
118+
28119
## License
29120
This project is [licensed under the MIT license](https://github.com/vityafx/thread-priority/blob/master/LICENSE).

0 commit comments

Comments
 (0)