Click here to Skip to main content
15,921,841 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: many CMutex objects: good idea? Pin
peterchen2-Jun-04 9:03
peterchen2-Jun-04 9:03 
GeneralRe: many CMutex objects: good idea? Pin
berndg2-Jun-04 9:12
berndg2-Jun-04 9:12 
GeneralRe: many CMutex objects: good idea? Pin
Daniel Turini2-Jun-04 10:17
Daniel Turini2-Jun-04 10:17 
GeneralRe: many CMutex objects: good idea? Pin
Alexander M.,2-Jun-04 12:54
Alexander M.,2-Jun-04 12:54 
GeneralRe: many CMutex objects: good idea? Pin
Tim Smith2-Jun-04 15:56
Tim Smith2-Jun-04 15:56 
AnswerRe: many CMutex objects: good idea? Pin
Andy Brummer2-Jun-04 10:21
sitebuilderAndy Brummer2-Jun-04 10:21 
AnswerRe: many CMutex objects: good idea? Pin
Alexander M.,2-Jun-04 12:55
Alexander M.,2-Jun-04 12:55 
AnswerRe: many CMutex objects: good idea? Pin
Tim Smith2-Jun-04 15:49
Tim Smith2-Jun-04 15:49 
As already pointed out, using mutexes is an overkill unless the locks are shared between processes. Use a critical section which is much faster but it still creates system objects (an event flag is created when a lock request is blocked.)

Having many critical sections can hurt system performance, but not by much. It isn't like NT maintains a link list of system objects and has to cycle though them to locate the system object. I probably uses a O(1) algorithm that is very fast.

The question is why do you need many locks instead of one. To answer that, you really need to know how your program is handling threading. If you don't have many threads, what is the chance that two threads will be needing the lock at one time? How long is the lock being held? Will one thread need to acquire more than one lock at a time?

The more threads you have accessing these objects and the longer the lock needs to remain locked, the more you need a more granular locking system. Instead of locking the whole object pool, you lock individual objects. Thus greatly reducing the chance of two threads locking the same lock. However, this really only works if the distribution of your objects needing to be locked is spread out. If you have just one or two objects that are being locked all the time while all the others are rarely locked, then this won't help much.

As far as needing to have multiple objects locked at one time, if you have this requirement you must start to consider deadlocks. A deadlock happens when thread A locks lock 1. Then thread B locks lock 2. Then thread A requests lock 2 while thread B requests lock 1. Neither thread can continue because each is waiting on a lock already acquired by the other thread. There are multiple ways to handle this. However, dealing with this type of problem is generally nasty.

Tim Smith

I'm going to patent thought. I have yet to see any prior art.
GeneralRe: many CMutex objects: good idea? Pin
berndg2-Jun-04 20:03
berndg2-Jun-04 20:03 
GeneralGet time zone info for a network computer. Pin
LizardWiz2-Jun-04 8:40
LizardWiz2-Jun-04 8:40 
GeneralRe: Get time zone info for a network computer. Pin
David Crow2-Jun-04 9:27
David Crow2-Jun-04 9:27 
GeneralRe: Get time zone info for a network computer. Pin
LizardWiz2-Jun-04 11:22
LizardWiz2-Jun-04 11:22 
GeneralRe: Get time zone info for a network computer. Pin
David Crow3-Jun-04 2:53
David Crow3-Jun-04 2:53 
QuestionWhy is my overlapped WriteFile blocking? Pin
JT Anderson2-Jun-04 8:28
JT Anderson2-Jun-04 8:28 
Generalstrng to CString Pin
kfaday2-Jun-04 8:00
kfaday2-Jun-04 8:00 
GeneralRe: strng to CString Pin
David Crow2-Jun-04 8:04
David Crow2-Jun-04 8:04 
GeneralRe: strng to CString Pin
kfaday2-Jun-04 8:29
kfaday2-Jun-04 8:29 
Questionhow to create a pop up menu Pin
kfaday2-Jun-04 7:10
kfaday2-Jun-04 7:10 
AnswerRe: how to create a pop up menu Pin
David Crow2-Jun-04 7:33
David Crow2-Jun-04 7:33 
GeneralRe: how to create a pop up menu Pin
kfaday2-Jun-04 8:51
kfaday2-Jun-04 8:51 
GeneralRe: how to create a pop up menu Pin
David Crow2-Jun-04 9:31
David Crow2-Jun-04 9:31 
GeneralRe: how to create a pop up menu Pin
kfaday2-Jun-04 16:51
kfaday2-Jun-04 16:51 
GeneralRe: how to create a pop up menu Pin
David Crow3-Jun-04 2:23
David Crow3-Jun-04 2:23 
GeneralRe: how to create a pop up menu Pin
kfaday3-Jun-04 2:26
kfaday3-Jun-04 2:26 
GeneralRe: how to create a pop up menu Pin
David Crow3-Jun-04 2:44
David Crow3-Jun-04 2:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.