Solved

Semaphores

Posted on 1998-12-11
2
160 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
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 demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

757 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

21 Experts available now in Live!

Get 1:1 Help Now