Click here to Skip to main content
15,887,683 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Thread synchronization problem Pin
Richard Andrew x6410-Sep-18 9:35
professionalRichard Andrew x6410-Sep-18 9:35 
GeneralRe: Thread synchronization problem Pin
samzcs10-Sep-18 10:09
samzcs10-Sep-18 10:09 
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
samzcs10-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
samzcs11-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
leon de boer10-Sep-18 22:08 
You called this thread but you describe these as tasks and I read your other responses ... so I am assuming this is a task switcher.

So I am guessing you are on a pre-emtive task switcher that is running multiple tasks.
If that is the case usually you synchronize by stopping the task switcher for switching for a short period as you have a critical section.

For example of a context switcher running off a timer interrupt, you just disable the interrupts which will stop the context switcher from switching your task out. On all context switchers there is a way to do this because at various points in the kernel they have to enter critical sections. You need to keep your time inside these sections as fast as you possibly can.

Now if it is going to take to long you need to use locks. They start simple and grow in complexity as you want different things.
Even on a basic task switcher it should have a semaphore because they are so easy to define or you can make one just using a volatile int.
typepdef volatile int semaphore;

The volatile is important because you are going to end up looping around reading the same address. You need to make sure the compiler doesn't optimize that to only reading it once and using the old value for each of the check loops. It must read the memory location each and every time.

To enter a semaphore locked area generally you will make a call to something called a name like semaphore_inc or semaphore_pend.
What that will do is make sure you have single aquire on the semaphore, if you don't it will sleep your task it will basically be doing this
void semaphore_inc (semaphore* sem) 
{
   disable context switcher;
   while (*sem != 0)
   {
      enable context switcher;
      sleep(1);
      disable context switcher;
   }
   *sem++;
   enable context switcher;
}

The disable and enable context switcher will be simple disable and enable interrupt opcodes on a simple timer interrupt system up to a small piece of code on more advanced switchers. What is important is do you get what happens with your task getting sleeped unless it has solo access to the semaphore. The 1 will be the minimum unit of time on the task switcher until your task can be woken up. It's non blocking because the sleep call will activate the next task to run (because you can't and so you give up your timeslice) and it will come back to you sometime later on another context switch. The moment you set the semaphore any other task trying to enter would have the same happen to it because they would ask for the aquire in the same way. You can see it is important the context switcher get turned off while various parts of the test are done, you don't want to be pre-emptively switched out while you are testing.

Now once you have finished you call something like semaphore_dec, or semaphore_release which will set the semaphore back to zero again turning off the context switcher where it needs. Once you exit other tasks may enter or they will wake from a sleep and find they can go in if they were trying to aquire lock. If you have to write the code for the release it is a really simple code
void semaphore_dec (semaphore* sem)
{
   disable context switcher;
   *sem--;
   enable context switcher;
}

So it ends up like this taskACall and taskBCall can never run at same time but they are non blocking. A task waiting to aquire the lock gives up it's timeslice to other tasks which will include the task currently in the locked area. Eventually the task in the locked area will come out and the waiting task will be able to enter but will lock the original task from re-entering until it leaves.
semaphore sem = 0;

void taskACall (void)
{
   semaphore_inc(&sem);
   .... // do task A code
   semaphore_dec(&sem);
}

void taskBCall (void)
{
   semaphore_inc(&sem);
   .... // do task B code
   semaphore_dec(&sem);
}

Now some CPU's and some systems have semaphores inbuilt into the MMU and C11 added atomics as a standard #include <stdatomic.h>.
So on different systems if you have to add the behaviour yourself there is a standard way to do it.

Without knowing more specifics that is about all I can tell you.
In vino veritas


modified 11-Sep-18 5:03am.

GeneralRe: Thread synchronization problem Pin
samzcs11-Sep-18 2:50
samzcs11-Sep-18 2:50 
GeneralRe: Thread synchronization problem Pin
leon de boer11-Sep-18 5:05
leon de boer11-Sep-18 5:05 
GeneralRe: Thread synchronization problem Pin
11917640 Member 11-Sep-18 19:53
11917640 Member 11-Sep-18 19:53 
GeneralRe: Thread synchronization problem Pin
samzcs12-Sep-18 8:12
samzcs12-Sep-18 8:12 
GeneralRe: Thread synchronization problem Pin
leon de boer12-Sep-18 18:31
leon de boer12-Sep-18 18:31 
GeneralRe: Thread synchronization problem Pin
11917640 Member 12-Sep-18 18:58
11917640 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
Member 130813699-Sep-18 15:47 
AnswerRe: Break when address reading Pin
Victor Nijegorodov10-Sep-18 2:04
Victor Nijegorodov10-Sep-18 2:04 
GeneralRe: Break when address reading Pin
Member 1308136910-Sep-18 15:45
Member 1308136910-Sep-18 15:45 
AnswerRe: Break when address reading Pin
leon de boer10-Sep-18 2:12
leon de boer10-Sep-18 2:12 
GeneralRe: Break when address reading Pin
Member 1308136910-Sep-18 15:49
Member 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
Member 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
Alexander Kindel6-Sep-18 8:43 
AnswerRe: confusion about fonts in Windows Pin
leon de boer6-Sep-18 17:12
leon de boer6-Sep-18 17:12 

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.