Shared db pool
Benjamin Thaut
code at benjamin-thaut.de
Mon Mar 18 00:28:41 PDT 2013
Am 17.03.2013 22:21, schrieb Alex Khmara:
> My task involves many worker threads, each of them uses Curl instance to
> do one or several requests and one Mysql connection instance to store
> data and do some aggregations. ALso sometimes these threads do other work
> that don't require these resources.
>
> So I want to make two pools (one for Curl and one for Mysql connections),
> so that number of DB connections (or Curl instances) will be lower than
> number of threads, so I cannot just use DataPool.WorkerLocalStorageRange.
>
> So I'm trying to create shared pool. And there arises question:
>
> if I have this code:
>
>
> class SharedPool {
>
> ...
> Mysql* get() {
> ...
> return cast(Mysql*) connections[freeIndex];
> }
> ...
> Mysql*[] connections;
> }
>
> shared SharedPool pool;
>
>
> when I will get syncronization overhead: only on access to SharedPool or
> on every access to Mysql instance?
>
> What I really want - is to NOT have any shared-related code after getting
> some Mysql instance and before returning it to pool, so Mysql instances
> must be essentially non-shared. Is it possible?
>
Your current code will not work at all, because you can not call get()
from shared instance of the class (because get is not a shared method).
Also your current code does not have any synchroization overhead. Just
adding shared to something does not mean that there will be any
synchronization added automatically (at least not yet). You need to add
synchronization yourself for example by using a "synchronized(this) {
... }" block.
Kind Regards
Benjamin Thaut
More information about the Digitalmars-d-learn
mailing list