Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Semaphores

Posted on 1998-12-11
2
Medium Priority
?
182 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 400 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

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 …
Ready to get certified? Check out some courses that help you prepare for third-party exams.
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.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .

824 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