<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Perplexed Labs &#187; mutex</title>
	<atom:link href="http://blog.perplexedlabs.com/tag/mutex/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.perplexedlabs.com</link>
	<description>web development war stories from the frontlines to the backend</description>
	<lastBuildDate>Sat, 24 Jul 2010 16:27:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Mutex with PHP and mySQL</title>
		<link>http://blog.perplexedlabs.com/2008/02/06/mutex-with-php-and-mysql/</link>
		<comments>http://blog.perplexedlabs.com/2008/02/06/mutex-with-php-and-mysql/#comments</comments>
		<pubDate>Wed, 06 Feb 2008 15:34:57 +0000</pubDate>
		<dc:creator>Matt</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[mutex]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.perplexedlabs.com/2008/02/06/mutex-with-php-and-mysql/</guid>
		<description><![CDATA[There are certain situations where you want a periodically executed script to have one and only one instance running at a time to prevent against poisoning of data due to simultaneous execution. There's actually an extremely simple way to accomplish this in a PHP environment by taking advantage of mySQL's built in "lock" functionality. The [...]


Related posts:<ol><li><a href='http://blog.perplexedlabs.com/2009/10/05/php-custom-session-handler/' rel='bookmark' title='Permanent Link: PHP Custom MySQL Session Handler'>PHP Custom MySQL Session Handler</a></li>
<li><a href='http://blog.perplexedlabs.com/2008/12/17/php-parallel-web-scraper/' rel='bookmark' title='Permanent Link: PHP Parallel Web Scraper'>PHP Parallel Web Scraper</a></li>
<li><a href='http://blog.perplexedlabs.com/2008/02/12/php-fast-large-megabyte-data-transfer-between-sessions/' rel='bookmark' title='Permanent Link: PHP fast, large (megabyte), data transfer between sessions'>PHP fast, large (megabyte), data transfer between sessions</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>There are certain situations where you want a periodically executed script to have one and only one instance running at a time to prevent against poisoning of data due to simultaneous execution.</p>
<p>There's actually an extremely simple way to accomplish this in a PHP environment by taking advantage of mySQL's built in "lock" functionality.</p>
<p>The following simple example illustrates the usage of the class that is listed below:</p>
<pre class="brush: php;">
&lt;?php
$lock = new cLock(&quot;updateEarnings&quot;);
if($lock-&gt;isFree()) {
	$lock-&gt;lock();
	// execute
	$lock-&gt;release();
} else {
	trigger_error(&quot;ALREADY LOCKED... ABORTING!&quot;, E_USER_NOTICE);
}
?&gt;
</pre>
<p>Class listing cLock.php: (keep in mind this class uses some helper functions of mine, particularly 'qdb()' and 'result()' which do the obvious)</p>
<pre class="brush: php;">
&lt;?php

class cLock
{
	var $lockname;
	var $timeout;
	var $locked;

	function cLock($name, $timeout = 0)
	{
		$this-&gt;lockname = $name;
		$this-&gt;timeout = $timeout;
		$this-&gt;locked = -1;
	}

	function lock()
	{
		$rs = qdb(&quot;SELECT GET_LOCK('&quot;.$this-&gt;lockname.&quot;', &quot;.$this-&gt;timeout.&quot;)&quot;);
		$this-&gt;locked = result($rs, 0);
		mysqli_free_result($rs);
	}

	function release()
	{
		$rs = qdb(&quot;SELECT RELEASE_LOCK('&quot;.$this-&gt;lockname.&quot;')&quot;);
		$this-&gt;locked = !result($rs, 0);
		mysqli_free_result($rs);

	}

	function isFree()
	{
		$rs = qdb(&quot;SELECT IS_FREE_LOCK('&quot;.$this-&gt;lockname.&quot;')&quot;);
		$lock = (bool)result($rs, 0);
		mysqli_free_result($rs);

		return $lock;
	}
}

?&gt;
</pre>


<p>Related posts:<ol><li><a href='http://blog.perplexedlabs.com/2009/10/05/php-custom-session-handler/' rel='bookmark' title='Permanent Link: PHP Custom MySQL Session Handler'>PHP Custom MySQL Session Handler</a></li>
<li><a href='http://blog.perplexedlabs.com/2008/12/17/php-parallel-web-scraper/' rel='bookmark' title='Permanent Link: PHP Parallel Web Scraper'>PHP Parallel Web Scraper</a></li>
<li><a href='http://blog.perplexedlabs.com/2008/02/12/php-fast-large-megabyte-data-transfer-between-sessions/' rel='bookmark' title='Permanent Link: PHP fast, large (megabyte), data transfer between sessions'>PHP fast, large (megabyte), data transfer between sessions</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.perplexedlabs.com/2008/02/06/mutex-with-php-and-mysql/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
