Answers

Question and Answer:

  Home  Windows Threads

⟩ Which one you will use to implement critical section?

Now sure what is asked but anyway:

1. If the question is how would you implement critical

section mechanism then the answer is to use a mutex.

2. If the question is to show how to use the critical

section then the following code fragment adresses that:

//

// Global variable or a member of some

// dynamically allocated structure, usualy within

// the data access to which it is protecting.

//

CRITICAL_SECTION cs;

VOID Init(VOID) {

InitializeCriticalSection( &cs );

}

VOID SomeCall(VOID) {

EnterCriticalSection( &cs );

//

// Thread-safe block of code protected by critical

section.

//

LeaveCriticalSection( &cs );

}

VOID

UnInit(VOID) {

DeleteCriticalSection( &cs );

}

 192 views

More Questions for you: