I am developing a library class that uses a serial port to communicate with a control circuit. If more sensors are installed then it takes longer for the circuit to send the response string. For example, if only one sensor is installed I should wait 100ms before reading the input buffer but if 6 sensors are installed I should wait 100ms + 6 * 20ms. For some of the functions (if the maximum number of sensors are attached) I may need to wait up to 9 seconds before the controller sends the response string.
My question is should I setup multi-threading within the library class or should that responsibility be left to the client 开发者_如何学编程code?
Thanks!
It's up to you whether you want the library to be implemented in such a way that a callback happens when the process completes, or if you just want to block the thread and allow the front end developer to do what they want with multithreading.
I'd say just blocking the thread is easiest from the library's standpoint, and is probably the most expected (it sounds like a simple setup, I wouldn't expect any kind of complicated thread management built in), just make sure to document it's behavior so the next developer can easily make a choice on whether or not they want to thread it out.
It is a design question. If you design the library, you choose the solution. If your boss or somebody defined the contract then you do it as he/she wants.
I would implement both, i.e. a basic class for simple scenarios with a single sensor, and something like a multithreaded wrapper class for cases where the user wants to use more sensors with background threads.
精彩评论