Home > Back-end >  Excuse me, multithreading, read operation need to be locked?
Excuse me, multithreading, read operation need to be locked?

Time:03-19

If two threads, thread A write lock, thread B read need lock?
Please advice!

CodePudding user response:

If there are only two threads, a thread only, another thread read-only, the two threads are not necessary to lock,

CodePudding user response:

Multithreading, as long as at least one thread in writing, all want to lock in principle,
What circumstances do not need to lock? Basic data types, speaking, reading and writing, and don't need to think about data consistency,

CodePudding user response:

Without A lock, B, A lock is useless, multithreading, speaking, reading and writing A non atomic variables, can be read at the same time, can't write at the same time, can't read and write at the same time,

CodePudding user response:

Need, or read data is not accurate,
A structure, for example, write only modify an element (members), read by this time, the whole structure,
Data is chaotic,

CodePudding user response:

Of course want to lock, multi-threaded file to read and write without lock could be chaos

CodePudding user response:

reference 3 floor wings again hard reply:
A lock, B no, that you will never lock, A multiple threads to read and write A non atomic variables that can be read at the same time, can't write at the same time, can't read and write at the same time,

Great god said is very right, a lock unlocked, the lock is no use
If is the basic data types that do not need to lock in principle, but to be on the safe side still add, unlocked programming very eat capability, but also has a lot to do with the specific system