English 中文(简体)
如何规划连接库?
原标题:How to program a connection pool?

Is there a known algorithm for implementing a connection pool? If not what are the known algorithms and what are their trade-offs?
What design patterns are common when designing and programming a connection pool?
Are there any code examples implement a connection pool using boost.asio?
Is it a good idea to use a connection pool for persisting connections (not http)?
How is threading related to connection pooling? When do you need a new thread?

最佳回答

如果你寻求一种单纯的校对政策(可能是一种联系或任何资源),则有两种简单的做法:

  1. 半衰期/半衰期模型(通常使用电站通过信息)。

  2. 领导人/随行人员模式(通常使用通过信息的请求)。

第一种做法类似:

  • You create a pool of threads to handle a resource. Often this size (number of threads) needs to be configurable. Call these threads Workers .
  • You then create a master thread that will dispatch the work to the Worker threads. The application program dispatches the task as a message to the master thread.
  • The master thread puts the same on the message Q of a chosen Worker thread and the Worker thread removes itself from the pool. Choosing and removing the Worker thread needs synchronization.
  • After the Worker completes the task, it returns to the thread-pool.

主人自己可以消费FCFSprioritized上完成的任务。 这将取决于你们的执行情况。

第二个模式(Leader/eners)类似:

  • Create a thread pool. Initially all are Workers. Then elect a Leader, automatically rest-all become followers. Note that electing a Leader has to be synchronized.
  • Put all the data to be processed on a single request Q.
  • The thread-pool Leader dequeues the task. It then immediately elects a new Leader and starts executing the task.
  • The new Leader picks up the next task.

可能还有其他办法,但上文概述的办法很简单,与大多数使用案例一起工作。

5. 半天

  • Higher context switching, synchronization, and data copying overhead.

领导人/村长

  • Implementation complexity of Leader election in thread pool.

Now you can decide for yourself the more correct approach. HTH,

问题回答

暂无回答




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?