I came up with a good use of the static keyword inside a function to be something like this:
void threadSafeWrite(int *array, int writeIndex, int writeData){
static void *threadLock = Lock_create();
Lock_aquire(threadLock);
array[writeIndex] = writeData;
Lock_release(threadLock);
}
In short, this is a good way to make a critical section. My question is how to initialize threadLock in streaming mode? The problem with the example, I'm afraid, is that the lock will be allocated multiple times, and each thread will use a different lock. Any ideas how to fix this? Looks like a chicken and egg problem. I want a solution (or solutions) that works with both pthreads threads and windows.
EDIT: The reason I want this functionality is because it provides a non-intrusive way to check if there is a difference when running part of the code single-threaded or multi-threaded (intended for debugging).
source
share