views:

26

answers:

1

Hello, 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.

+1  A: 

If createConnection is thread-safe then you don't need it.

If createConnection isn'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.

Glen
It says it's thread safe if the environment is created in THREADED_MUTEXED mode. But still, after a while, the threads get locked while creating connection.http://www.youngcow.net/doc/oracle10g/appdev.102/b14294/reference014.htm
sahs
My reading of those docs are that calls to `createEnvironment()` are thread-safe if `THREADED_MUTEXED` is used. However, nothing is said about `createConnection` being thread-safe. If you're experiencing problems and they go away when you wrap `createConnection` in a critical section, then you should leave the critical section in place.
Glen
I'm running the application with my critical sections in about 3 hours and no problem happened so far. However, I'm still in doubt because it is running much slower than that without critical sections. Also I guess it doesn't mean it's thread safe just because it doesn't get locked while testing. Am I right?
sahs