1

I have a Class File. I created two threads A and B. In A and B, each thread, I create an auto varible File myfile. And then A and B will operate it. Is that secure? Will it lead to the inconsistent of data?

William Pursell
  • 204,365
  • 48
  • 270
  • 300
Fei Xue
  • 1,995
  • 5
  • 19
  • 29
  • 1
    Can you post some code? – dunc123 Aug 08 '13 at 13:13
  • 1
    Is it the same file? And what mode(read/write) is the file opened in? – HAL Aug 08 '13 at 13:14
  • @HAL `File` is the class name and doesn't necessarily relate to `the same file on the OS`. At least that's what I think. – ATaylor Aug 08 '13 at 13:15
  • @ATaylor Yes, I was just asking for more details. – HAL Aug 08 '13 at 13:17
  • Using `File` implies that you might be doing file input/output? Writing to the same file from 2 threads can be problematic. More info: http://stackoverflow.com/questions/7842511/safe-to-have-multiple-processes-writing-to-the-same-file-at-the-same-time-cent – Felix Glas Aug 08 '13 at 13:20

3 Answers3

2

From the pure memory ressource perspective, it will depend on the scope of your File variables :

  • If they are local function variables, you are good to go. Each thread of execution owns it own stack, totally separate from other threads, where it create local variables.

  • If they are static, you are referring to the same global address.

From the file access perspective, it will depends if it is the same file, and if you are writing to it or not.

Ad N
  • 7,930
  • 6
  • 36
  • 80
1

No, that shouldn't be a problem, simply because each instance of your thread has it's own instance of the variable.

Examine the addresses of the variables to be sure, if they differ (which they will if I understand you correctly), you're fine.

Inconsistencies between threads will only occur, if two threads concurrently access the same variable. Not just 'the same name', but really the same variable.

Now, if the variable was global and both threads were to access it, you'd need some sort of mutual exclusion to prevent data corruption, but auto-variables are safe.

ATaylor
  • 2,598
  • 2
  • 17
  • 25
  • that is to say, the `myfile` in thread A does not know the existance of the `myfile` in thread B, and it has no chance to change the same name varible in the other thread, right? – Fei Xue Aug 08 '13 at 13:21
  • @ccsnailpp Yes, that's the characteristic of a local (auto) variable. It's scope is limited to the instance of the function/class it is defined in. No matter whoever else, even if it's another instance of the same function/class, it will never have access to the variables of the siblings, unless you define a getter, which you didn't. – ATaylor Aug 08 '13 at 13:23
1

The variables will be distint from each other as each thread will have its own stack, and each instance of the variable will live on that stack.

However, it sounds like you're accessing an underlying filesystem file. If it's the same file and one or both of the threads are writing to the file then this may cause data consistency issues. You'll need to consult the documentation for your operating system to see what it says. If both threads are just reading from the same file then you'll be fine.

Sean
  • 60,939
  • 11
  • 97
  • 136
  • A little question: This sounds like a classical race condition, does it not? Usually, once a file is opened, the application obtains a 'Lock', which means that opening the same file again with a different process/thread will fail, will it not? – ATaylor Aug 08 '13 at 13:25
  • @ATaylor - it depends. It's certainly possible for two threads/processes to open the same file for reading. – Sean Aug 08 '13 at 13:39