As a software engineer I always find myself facing complex problems but for me the most complex ones are the ones related to multitasking, as you will have to deal with race conditions, interlocking, etc.
I use C for my daily work, so for example, if I have to create a (basic) semaphore abstraction with critical sections I'd do (pseudocode, to illustrate the question)
struct OAL_Semaphore
{
boolean is_given;
boolean is_taken;
Task_T owner;
};
struct OAL_Semaphore* OAL_Semaphore_new();
boolean OAL_Semaphore_take(struct OAL_Semaphore* s);
boolean OAL_Semaphore_give(struct OAL_Semaphore* s);
boolean OAL_Semaphore_take(struct OAL_Semaphore* s)
{
ENTER_CRITICAL
s->is_taken = TRUE;
EXIT_CRITICAL
/*Some kind of wait*/
ENTER_CRITICAL
s->is_taken = FALSE;
s->is_given = FALSE;
EXIT_CRITICAL
return TRUE;
}
boolean OAL_Semaphore_give(struct OAL_Semaphore* s)
{
ENTER_CRITICAL
if (s->is_taken) s->is_given = TRUE;
EXIT_CRITICAL
return TRUE;
}
I always keep asking myself if there is a way the program can behave that ends up going to an interlock/race condition/etc, in short, multitasking issue. And usually after thinking for some hours I don't have a clear take.
Which mental model / perspective can I use to reduce multitasking problems to a more basic scenario?