0

I don't study this area of computing to be honest. Actually my references is some web and academic articles then I'm insecure but curious about some concepts of parallel computing.

I've formulated two sentences and would like to validate it.


First one:

Imperative languages uses variables to abstract hardware memory of a computer. If there is two parallel threads and at least one performs a write, without synchronization mechanisms, a data race happens.

We can consider that data races are intrinsic to Imperative Programming paradigm?


Second one:

Data races can produces unexpected results. Since data races occurs with multiple parallel threads, then they are a problem of multithreading ability.

We can consider that data races is an obstacle not only specifically to multithreading but for parallel computing in general?


My real goal is write some text correlating Imperative Programming and Parallel Processing to explain the benefits of Functional Programming. Any correction and further information is welcome.

Alexandre Thebaldi
  • 4,546
  • 6
  • 41
  • 55

1 Answers1

0

Data races are about event chronology, and aren't even particular to statements in code, consider the following (single-threaded) JavaScript:

let fs = require('fs');
let dirContents = fs.readdirSync('./');
let files = dirContents.map(fname => fs.readFileSync(fname, 'utf-8'));

This code contains a data race, not because of anything being done by the code itself, but because some other program could have come along and deleted the last file in the list between the time we (sequentially) started reading the first file and the time we try to read that last one. The code is deceptively short but there's an imperative iteration still occuring under the hood in both lines 2 and 3.

And JavaScript despite being single-threaded is full of concurrency, the reason the above fs functions have 'Sync' in the name is because the default versions are asynchronous and its surprisingly easy to introduce race conditions in the code itself in JavaScript. So threads are a red herring, they just make it arguably more likely that data races exist in the code, their absence proves nothing.

There are only two* ways to work around this problem. One is to use resource locks to attempt to ensure that events happen in an expected order. Another is to ensure that nothing ever changes in place. And that is the functional programming approach: concepts like immutable data, the whole program is one big expression rather than a series of steps, etc.

*There is actually another way, you can construct a formal proof of the correctness of the program. This method is somewhat impractical, at least at this particular moment in the history of computer science.

Jared Smith
  • 19,721
  • 5
  • 45
  • 83