How to use CMutex, CSemaphore in VC++ MFC



How to use CMutex, CSemaphore in VC++ MFC..

Answer / Ravi Prakash Gupta

CMutex and CSemaphore are used for synchronization in Multi-Threaded Programming in Microsoft Foundation Classes (MFC). Here's an example of how to use a CMutex: 1. Include the necessary header files: #include <afxmt.h>
2. Create a global or local CMutex object:nCMutex m_mutex;
3. Lock the mutex before critical sections of code: m_mutex.Lock();
4. Unlock the mutex after the critical section:nm_mutex.Unlock();nFor CSemaphore, you can use it to limit the number of threads accessing a particular resource. Here's an example: 1. Include the necessary header files: #include <afxmt.h>
2. Create a global or local CSemaphore object:nCSemaphore m_sem(5); // Initialize with 5 available semaphores
3. Wait for a semaphore before accessing a resource: m_sem.Lock();
4. Release the semaphore after completing the critical section: m_sem.Unlock();

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More OOPS Interview Questions

They started with the brief introduction followed by few basic C++ questions on polumorphism, inheritance and then virtual functions. What is polymorphims? How you will access polymorphic functions in C? How virtual function mechanism works?

1 Answers  


What exactly is polymorphism?

1 Answers  


how to find the correct email address format by using the programe?

1 Answers   Accenture,


What is object in oops?

1 Answers  


What is difference between #define and const?

3 Answers   emc2,


What are the four main oops concepts?

1 Answers  


What is a unary operator?

5 Answers  


what isthe difference between c structure and c++ class

5 Answers  


What is abstraction in oop with example?

1 Answers  


pointers are support in C#? if yes then how to use it?

8 Answers   Softvision Solution,


what is abstract class ? when is used in real time ? give a exp

5 Answers  


can you give real time example for polymarphism

5 Answers   TCS,


Categories