Home > Back-end >  Multithreaded how an array subscript synchronization
Multithreaded how an array subscript synchronization

Time:10-12

Such as creating a N (such as 10 threads) threads and a size of 5 array,
Scene: A thread to write subscript 0 array item, B and C threads at the same time, write the subscript 1 array item
Problem that how to make A thread to write index 0, the thread does not affect the B and C write subscript 1, but between B and C are mutually exclusive, also said that if you use A lock, how to do only one lock array?

PS: the first available demo plus 200

CodePudding user response:

I feel only A single write [0] there is no competition for the memory, memory

But in most cases there are reading is written, this time will take into account the issue of mutually exclusive,

Int arr [10]
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Thread B:
...
Lock (the second element of the read-write lock)
Write arr [1]
Unlock
.
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Thread C:
.
Lock (the second element of the read-write lock)
Write arr [1]
Unlock
.
---------------------------------------------------

CodePudding user response:

Get an array of size to 5 lock,

CodePudding user response:

reference response: 1/f, qian dream life
I feel only A single write [0] memory does not exist in the memory of competition,

But in most cases there are reading is written, this time will take into account the issue of mutually exclusive,

Int arr [10]
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Thread B:
.
Lock (the second element of the read-write lock)
Write arr [1]
Unlock
.
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Thread C:
.
Lock (the second element of the read-write lock)
Write arr [1]
Unlock
.
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

This will lock the entire arr

CodePudding user response:

Subscript no need to worry about 0 no thread contention
When A thread to write A subscript 0 would not affect writing subscript 1 B and C thread

CodePudding user response:

reference OneOnce reply: 3/f
Quote: reference response: 1/f, qian dream life
I feel only A single write [0] memory does not exist in the memory of competition,

But in most cases there are reading is written, this time will take into account the issue of mutually exclusive,

Int arr [10]
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Thread B:
.
Lock (the second element of the read-write lock)
Write arr [1]
Unlock
.
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Thread C:
.
Lock (the second element of the read-write lock)
Write arr [1]
Unlock
.
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

This will lock the entire arr


The lock is locked in my memory the code? I feel should remember wrong, only to lock the two for arr [1] cannot be parallel of write operation,

CodePudding user response:

The
reference 4 floor lin5161678 reply:
subscript 0 no competition without thread tube
When A thread to write A subscript 0 would not affect thread B and C to write the subscript 1

May also want to visit the subscript X or Y thread 0, if A lock arr [0], this time will affect the other threads access arr other indices

CodePudding user response:

And the subscript as a lock

CodePudding user response:

refer to 7th floor lin5161678 response:
and subscript a lock

But write access can be a method, such as:
Void the write (int index, int value)
{
The lock ();
Arr [index]=value;
Unlock ();
}
This kind of writing for all call the method of thread mutex

CodePudding user response:

refer to the eighth floor OneOnce response:
Quote: refer to 7th floor lin5161678 response:
and subscript a lock

But write access can be a method, such as:
Void the write (int index, int value)
{
The lock ();
Arr [index]=value;
Unlock ();
}
This kind of writing for all thread calls the method mutually exclusive

What I want is to the index level of the lock

CodePudding user response:

references 9 f OneOnce response:
Quote: refer to the eighth floor OneOnce response:
Quote: refer to 7th floor lin5161678 response:
and subscript a lock

But write access can be a method, such as:
Void the write (int index, int value)
{
The lock ();
Arr [index]=value;
Unlock ();
}
This kind of writing for all thread calls the method mutually exclusive

What I want is to the index level of lock
no index level of said
Don't be silly
No lock array level

CodePudding user response:

 lock (); 
B=index++;
Arr [b]=value;
Unlock ();

CodePudding user response:

Lock is what's inside and lock contention didn't lock data
Can understand this
The so-called lock data is a logical entity rather than a code

Reference
may also want to visit the subscript X or Y thread 0, if A lock arr [0], this time will affect the other threads access arr other subscript

A lock
A read/write 0 subscript
Unlock A

X y threads need to manipulate the subscript 0
Then x y and A competition to A thread lock

Other threads not A lock operation will not be affected by any

CodePudding user response:

The index level of lock, such as 2 l, get a one-to-one lock, 10 elements 10 lock this cough up ~

This need some strange

CodePudding user response:

refer to the 13th floor response: thousand dreams life
the index level of lock, it is like 2 l, get a one-to-one lock, 10 elements 10 lock this cough up ~

This need some strange

Advice look at specific problems, and then the measure of whether really need to do this, lock, after all, some more, there should be a better way

CodePudding user response:

thousand dream life reference 14 floor response:
Quote: refer to the 13th floor response: thousand dreams life
the index level of lock, it is like 2 l, get a one-to-one lock, 10 elements 10 lock this cough up ~

This need some strange

Suggest look at specific problems, and then the measure of whether really need to do this, after all the lock some more, there should be a better way

General use are directly lock the entire array, and then finished operations to unlock,

The restroom so many pit a this actual problem is also a door lock,
Is, of course, optimize the way some numeral toilet, through signal processing, the premise is that all people don't care about what is the location of the toilet,

nullnullnull
  • Related