@@ -1427,39 +1427,76 @@ fn _assert_sync_and_send() {
1427
1427
_assert_both :: < Thread > ( ) ;
1428
1428
}
1429
1429
1430
- /// Returns the number of hardware threads available to the program.
1431
- ///
1432
- /// This value should be considered only a hint.
1433
- ///
1434
- /// # Platform-specific behavior
1435
- ///
1436
- /// If interpreted as the number of actual hardware threads, it may undercount on
1437
- /// Windows systems with more than 64 hardware threads. If interpreted as the
1438
- /// available concurrency for that process, it may overcount on Windows systems
1439
- /// when limited by a process wide affinity mask or job object limitations, and
1440
- /// it may overcount on Linux systems when limited by a process wide affinity
1441
- /// mask or affected by cgroups limits.
1430
+ /// Returns an estimate of the default amount of parallelism a program should use.
1431
+ ///
1432
+ /// Parallelism is a resource. A given machine provides a certain capacity for
1433
+ /// parallelism, i.e., a bound on the number of computations it can perform
1434
+ /// simultaneously. This number often corresponds to the amount of CPUs or
1435
+ /// computer has, but it may diverge in various cases.
1436
+ ///
1437
+ /// Host environments such as VMs or container orchestrators may want to
1438
+ /// restrict the amount of parallelism made available to programs in them. This
1439
+ /// is often done to limit the potential impact of (unintentionally)
1440
+ /// resource-intensive programs on other programs running on the same machine.
1441
+ ///
1442
+ /// # Limitations
1443
+ ///
1444
+ /// The purpose of this API is to provide an easy and portable way to query
1445
+ /// the default amount of parallelism the program should use. Among other things it
1446
+ /// does not expose information on NUMA regions, does not account for
1447
+ /// differences in (co)processor capabilities, and will not modify the program's
1448
+ /// global state in order to more accurately query the amount of available
1449
+ /// parallelism.
1450
+ ///
1451
+ /// The value returned by this function should be considered a simplified
1452
+ /// approximation of the actual amount of parallelism available at any given
1453
+ /// time. To get a more detailed or precise overview of the amount of
1454
+ /// parallelism available to the program, you may wish to use
1455
+ /// platform-specific APIs as well. The following platform limitations currently
1456
+ /// apply to `available_parallelism`:
1457
+ ///
1458
+ /// On Windows:
1459
+ /// - It may undercount the amount of parallelism available on systems with more
1460
+ /// than 64 logical CPUs. However, programs typically need specific support to
1461
+ /// take advantage of more than 64 logical CPUs, and in the absence of such
1462
+ /// support, the number returned by this function accurately reflects the
1463
+ /// number of logical CPUs the program can use by default.
1464
+ /// - It may overcount the amount of parallelism available on systems limited by
1465
+ /// process-wide affinity masks, or job object limitations.
1466
+ ///
1467
+ /// On Linux:
1468
+ /// - It may overcount the amount of parallelism available when limited by a
1469
+ /// process-wide affinity mask, or when affected by cgroup limits.
1470
+ ///
1471
+ /// On all targets:
1472
+ /// - It may overcount the amount of parallelism available when running in a VM
1473
+ /// with CPU usage limits (e.g. an overcommitted host).
1442
1474
///
1443
1475
/// # Errors
1444
1476
///
1445
- /// This function will return an error in the following situations, but is not
1446
- /// limited to just these cases:
1477
+ /// This function will, but is not limited to, return errors in the following
1478
+ /// cases:
1447
1479
///
1448
- /// - If the number of hardware threads is not known for the target platform.
1449
- /// - The process lacks permissions to view the number of hardware threads
1450
- /// available.
1480
+ /// - If the amount of parallelism is not known for the target platform.
1481
+ /// - If the program lacks permission to query the amount of parallelism made
1482
+ /// available to it .
1451
1483
///
1452
1484
/// # Examples
1453
1485
///
1454
1486
/// ```
1455
1487
/// # #![allow(dead_code)]
1456
1488
/// #![feature(available_parallelism)]
1457
- /// use std::thread;
1489
+ /// use std::{io, thread} ;
1458
1490
///
1459
- /// let count = thread::available_parallelism().map(|n| n.get()).unwrap_or(1);
1491
+ /// fn main() -> io::Result<()> {
1492
+ /// let count = thread::available_parallelism()?.get();
1493
+ /// assert!(count >= 1_usize);
1494
+ /// Ok(())
1495
+ /// }
1460
1496
/// ```
1497
+ #[ doc( alias = "available_concurrency" ) ] // Alias for a previous name we gave this API on unstable.
1461
1498
#[ doc( alias = "hardware_concurrency" ) ] // Alias for C++ `std::thread::hardware_concurrency`.
1462
- #[ doc( alias = "available_concurrency " ) ] // Alias for a name we gave this API on unstable .
1499
+ #[ doc( alias = "num_cpus " ) ] // Alias for a popular ecosystem crate which provides similar functionality .
1463
1500
#[ unstable( feature = "available_parallelism" , issue = "74479" ) ]
1464
1501
pub fn available_parallelism ( ) -> io:: Result < NonZeroUsize > {
1465
1502
imp:: available_parallelism ( )
0 commit comments