开发者

is it acceptable to use ThreadPool in a library?

开发者 https://www.devze.com 2022-12-08 14:05 出处:网络
is it acceptable to use Threa开发者_高级运维dPool in a library? because that obviously might cause some unpleasant problems if the user of your library is using ThreadPool as well (due to ThreadPool

is it acceptable to use Threa开发者_高级运维dPool in a library?

because that obviously might cause some unpleasant problems if the user of your library is using ThreadPool as well (due to ThreadPool being a static class of course)..

what's the convention?


Yes.I think it is appropriate to make use of the ThreadPool in library code. Even if the user may use ThreadPool outside, ThreadPool is still good enough to tune itself.

On the other hand, as a library developer, you should provide flexibility: user may choose to use ThreadPool, a specific thread(s), or even a 3rd party thread pool implementation.


Yes.

As long as it's well documented, and you provide methods to allow the user of the library to control the threadpool, such as min/max threads and maybe the option to not use a threadpool at all.

You should also make it very clear which exposed parts of your library are threadsafe and which are not.


ThreadPool is designed to be used by multiple components simultaneously. So it in itself presents no particular problem if used from your particular library.

What can be a problem is threading behavior in general in your library. It must be clearly documented what the threading semantics of your library are. How these threads are created and used should should be an implementation detail. The ThreadPool itself shouldn't present a problem unless one of it's inherent properties (COM apartment affinity, inability to cancel threads, etc ...) presents a problem for your API or consumers.

0

精彩评论

暂无评论...
验证码 换一张
取 消