临界区 sleep

24.1 Sleep

In some cases race conditions can be repeated when all but one thread are blocked (for example waiting for an SQL lock). Then the remaining thread has plenty of time to go through the critical piece of code.

The problem here is to assure that the blocking threads run until they reach their blocking point before the remaining thread reaches the critical code.

One solution is to use the 'sleep' command of 'mysqltest' in front of the SQL statement that drives the remaining thread into the critical code.

Example:

--connection conn1
   LOCK TABLE t1 WRITE;
       --connection conn2
       # This will block in wait_for_lock().
       send INSERT INTO t1 VALUES (1);
   --connection conn1
   # Sleep until we can be sure that conn2 reached wait_for_lock().
   --sleep 2
   # Run through the critical code.
   FLUSH TABLE t1;

The BIG, BIG problem with 'sleep' is that you need to specify a fixed time. It must be big enough so that the test works as intended even on a very slow machine that is under heavy load. Hence it is much too big for the average machine. A major waste of time.

The bottom line is: AVOID 'SLEEP' WHEREVER POSSIBLE.

MySQL :: MySQL Internals Manual :: 24.1 Sleep https://dev.mysql.com/doc/internals/en/sleep.html

原文地址:https://www.cnblogs.com/rsapaper/p/15475141.html