Skip to content

Page 660: The warning of what to watch out for under the "Parallel Algorithms Are Not Magic" sounds probably wrong/misleading in part. #141

Description

@WraithGlade

From the text:

A red flag is any algorithm that passes a function object to the algorithm. If the function object has mutable state, the executing threads will have shared access and you might have a race condition.

I'm no expert in concurrency or parallelism, but two parts of this section of the text sound very likely to be wrong/misleading.

From what I understand, the existence of mutable state in the function object should only result in the potential for race conditions if that mutable state is shared. In contrast though, internal mutable state that isn't visible outside of the function object seems like it would be fine. The way you've worded it though sounds like no mutable state whatsoever should ever be used inside a function object though, which would be very limiting. It sounds wrong. Your example of a race for std::transform also uses shared mutable state, not just normal internal mutable state, so this lines up with my suspicions here.

On the other hand though, I'm no expert in concurrency and parallelism, so I might be wrong potentially. I don't think so though. The above section of text from the book sounds fishy and doesn't seem to line up with what concurrency/parallelism is supposed to be like, from what I understand of it.

Thus the two problems in the above text:

(1) "any algorithm that passes a function object to the algorithm" is not in fact a red flag, in and of itself, if my understanding is correct.

(2) The other sentence should probably be changed to read as follows (or similar):

If the function object has shared mutable state, the executing threads will have shared access and you might have a race condition.

Correct me if I'm wrong. I'm not 100% sure, but I'm fairly confident.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions