Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need Suggestion/Help on semaphore

Status
Not open for further replies.

aMember

Programmer
Jun 12, 2002
99
0
0
US
First, what would be a good book/reference for using VC++ and for learning the libraries of C++?

I'm new to C++ and have never used VC++ before. I've tried to coordinate 3 tasks using semaphores but am having difficulty getting the synchronization correct. I am trying to work the problem from home now instead of work so cannot post the code. Have MS VC++ at home but do not know where to start. Only have online help and find that a bit convoluted. I feel like I'm chasing my tail with it.

Class B is a counting semaphore initialized to 1 in the CTOR.
Class C is a binary semaphore (mutex with ownership, I think)

I can't get the synchronization between Classes to work.
Task A runs continuously.
Task C runs continuously.
Task B runs only on the user's command.

When Task B is commanded to run, it must call invoke operations on Class C to suspend.

Task A can also invoke that same operation to suspend and also resume the task.

In the CTOR of Class A, an object of Class C is created then an object of Class B is created, passing the pointer to Class C.

In the CTOR of Class B & C, the tasks are initialized and run

Can't post the code as I'm not at work right now (how dedicated is that!) but this is the gist of it.

Is there enough info here to make the flaw evident? Any advice would be appreciated.
Dana

-----------------
|----can sem_give ---| | ---can sem_take/give
| | Class A | |
| --has a----- | | ---has a |
| | ----------------- | |
\ / \ / \ / \/
--------------- ------------------
| Class B |-- can sem_take/give --> | Class C |
| | | |
--------------- -----------------


========================================
Class_B::task_execution()
{
while (true)
{
// pend on a semaphore until the user (class A)
// makes a call to release the semaphore
// allowing this take to succeed
//
sem_take() Class B semaphore


// wait here until class C task suspends
//
sem_take() Class C semaphore

//////////////////////////////
// do Class B task stuff here
//////////////////////////////


// Call Class_C task to resume
//
sem_give() Class C semaphore


// suspend class B task until next time
//
sem_take class_B semaphore

// wait here to free some CPU

}

}

====================================================
Both Class B and Class have these same operations - suspend and resume:
=====
::suspend()
{
// Take the semaphore to temporarily
sem_take() the Class semaphore
}

=============
::resume()
{
// Give up the semaphore
sem_give() the class semaphore

}

====================================================





Class_C task is (simplified)
========================================

Class_c::task_execution()
{
while (true)
{
// take it's own semaphore - if successful,
// task has not been suspended by another task
class_c_semaphore->take();

// do stuff here

// give up the semaphore to allow other tasks
// to suspend it if necessary
class_c_semaphore->give();

// wait here to free some CPU

}
}

========================================


Class_A::member_function_1(const int user_choice)
{
switch (user_choice)
{
case RUN_TASK_B :
sem_give the Class B semaphore;
break;
case SUSPEND_TASK_C:
sem_take the Class C semaphore;
break;
case RESUME_TASK_C:
sem_give the class C semaphore;
break;

}
}


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top