{"id":213,"title":"\u9ad8\u5e76\u53d1\u65f6\u8868\u9501\u4e0e\u6587\u4ef6\u9501","good":0,"bad":0,"hit":2243,"created_at":"2016-03-26 19:31:15","content":"

\u5e94\u7528\u573a\u666f\uff1a\u9ad8\u5e76\u53d1\u65f6\u4e0b\u5355\uff0c\u6216\u8005\u5bf9\u67d0\u4e00\u8868\u8fdb\u884c\u64cd\u4f5c<\/p>

<\/p>

\u5982\uff1a\u67d0\u8868\u4e2d\u6709\u4e00\u4e2a\u6570\u5b57\u4e3a100\uff0c\u73b0\u5728\u591a\u4eba\u540c\u65f6\u8fdb\u884c\u53d6\u51fa\uff0c-1\uff0c\u5b58\u56de\uff0c\u5047\u8bbe\u6709\u4e00\u767e\u4e2a\u4eba\u540c\u65f6\u6267\u884c\u4e00\u6b21\uff0c\u6211\u4eec\u9884\u671f\u7684\u7ed3\u679c\u662f0(\u4f4e\u5e76\u53d1\u4e0b)\uff0c\u4f46\u4e8b\u5b9e\u4e0a\u8fd0\u884c\u7684\u7ed3\u679c\u5374\u662f99<\/p>

<\/p>

MYSQL\u4e2d\u7684\u9501<\/p>

         \u540c\u4e00\u65f6\u95f4\u53ea\u6709\u4e00\u4e2a\u4eba\u53ef\u4ee5\u83b7\u53d6\u9501\uff0c\u5176\u4ed6\u4eba\u53ea\u80fd\u963b\u585e\u7b49\u5f85\u7b2c\u4e00\u4e2a\u4eba\u91ca\u653e\u9501\u3002<\/p>

         LOCKTABLE \u8868\u540d read|write;<\/p>

         UNLOCKTABLES;<\/p>

Read:\u6240\u6709\u4eba\u90fd\u53ea\u53ef\u4ee5\u8bfb\uff0c\u53ea\u6709\u91ca\u653e\u9501\u4e4b\u540e\u624d\u53ef\u4ee5\u5199<\/p>

Write:\u53ea\u6709\u9501\u8868\u7684\u5ba2\u6237\u53ef\u4ee5\u64cd\u4f5c\u8fd9\u4e2a\u8868\uff0c\u5176\u4ed6\u5ba2\u6237\u8bfb\u90fd\u4e0d\u80fd\u8bfb\u3002<\/p>

\u6ce8\u610f\uff1a\u5728\u9501\u8868\u8fc7\u7a0b\u4e2d\u53ea\u80fd\u64cd\u4f5c\u9501\u5b9a\u7684\u8868\uff0c\u5982\u679c\u8981\u64cd\u4f5c\u5176\u4ed6\u8868\uff0c\u9700\u8981\u90fd\u9501\u8d77\u6765\uff01\u3002<\/p>

\u540c\u65f6\u9501\u5b9a\u591a\u5f20\u8868\uff1aLOCK TABLE \u8868\u540d1 read|write, \u88682 read|write, \u88683 read|write\u2026..<\/p>

 <\/p>

\u7f3a\u70b9\uff1a\u963b\u585e\u3002\u6709\u4e9b\u529f\u80fd\u9700\u8981\u9501\u591a\u5f20\u8868\uff0c\u800c\u6709\u4e9b\u8868\u6574\u4e2a\u7f51\u7ad9\u90fd\u8981\u7528\uff0c\u4e00\u65e6\u9501\u5b9a\uff0c\u4f1a\u8ba9\u6574\u4e2a\u7f51\u7ad9\u5904\u5728\u963b\u585e\u72b6\u6001\u3002<\/p>

 <\/p>

 <\/p>

PHP\u4e2d\u7684\u9501<\/p>

         \u7279\u70b9\uff1a\u975e\u963b\u585e\u3002\u5bf9\u8868\u6ca1\u6709\u4efb\u4f55\u538b\u529b\uff0c\u6240\u4ee5\u5b9e\u9645\u5e94\u7528\u4f7f\u7528\u8fd9\u79cd\u6bd4\u8f83\u597d<\/p>

<\/p>

<?phpnamespace classes;class Helper{    private static $_lockFp = array();        public static function startLock($lockFileName)    {        self::$_lockFp[$lockFileName] = $fp = fopen(ROOT . '\/data\/lock\/'.$lockFileName, 'r');\/\/\u6587\u4ef6\u9501        if(!$fp)            return FALSE;        $try = 10;\/\/\u8bd5\u5341\u6b21\uff0c\u907f\u514d\u4e00\u76f4\u6b7b\u5faa\u73af        $lock = false;        do         {            $lock = flock($fp, LOCK_EX);            if(!$lock)                usleep(50000);  \/\/ \u4f11\u606f0.05\u79d2,\u51cf\u8f7bcpu\u538b\u529b,usleep\u5355\u4f4d\u662f\u5fae\u79d2        }while(!$lock && --$try > 0);        return $lock;    }    public static function endLock($lockFileName)    {        if(isset(self::$_lockFp[$lockFileName]))        {            @flock(self::$_lockFp[$lockFileName], LOCK_UN);            @fclose(self::$_lockFp[$lockFileName]);        }    }}<\/p>"}