Cleaner way of working with a shared resource?

Chris M. chrismohrfeld at comcast.net
Thu Mar 29 17:41:15 UTC 2018


I'm working with mysql-native for a project, and have been using 
a single, shared Connection 
(http://semitwist.com/mysql-native-docs/v2.2.0/mysql/connection/Connection.html) among multiple threads. The issue here is that since it's shared, I can't use certain functions such as exec() or close() since they're not shared.

I've been doing the following as a workaround. It's not a huge 
deal to do, but is this the best way to accomplish this? Short of 
going through the library and making the functions shared myself 
of course.

shared Connection con = ...;

void closeDatabase()
{
     synchronized
     {
         auto _con = cast(Connection) con;

         if (!_con.closed)
             _con.close();
     }
}


More information about the Digitalmars-d-learn mailing list