Click to See Complete Forum and Search --> : Thread Synchronization


joscollin
July 4th, 2005, 03:17 AM
Which is the efficient method to Synchronize the access to a file by two threads in different processes?

Andreas Masur
July 4th, 2005, 07:12 AM
[ Redirected thread ]

MikeAThon
July 4th, 2005, 01:39 PM
It depends what you want to do.

One way is to make a rule for yourself that anytime a thread uses the file handle, that it surrounds the code with a Lock/Unlock on a critical section that's common between the threads.

Tell us more about your objectives.

Mike

kuphryn
July 4th, 2005, 10:34 PM
Correct. Critical section is fast and is a good solution for access control across threads.

Kuphryn

pradish
July 5th, 2005, 12:25 AM
Hi colly


CriticalSection is faster because it works in the user mode not in the kernal mode like other sychronization object but it cannot be used across processes.

you can use a semaphore and treat the File as a resource

use CreateSemaphore and its parmeters initial resource count and maximum resource count intelligently to get what you want.

thanks and regards
pradish

MikeAThon
July 5th, 2005, 02:05 AM
Which is the efficient method to Synchronize the access to a file by two threads in different processes?
OK, I didn't read carefully enough. Since they're different processes, you can't use a critical section.

Use a semaphore (as suggested by pradish) or a mutex.

Mike

kuphryn
July 5th, 2005, 11:11 AM
I'm blind. In that case, I feel one possible solution is mutex, as mentioned already. If you're access file via Win32 API, then check out MSDN to see if there the API has support for synchronization across processes.

Kuphryn

Axter
July 6th, 2005, 03:45 AM
You can use a wrapper class to wrap your file handle.
Check out the following link:

http://code.axter.com/sync_ptr.h
http://code.axter.com/sync_ctrl.h

The sync_ptr class acts like a pointer to your object, and automatically locks and unlocks the object when you access it.
It can be used on Win32 and UNIX/Linux targets, and with Win32 you can use either critical_section logic or mutex, which is what you would want for your paticular requirements.