C Do Locks Make Variables Read Only

Are read only variables thread safe?

How-do-you-do,
im new to threading and a have a quick question:

Is information technology thread prophylactic to have multiple threads reading from the aforementioned variable without protecting information technology with a mutex lock?

and if and then, is it then safe to accept just one thread write to it (without locking it) every bit long every bit every other thread only reads it?

or exercise i have to lock a mutex every time, no matter what?

Last edited on

depends on what you are doing. how did that value get assigned? If the value can potentially exist out of date for the current thread, its not condom. It volition run and execute, simply it volition use the wrong value. So you don't need a mutex but you demand to be certain that either an out of appointment value is OK OR that yous have the latest copy (see, volatile keyword, but that is inefficient so utilise only when necessary). Mutex is needed if y'all write to the same thing at the same time, or if changing something in i thread could pause another (like reading from same file variable in two threads). A mutex tin solve the sync problem as well, and it is besides a operation hit, just yous are using it for something slightly off its intended purpose (not too far off, only a little).

how almost a practical case:
example one, you want to print your vehicle'south current gps location on the screen.
if your thread that prints is off from the thread that reads the hardware, its OK: the user won't know the difference between the last read and the non yet seen current value.

case 2: you lot use the gps value to steer the vehicle. Now the upwardly to date value is critical, and the concluding value just won't do.

Last edited on

Adding to Jonnin'south wise words:

and if so, is it and so safe to have just one thread write to it (without locking it) equally long as every other thread merely reads information technology?

No. Y'all accept no guarantee that halfway through writing, some other thread won't come up in and read information technology while it's in some weird half-fashion state. Some types will endure this more than others.

However, if it's an atomic variable, that's not a trouble.

If you desire to learn more about multithreading yous can look at this, it is a pretty in-depth and curt caption of all the things you should be aware of.
https://randu.org/tutorials/threads/

And also the book "Concurrency in Action" is as well very expert book that shows how to use C++'southward thread library, and shows a lot of structures and use cases.

And note that multithreading isn't a skilful idea if you haven't gotten a good agreement of the fundamentals of C++, and how large projects in C++ look like, since beginners usually start off programming without knowing what they are doing, and write lawmaking without doing any research on how to do it in a more sane fashion, and multithreading is not something that works well with code pulled out of your ass. Many structures in multithreaded code are very consistent (structurally) between many codebases, similar to the Gang of Four structures and other idioms since misusing multithreading tin easily make your code very coupled and very ugly, if used naively.

It's obviously a good tool for certain scenarios, like having a loading screen for a graphical project while not having a "non responding" message if y'all naively brand a single threaded project, only you lot will observe that in many cases it isn't worth it because you are breaking the Osculation rule, which fifty-fifty large scale projects try to follow. Simply multithreading should non be used for functioning, for a inexperienced programmer considering in that location is all the same much to larn nigh optimized single threaded lawmaking.

Notwithstanding, if it's an atomic variable, that'southward not a problem.

^^^^
That is too very important. Atomic means 'will fit in a unmarried cpu annals' for a elementary definition. Imagine you were dealing with a string. Imagine it was 1/ii of the way through its update when you lot read information technology. If it was hello globe and yous inverse it to cheerio brutal world, you might go goodo world. I didnt remember of it every bit I had integers on my mind.

Concluding edited on

stav wrote:
Is information technology thread safe to have multiple threads reading from the same variable without protecting it with a mutex lock?

Only if the initialization/last write to that variable was synchronized with all these reads (or the variable is atomic)

stav wrote:
is information technology then prophylactic to have only 1 thread write to it (without locking it) equally long as every other thread only reads information technology?

this is never prophylactic (formally, undefined behavior due to a data race) except if the variable is atomic

jonnin wrote:
Atomic ways 'will fit in a unmarried cpu register' for a uncomplicated definition

in C++, "atomic" means of type std::diminutive<T> , size doesn't affair (T can be char or it can be std::array<char, 100>)

jonnin wrote:
see, volatile keyword, simply that is inefficient then utilise only when necessary).

No, don't see volatile keyword. It is ever an mistake to use volatile anything in relation to multithreading.
Encounter http://isvolatileusefulwiththreads.com/c++/

Final edited on

Topic archived. No new replies allowed.

rodriguezlietund.blogspot.com

Source: http://www.cplusplus.com/forum/general/246971/

0 Response to "C Do Locks Make Variables Read Only"

إرسال تعليق

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel