Can both threads of a dual threaded program access a given file?

Each sets a global flag 'write_' when it wishes to write, and the other should wait for access at that time. Problem the program crashes each time - one thread crashes at least, but Windows indicates that the program has faulted and must be closed!!!

Each thread also sets another global flag when it updates a storage location... This apparently works, but the file access does not.
Posted on 2005-03-28 00:08:55 by V Coder
Don't use "global flags", use proper threadsafe methods like mutexes or critical sections...
Posted on 2005-03-28 04:00:36 by f0dder
i recommend:

BOOL InitializeCriticalSectionAndSpinCount(
? LPCRITICAL_SECTION lpCriticalSection,
? DWORD dwSpinCount
);

Windows 2000:? If the high-order bit is set, the function preallocates the event used by the EnterCriticalSection function. Do not set this bit if you are creating a large number of critical section objects, because it will consume a significant amount of nonpaged pool. This flag is not necessary on Windows XP and later, and it is ignored.


Global flags are supposed to crash, when accessed by two different CPUs at the same time, aren't they..?
Posted on 2005-03-28 11:40:12 by ti_mo_n
Don't use "global flags", use proper threadsafe methods like mutexes or critical sections...


This seems an overkill if one just needs to share a flag. One could use InterlockedIncrement() and InterlockedDecrement(). I agree, however, that the OP should look into synchronization mechanisms, namely critical sections and events (or better yet, conditions).

BTW, sometimes even plain flags fit, but not in this case, I suspect.
Posted on 2005-03-28 21:01:32 by death
I should have replied some time ago. There was a problem in the assembler. The crashing stopped on an update.

The intention is pure speed - but repeated access to the same area of memory may slow down the bus....

Flags as above seem a good soution: quit, wait, write. There are exactly (no more or less) two threads. On WM_CLOSE etc... the quit flag is set to 1, the first (controller) thread increments it to 2, and after completing any tasks, the second thread sets it to 3. Both enter terminal cycling. WND_PROC then saves the program data and quits, closing both threads.

When either thread accesses the main data area it sets wait. Then it processes. It usually will not need to write based on the results, so go back to main data area. Or else set write flag, and so on.



Posted on 2005-06-10 01:53:30 by V Coder
The real goal is to not use synchonisation at all, but thats probably overkill.
Try using overlapped IO on the files, the win32 system will keep it thread safe.
If you need to make sure a write is available for the threads next read, then that a little
more difficult.

Maybe you can describe your need to have more than one thread access a file ?
Posted on 2005-06-10 17:29:46 by James_Ladd

Global flags are supposed to crash, when accessed by two different CPUs at the same time, aren't they..?


No, that's a programmer's myth ;) But they do lead to synchronization problems, since it's value might not be kept consistent when accessed by more than one thread (or CPU) at the time.
Posted on 2005-06-13 14:59:42 by QvasiModo
The difficulty lies in updating the shared resource, whether it's the global variable or the file, under preemptive conditions. You want the test and update of a mutex to occur together without interruption. Otherwise, you've got yourself a synchronization error. Most OS textbooks will go through how to synchronize when you don't have that "atomic" test-and-update, by showing basic synchronization code that doesn't work, and going through various fixes that also don't work, until they show a version that works. The result for two tasks is Dekker's solution, and Dijkstra has a multitask extension of it.
Posted on 2005-06-14 00:54:13 by tenkey