Suppose you have a number of methods that needs a rayon ThreadPool
to perform computation in parallel. The ThreadPool
specifies the amount of parallelism. You will be calling several of these methods.
I see two options:
- Each method has an argument of type
&ThreadPool
. This argument can be used or passed to other methods without further processing, but if the method returns a value it will be impossible to pass a references to a temporaryThreadPool
created on the fly, forcing the caller to define a variable containing the thread pool only for the purpose of passing a reference,. - Each method has an argument of type
Borrow<ThreadPool>
. The argument can be owned, or borrowed, or even created on the fly, but now the function has to usethread_pool.borrow()
to access the pool (an initiallet thread_pool = thread_pool.borrow()
will suffice), the only exception being when the argument is passed exactly once to another similar method.
Essentially, the first solution might require a bit more work from callers if they need to create a thread pool on the fly. The second solution makes it possible to pass thread pools created on the fly, but imposes a little burden on the callee.
Is there an idiomatic choice here? Or it is just a matter of personal taste?