I have an application where two CallBacks access a single
resource. I am told that I can use the initialize the Class
CSemaphore and then call CSingleLock::Lock at the beginning and CSingleLock::UnLock  at the end of both CallBacks. Is this true? How do I instantiate and use these functions.
IF this is the correct approach, can some one show me the
4 or 5 lines of code that initializes the CSemaphore,
and calls Lock and Unlock functions? Thanks.
Who is Participating?
Tommy HuiConnect With a Mentor EngineerCommented:
I wouldn't use a semaphore here. Instead, I would use a critical section. The idea is the same though.

First, you need to create a critical section that can be accessible from both callback functions. One possible implementation is a global instance.

CCriticalSection g_CS;

Then in your callbacks, you would do something like

void Callback1()
  // do stuff here

  // Get read to use shared resource

  // Use resource

  // Free the resource

You would have the same general piece of code for the second callback.

mparavasAuthor Commented:
Excellent idea. It worked great. Thanks a lot.
All Courses

From novice to tech pro — start learning today.