Threads and simultaneous read and write

P

Piotr Jakubowski

If I have some primitive type or a pointer ands in one thread I change
given variable/pointer and in the second one I read the same resource
what would be the result of that situation? Will I randomly get one of
two values (old or new) or would I get something totally screwed up?
 
Ö

Öö Tiib

If I have some primitive type or a pointer ands in one thread I change
given variable/pointer and in the second one I read the same resource
what would be the result of that situation? Will I randomly get one of
two values (old or new) or would I get something totally screwed up?

Depends on platform. If writing or reading that type is not atomic on
that platform then you may get screwed up thing.
 
J

Joshua Maurice

If I have some primitive type or a pointer ands in one thread I change
given variable/pointer and in the second one I read the same resource
what would be the result of that situation? Will I randomly get one of
two values (old or new) or would I get something totally screwed up?

By the formal POSIX, win32, and upcoming C++0x standards, you will
have undefined behavior - that is your program may crash or do
anything else it wants.

In practice, it depends on the type of the pointer and the hardware.
You might get the previous value, the next value, a mix if the
hardware doesn't support atomically writing that size of data, and you
might get some completely bogus value as well - what Java calls "out
of thin air".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top