<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>http://jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>http://jackxiang.com/post//</link>
<title><![CDATA[PHP中的uniqid在高并发下的重复问题]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[Php/Js/Shell/Go]]></category>
<pubDate>Wed, 27 Feb 2013 08:52:28 +0000</pubDate> 
<guid>http://jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;今天群里有人问关于生成唯一id的问题，有人说用uniqid来生成，但也有人说这个生成的id未必是唯一，尤其在高并发时，原文如下：<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;项目是一个高并发的web项目，并且会有后台进程（pcntl并发），两者都会利用uniqid去生成唯一id，今天发现一个bug，在高并发情况下，uniqid可能产生重复输出<br/><br/>以下是测试代码：<br/><br/><textarea name="code" class="php" rows="15" cols="100">&nbsp;&nbsp;
&lt;?php&nbsp;&nbsp;
function new_child($func_name) &#123;&nbsp;&nbsp;
$args = func_get_args();&nbsp;&nbsp;
unset($args[0]);&nbsp;&nbsp;
$pid = pcntl_fork();&nbsp;&nbsp;
if($pid == 0) &#123;&nbsp;&nbsp;
function_exists($func_name) and exit(call_user_func_array($func_name, $args)) or exit(-1);&nbsp;&nbsp;
&#125; else if($pid == -1) &#123;&nbsp;&nbsp;
echo &quot;Couldn’t create child process.&quot;;&nbsp;&nbsp;
&#125; else &#123;&nbsp;&nbsp;
return $pid;&nbsp;&nbsp;
&#125;&nbsp;&nbsp;&nbsp;&nbsp; 
&#125;&nbsp;&nbsp;
function generate() &#123;&nbsp;&nbsp;
$t = array();&nbsp;&nbsp;
while($i ++ &lt; 10) &#123;&nbsp;&nbsp;
$uid = uniqid(true).&quot;/n&quot;;&nbsp;&nbsp;
array_push($t, $uid);&nbsp;&nbsp;
&#125;&nbsp;&nbsp;&nbsp;&nbsp; 
sort($t);&nbsp;&nbsp;
while(-- $i &gt;=0) &#123;&nbsp;&nbsp;
echo array_shift($t);&nbsp;&nbsp;
&#125;&nbsp;&nbsp;&nbsp;&nbsp; 
&#125;&nbsp;&nbsp;
while($i ++ &lt; 1000) &#123;&nbsp;&nbsp;
new_child(generate);&nbsp;&nbsp;
&#125;&nbsp;&nbsp;

?&gt;
</textarea><br/><br/>测试方法： 命令行运行此程序，重定向输出到文件，然后利用下面程序检查重复：<br/><br/><textarea name="code" class="php" rows="15" cols="100">&nbsp;&nbsp;
&lt;?php&nbsp;&nbsp;
$f = file(&quot;tttttt&quot;);&nbsp;&nbsp;
$f = array_count_values($f);&nbsp;&nbsp;
foreach($f as $k =&gt; $c) if($c &gt; 1) echo $c.&#039;_&#039;.$k;&nbsp;&nbsp;
?&gt; 
</textarea><br/><br/>解决方法： 我们现在是在uniqid后又加了rand(1, 10000)，在1000并发，每进程10次uniqid的情况下，再没有产生重复。<br/><br/>来自：http://www.dewen.org/q/1425
]]>
</description>
</item><item>
<link>http://jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] PHP中的uniqid在高并发下的重复问题]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>