Writing to and reading from an array at the same time

Would that be possible? Say, data is being written to an array "arr" from a file AND data is being read from the array "arr" into another file at the same time, literally. In general, can you call two functions at the same time?
Yes, it is possible, however, generally it is a bad idea that two concurrent functions directly access common read-write data. What do you need that for?
Last edited on
I suggest you put that array in an object of some class and then add functionality to that class so that you can get some restrictions when to read/write to the array. For example you can use methods for push()-ing and pop()-ing elements out of the array. I also suggest lock() / unlock() methods so you can prevent the array from changes when you feel such would be harmful for it.
In general it's a bad idea to read and write to a common data at the same time. You can run into endless loops like:

while(you haven't reached the end) read current value and append it at the end

and other nasty stuff.
Last edited on
closed account (zb0S216C)
In order to perform a read and write operation simultaneously, you need threads. Also, you would require a mutex[1] (if you're using threads), which protects a section of resources while a thread read/writes to it.

References:
[1]http://en.wikipedia.org/wiki/Mutual_exclusion


Wazzak
Last edited on
I am trying to simulate Process Synchronization, wherein two threads/processes would be able to access the array simultaneously and I would employ semaphores in order to synchronize the execution of the processes.
Topic archived. No new replies allowed.