This question was tossed as a starting point in a technical interview at a semi-conductor company. I guess there are no right ans - the purpose of this open ended question is to gauge the engineer's depth and comfort level. Question:- design an API which monitors an address and returns when the address contains a specific value:-
void reach_target_value(volatile int* addr, int value);
My naive solution was something like this:-
void reach_target_value(volatile int* addr, int value)
{
while(*addr != value)
{
//Do nothing - spin in a tight loop
}
}
Now obviously in a multi processing/ multi threaded environment, it might miss to return if the address happens to reach the target value when the executing process/thread is context switched. Leaving this case how to enhance the code to ensure it works well in a multi-threaded environment where multiple threads would be monitoring the same address with different target values? Thanks for reading - would appreciate your suggestions - code - pointers!