Using CRITICAL_SECTION for different class instances
Posted on 2001-06-09
I have some
// Pointer to some data used by this class instance
// Attach data pointer to the class instance
void AttachResourcePointer(void* p)
if(!p) m_pResourcePointer = p;
// Do some resource processing
// Do something with m_pResourcePointer
// Critical Section
static CRITICAL_SECTION m_CS;
and I use several instances of this class in my program:
ResourceUser RU1, RU2;
Here p1 and p2 are some resource pointers (FILE*, for example), used by the two instances of ResourceUser. The problem is that both RU1 and RU2 are accessed by several threads in my program, and to make it safe, I use critical section m_CS in methods like ProcessResource() as shown above. However, RU1 And RU2 are using different resources ? so, if I declare m_CS as static to make it globally unique, any thread access to RU1 resource will also block the RU2 resource ? I do not want this to happen. How can I fix that? What happens, for instance, if I remove ?static? from the m_CS declaration ? will this contradict the usage of CRITICAL_SECTION ? In other words, how do I declare/use the critical section in my class so that using it in one class instance will not block another class instance ?
I certainly do not want to declare several static m_CS1, m_CS2 etc. just to use each particular m_CSn for its instance only ? the number of class instances in use changes dynamically.