Solved

Semaphores

Posted on 1998-12-11
2
162 Views
Last Modified: 2013-11-20
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.
0
Comment
Question by:mparavas
2 Comments
 
LVL 15

Accepted Solution

by:
Tommy Hui earned 100 total points
ID: 1326066
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
  g_CS.Lock();

  // Use resource

  // Free the resource
  g_CS.Unlock();
}

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

0
 

Author Comment

by:mparavas
ID: 1326067
Excellent idea. It worked great. Thanks a lot.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Unix / Linux grid computing 5 148
matchUp  challenge 6 53
wordcount challenge 11 108
wordmultiple challenge 12 119
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now