I’m writing a multi-threaded application. My worker threads get connection from an environment object as follows:.
//EnterCriticalSection(&cs);
conn = env->createConnection(username, password, connStr);
//LeaveCriticalSection(&cs);
For concurrency, should the connection be created in a critical section or not? Does the env need it? And why?
Thanks.
If
createConnectionis thread-safe then you don’t need it.If
createConnectionisn’t thread-safe then you do need the critical section.Consult your documentation to see whether it’s thread-safe or not. If it doesn’t explicitly say it’s thread-safe, them play it safe and wrap it in a critical section.
Edit: Of course, all of the above assumes that multiple threads will be calling
createConnection. If they’re not, then obviously you won’t need the critical section at all.