Click here to Skip to main content
14,336,051 members

C / C++ / MFC

 
GeneralRe: Thread synchronization problem Pin
Richard Andrew x6410-Sep-18 10:11
professionalRichard Andrew x6410-Sep-18 10:11 
GeneralRe: Thread synchronization problem Pin
samzcs10-Sep-18 10:28
membersamzcs10-Sep-18 10:28 
QuestionRe: Thread synchronization problem Pin
CPallini10-Sep-18 21:02
mveCPallini10-Sep-18 21:02 
AnswerRe: Thread synchronization problem Pin
samzcs11-Sep-18 2:45
membersamzcs11-Sep-18 2:45 
GeneralRe: Thread synchronization problem Pin
CPallini11-Sep-18 3:23
mveCPallini11-Sep-18 3:23 
AnswerRe: Thread synchronization problem Pin
leon de boer10-Sep-18 22:08
memberleon de boer10-Sep-18 22:08 
GeneralRe: Thread synchronization problem Pin
samzcs11-Sep-18 2:50
membersamzcs11-Sep-18 2:50 
GeneralRe: Thread synchronization problem Pin
leon de boer11-Sep-18 5:05
memberleon de boer11-Sep-18 5:05 
It's exactly the same you have to be manually yielding or forcibly switching or else you would never return.

So the semaphore code is exactly the same except you can ignore all the switcher enable and disable all you do is if the semaphore is locked you immediately switch to the other code (which is your version of sleep) because you can't do anything on that task.

So basically if taskA has the semaphore, then whenever you call taskB code and you want to enter the semaphore lock area you just exit and call taskA because taskB can't run until taskA gets it's fat butt out of the lock area. So in your case you would actually get a slight processing boost on taskA because it would get called twice (minus the exit and switch time) for each count loop.

The same applies if taskB has the semaphore and taskA went to enter the semaphore lock area then it would simply exit and call taskA.

It's pretty straight forward the task that is waiting on the lock is dead in the water and you just need it to give all the processing power to the other task to get it out of the lock area.

As your setup is so simple you could also just setup a state machine to mirror a semaphore, you have 5 states
1.) Neither A,B in lock area, 2.) A in lock area B out, 3.) A in B waiting, 4,) B in A out, 5.) B in A waiting
The states are changed as A & B enter and leave the lock area ...see functions in code after the modified work loop
  enum lock_state {ABoutofLockArea = 0, AinLockAreaBnormal, AinLockAreaBwaiting,  BinLockAreaAnormal, BinLockAreaAwaiting};
   
  enum lock_state state = 0;

/* MODIFIED WORK LOOP */

  while (1){
    if (count == 5)
    {
        if (state == AinLockAreaBwaiting) TaskA is called // no point calling taskB its waiting
         else task B be called
     }
     if (count == 10) 
     {
       count = 0;
       if (state == BinLockAreaAwaiting) TaskB is called // no point calling taskA its waiting
         else task A be called.
     }
   }

/* HERE ARE YOUR STATE CHANGE FUNCTIONS */

void taskA_EnterlockArea(void)
{
   if (state == BinLockAreaAnormal) state = BinLockAreaAwaiting;  // B already in there A must wait
      else state = AinLockAreaBnormal  // B is not there so A can enter
}

void taskA_LeavinglockArea(void)
{
   if (state == AinLockAreaBwaiting) state = BinLockAreaAnormal; // B is waiting it can now enter, A goes back to normal
     else state = ABoutofLockArea;   // Otherwise no one is in the lock area     
}

void taskB_EnterlockArea(void)
{
  if (state == AinLockAreaBnormal) state = AinLockAreaBwaiting;  // A already in there B must wait
      else state = BinLockAreaAnormal   // A is not there so B can enter
}

void taskB_LeavinglockArea(void)
{
   if (state == BinLockAreaAwaiting) state = AinLockAreaBnormal; // A is waiting it can now enter, B will go back to normal
     else state = ABoutofLockArea;   // Otherwise no one is in the lock area 
}

The advantage the semaphore has over the state machine, is you can add more and more semaphores with no complexity increase. As you try to lock more areas through a state machine it quickly becomes overly complex.
In vino veritas


modified 11-Sep-18 19:51pm.

GeneralRe: Thread synchronization problem Pin
11917640 Member 11-Sep-18 19:53
member11917640 Member 11-Sep-18 19:53 
GeneralRe: Thread synchronization problem Pin
samzcs12-Sep-18 8:12
membersamzcs12-Sep-18 8:12 
GeneralRe: Thread synchronization problem Pin
leon de boer12-Sep-18 18:31
memberleon de boer12-Sep-18 18:31 
GeneralRe: Thread synchronization problem Pin
11917640 Member 12-Sep-18 18:58
member11917640 Member 12-Sep-18 18:58 
AnswerRe: Thread synchronization problem Pin
«_Superman_»23-Sep-18 22:26
professional«_Superman_»23-Sep-18 22:26 
QuestionBreak when address reading Pin
Member 130813699-Sep-18 15:47
memberMember 130813699-Sep-18 15:47 
AnswerRe: Break when address reading Pin
Victor Nijegorodov10-Sep-18 2:04
memberVictor Nijegorodov10-Sep-18 2:04 
GeneralRe: Break when address reading Pin
Member 1308136910-Sep-18 15:45
memberMember 1308136910-Sep-18 15:45 
AnswerRe: Break when address reading Pin
leon de boer10-Sep-18 2:12
memberleon de boer10-Sep-18 2:12 
GeneralRe: Break when address reading Pin
Member 1308136910-Sep-18 15:49
memberMember 1308136910-Sep-18 15:49 
AnswerRe: Break when address reading Pin
Richard Andrew x6410-Sep-18 8:17
professionalRichard Andrew x6410-Sep-18 8:17 
GeneralRe: Break when address reading Pin
Member 1308136910-Sep-18 15:50
memberMember 1308136910-Sep-18 15:50 
AnswerRe: Break when address reading Pin
«_Superman_»23-Sep-18 22:30
professional«_Superman_»23-Sep-18 22:30 
Questionconfusion about fonts in Windows Pin
Alexander Kindel6-Sep-18 8:43
memberAlexander Kindel6-Sep-18 8:43 
AnswerRe: confusion about fonts in Windows Pin
leon de boer6-Sep-18 17:12
memberleon de boer6-Sep-18 17:12 
GeneralRe: confusion about fonts in Windows Pin
Alexander Kindel6-Sep-18 18:21
memberAlexander Kindel6-Sep-18 18:21 
GeneralRe: confusion about fonts in Windows Pin
leon de boer7-Sep-18 8:41
memberleon de boer7-Sep-18 8:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.