• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2023 Huawei Device Co., Ltd.
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 //     http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 //! Multi_thread RuntimeBuilder usage.
15 #![cfg(not(feature = "ffrt"))]
16 use ylong_runtime::builder::RuntimeBuilder;
17 
18 // Simple asynchronous tasks
test_future(num: usize) -> usize19 async fn test_future(num: usize) -> usize {
20     num
21 }
22 
main()23 fn main() {
24     // Creates multi-instance asynchronous thread pools
25     // Sets the maximum capacity of the asynchronous thread pool
26     let core_pool_size = 4;
27     // Sets whether the asynchronous thread pool is tied for core processing
28     let is_affinity = true;
29     // Creates runtime environment first time
30     let runtime_one = RuntimeBuilder::new_multi_thread()
31         .is_affinity(is_affinity)
32         .worker_num(core_pool_size)
33         .build()
34         .unwrap();
35 
36     // Sets the maximum capacity of the asynchronous thread pool
37     let core_pool_size = 4;
38     // Sets whether the asynchronous thread pool is tied for core processing
39     let is_affinity = true;
40     // Creates runtime environment second time (only asynchronous thread pools are
41     // supported)
42     let runtime_two = RuntimeBuilder::new_multi_thread()
43         .is_affinity(is_affinity)
44         .worker_num(core_pool_size)
45         .build()
46         .unwrap();
47 
48     // Gets the respective task hooks
49     let handle_one = runtime_one.spawn(test_future(1));
50     let handle_two = runtime_two.spawn(test_future(2));
51 
52     // Gets task execution results via hooks
53     let _result_one = runtime_one.block_on(handle_one).unwrap();
54     let _result_two = runtime_two.block_on(handle_two).unwrap();
55 }
56