Home > Back-end >  Consult a multi-threaded locking problems
Consult a multi-threaded locking problems

Time:10-20

Multi-threaded programs, including a 1 thread used for initialization data regularly, and multiple threads access initialization good data, I think at the time of initialization, other threads can not access the program, but the data initialization is completed, other threads can be unlocked when access to the data, because the lock can lead to access between threads mutexes, this how to design? Visit other threads consistent open, cannot be stopped

CodePudding user response:

Initializes the lock, it is no problem,
But multithreaded access cannot lock at the same time? Determine all read-only access? All is read-only access can be unlocked,

CodePudding user response:

reference 1st floor zjq9931 response:
initialize the lock, is no problem,
But multithreaded access cannot lock at the same time? Determine all read-only access? All read-only access can be unlocked,
is read-only access, if not locked, can't distinguish and initialization thread

CodePudding user response:

refer to the second floor Wolf mixed response:
Quote: refer to 1st floor zjq9931 response:
initialize the lock, is no problem,
But multithreaded access cannot lock at the same time? Determine all read-only access? All is read-only access can be unlocked,
is read-only access, if not locked, can't and initialization thread separate


Sequentially, after initialization completes, restart the other read-only thread,

CodePudding user response:

Use AB double buffer strategy,
Initialize A, access B
Initialize B, access to A

CodePudding user response:

best is initialized after restarting threads access

If access to the thread start is not controlled by you, can't do this,

Then consider this:
 
STD: : atomic Ready (false);
//initialize the thread:
//to do a series of initialization...
//...
//...
Ready=true;

 
//access thread:
while (! Ready) {STD: : this_thread: : yield (); }//wait for ready to true.
//after began to visit
//...
  • Related