<?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"
	>

<channel>
	<title>Alexander Sandler on the Net</title>
	<atom:link href="http://www.alexandersandler.net/feed" rel="self" type="application/rss+xml" />
	<link>http://www.alexandersandler.net</link>
	<description></description>
	<pubDate>Sun, 09 Nov 2008 07:25:37 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.5</generator>
	<language>en</language>
			<item>
		<title>pthread spinlocks</title>
		<link>http://www.alexandersandler.net/pthread-spinlocks</link>
		<comments>http://www.alexandersandler.net/pthread-spinlocks#comments</comments>
		<pubDate>Sun, 09 Nov 2008 07:23:39 +0000</pubDate>
		<dc:creator>Alexander Sandler</dc:creator>
		
		<category><![CDATA[Short articles]]></category>

		<category><![CDATA[mutex]]></category>

		<category><![CDATA[posix]]></category>

		<category><![CDATA[pthread]]></category>

		<category><![CDATA[semaphore]]></category>

		<category><![CDATA[spinlock]]></category>

		<category><![CDATA[synchronization]]></category>

		<guid isPermaLink="false">http://www.alexandersandler.net/?p=674</guid>
		<description><![CDATA[Continuing my previous post, I would like to talk about relatively new feature in glibc and pthreads in particular. I am talking about spinlocks.

Quiet often you want to protect some simple data structures from simultaneous access by two or more threads. As in my previous post, this can even be a simple integer. Using mutexes [...]]]></description>
			<content:encoded><![CDATA[<p>Continuing my <a href="http://www.alexandersandler.net/do-you-need-mutex-to-protect-int">previous post</a>, I would like to talk about relatively new feature in glibc and pthreads in particular. I am talking about spinlocks.<br />
<span id="more-674"></span><br />
Quiet often you want to protect some simple data structures from simultaneous access by two or more threads. As in my previous post, this can even be a simple integer. Using mutexes and semaphores (critical sections if you wish) to protect simple integer is an overkill and here&#8217;s why.</p>
<p>Modifying or reading value of a single integer requires quiet often as few as two or three CPU instructions. On the other hand, acquiring semaphore is a huge operation. It involves at least one system call translated into thousands of CPU instructions. Same when releasing the semaphore.</p>
<p>Things are a little better with mutexes, but still far from being perfect. Posix mutexes in Linux implemented using futexes. Futex stands for Fast-Usermode-muTEX. The idea behind futex is not to do system call when locking <strong>unlocked</strong> futex. Waiting for locked futex would still require system call because this is how processes wait for something in Linux. Yet locking unlocked futex can be done without asking kernel to do things for you. Therefore, locking futex is, at least in some cases, is very fast.</p>
<p>The problem is that in rest of the cases mutex in Linux is slow as semaphore. And as with semaphores, spending thousands of CPU cycles just to protect a single integer is definitely an overkill.</p>
<p>This is exactly the problem spinlocks solve. Spinlock is another synchronization mechanism. It works in the same manner as mutexes. I.e. only one thread can have it locked at the same time. However there&#8217;s a difference.</p>
<p>When thread tries to lock locked spinlock, it won&#8217;t sleep waiting for the spinlock to get unlocked. It will do busy wait, i.e. spinning in a <em>while</em> loop. This is why its called <strong>spin</strong>lock.</p>
<p>Locking spinlock takes only tens of CPU cycles. One important thing with spinlocks is to hold it for short period of time. Don&#8217;t be surprised when your program will begin consuming too much CPU, all because one of your threads holds some spinlock for too long. To avoid this, try to avoid executing big chunks of code while holding a spinlock. And by all means avoid doing I/O while holding spinlock.</p>
<p>Now for the easy part.</p>
<p>First, to make things work, you have to include pthread.h. <em>pthread_spin_init() </em>initializes the spinlock. <em>pthread_spin_lock()</em> locks one and <em>pthread_spin_unlock()</em> unlocks it. All as with pthread mutexes. And there are manual pages for each and every one of them of course.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.alexandersandler.net/pthread-spinlocks/feed</wfw:commentRss>
		</item>
		<item>
		<title>Do you need a mutex to protect an int?</title>
		<link>http://www.alexandersandler.net/do-you-need-mutex-to-protect-int</link>
		<comments>http://www.alexandersandler.net/do-you-need-mutex-to-protect-int#comments</comments>
		<pubDate>Thu, 23 Oct 2008 15:16:23 +0000</pubDate>
		<dc:creator>Alexander Sandler</dc:creator>
		
		<category><![CDATA[Short articles]]></category>

		<category><![CDATA[integer]]></category>

		<category><![CDATA[multi-threaded]]></category>

		<category><![CDATA[mutex]]></category>

		<category><![CDATA[semaphore]]></category>

		<category><![CDATA[spinlock]]></category>

		<category><![CDATA[synchronization]]></category>

		<category><![CDATA[threads]]></category>

		<guid isPermaLink="false">http://www.alexandersandler.net/?p=656</guid>
		<description><![CDATA[Recently I ran into few pieces of code here and there that assumed that int is an atomic type. I.e. when you modify value of the variable from two or more different threads at the same time, all of the changes you&#8217;ve made to the value will remain intact.
But really, can you modify variables of [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I ran into few pieces of code here and there that assumed that <em>int</em> is an atomic type. I.e. when you modify value of the variable from two or more different threads at the same time, all of the changes you&#8217;ve made to the value will remain intact.</p>
<p>But really, can you modify variables of basic types (integers, floats, etc), from two or more threads, at the same time, without screwing their value?<br />
<span id="more-656"></span></p>
<p>Until now I&#8217;ve been very precautious with questions of this kind. I&#8217;ve spend enormous amount of time solving synchronization problems. So when I have a variable that being modified or accessed from two or more threads I always put some mutex or semaphore (spinlock in kernel) around it - no questions asked.</p>
<p>Still, I decided to check things out, perhaps one more time. I wanted to see what happens with a variable if two or more threads modifying it. So, I&#8217;ve written a short program that demonstrates what happens when you do something like this.</p>
<p>Here&#8217;s the code. See my comments below to understand what it does and how it does it.</p>
<pre>#include &lt;stdio.h&gt;
#include &lt;pthread.h&gt;
#include &lt;unistd.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;sched.h&gt;
#include &lt;linux/unistd.h&gt;
#include &lt;sys/syscall.h&gt;
#include &lt;errno.h&gt;

#define INC_TO 1000000 // one million...

int global_int = 0;

pid_t gettid( void )
{
	return syscall( __NR_gettid );
}

void *thread_routine( void *arg )
{
	int i;
	int proc_num = (int)(long)arg;
	cpu_set_t set;

	CPU_ZERO( &amp;set );
	CPU_SET( proc_num, &amp;set );

	if (sched_setaffinity( gettid(), sizeof( cpu_set_t ), &amp;set ))
	{
		perror( "sched_setaffinity" );
		return NULL;
	}

	for (i = 0; i &lt; INC_TO; i++)
	{
		global_int++;
	}

	return NULL;
}

int main()
{
	int procs = 0;
	int i;
	pthread_t *thrs;

	// Getting number of CPUs
	procs = (int)sysconf( _SC_NPROCESSORS_ONLN );
	if (procs &lt; 0)
	{
		perror( "sysconf" );
		return -1;
	}

	thrs = malloc( sizeof( pthread_t ) * procs );
	if (thrs == NULL)
	{
		perror( "malloc" );
		return -1;
	}

	printf( "Starting %d threads...\n", procs );

	for (i = 0; i &lt; procs; i++)
	{
		if (pthread_create( &amp;thrs[i], NULL, thread_routine, (void *)(long)i ))
		{
			perror( "pthread_create" );
			procs = i;
			break;
		}
	}

	for (i = 0; i &lt; procs; i++)
		pthread_join( thrs[i], NULL );

	free( thrs );

	printf( "After doing all the math, global_int value is: %d\n", global_int );
	printf( "Expected value is: %d\n", INC_TO * procs );

	return 0;
}</pre>
<p>You can download the code and a Makefile <a href="http://www.alexandersandler.net/wp-content/uploads/2008/10/atomics.tgz">here</a>.</p>
<p>The setup is simple. Number of threads is as number of CPUs in your machine (this includes cores and even hyperthreaded semi-cores). Each thread affined to certain core with <em>sched_setaffinity()</em>. I.e. scheduler configured to run certain thread on certain core, to make sure that all cores access that variable. Each thread increases a global integer named <em>global_int</em> one million times.</p>
<p>In the meantime, main thread waits for the worker threads to do their job. Once they&#8217;re done, it prints the value of <em>global_int</em>.</p>
<p>And the result is:</p>
<pre>Starting 4 threads...
After doing all the math, global_int value is: 1908090
Expected value is: 4000000</pre>
<p>I guess numbers speak for themselves.</p>
<p>In case you still have some doubts, consider this. When I compile the code with -O2 compiler option (enabling optimization), the value of the <em>global_int</em> is 4000000, as you may have expected. So, it is not really black and white. Also, things may be different on different computers.</p>
<p>However, the bottom line is that you want to be on the safe side. This code is a little simple proof that in some cases, simultaneous access to certain basic variable can cause its value to become ambiguous. So, to be on the safe side, protect your shared variables, no matter if it is a complex data structure or simple integers.</p>
<p>If you have further questions, comment here or send me an email to <a href="mailto:alexander.sandler@gmail.com">alexander.sandler@gmail.com</a>.</p>
<p>PS: I think this issue deserves full scale article. What do you think?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.alexandersandler.net/do-you-need-mutex-to-protect-int/feed</wfw:commentRss>
		</item>
		<item>
		<title>Back</title>
		<link>http://www.alexandersandler.net/back</link>
		<comments>http://www.alexandersandler.net/back#comments</comments>
		<pubDate>Thu, 23 Oct 2008 13:06:38 +0000</pubDate>
		<dc:creator>Alexander Sandler</dc:creator>
		
		<category><![CDATA[Blog]]></category>

		<guid isPermaLink="false">http://www.alexandersandler.net/?p=654</guid>
		<description><![CDATA[Vacation is over and its time to get busy again  
]]></description>
			<content:encoded><![CDATA[<p>Vacation is over and its time to get busy again <img src='http://www.alexandersandler.net/wp-content/plugins/smilies-themer/modern/smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.alexandersandler.net/back/feed</wfw:commentRss>
		</item>
		<item>
		<title>Where the hell am I?</title>
		<link>http://www.alexandersandler.net/where-the-hell-am-i</link>
		<comments>http://www.alexandersandler.net/where-the-hell-am-i#comments</comments>
		<pubDate>Tue, 14 Oct 2008 11:15:15 +0000</pubDate>
		<dc:creator>Alexander Sandler</dc:creator>
		
		<category><![CDATA[Blog]]></category>

		<guid isPermaLink="false">http://www.alexandersandler.net/?p=651</guid>
		<description><![CDATA[Just in case you&#8217;re wondering where the hell am I and why I am not writing new stuff, I am on a short vacation. It&#8217;s holidays time here in Israel, so I took the liberty to split for a couple of weeks. I am actively sweeming in different sees, traveling around the country and sitting [...]]]></description>
			<content:encoded><![CDATA[<p>Just in case you&#8217;re wondering where the hell am I and why I am not writing new stuff, I am on a short vacation. It&#8217;s holidays time here in Israel, so I took the liberty to split for a couple of weeks. I am actively sweeming in different sees, traveling around the country and sitting home doing nothing. Fiesta will be over on Oct. 22. Until then, have fun! <img src='http://www.alexandersandler.net/wp-content/plugins/smilies-themer/modern/smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.alexandersandler.net/where-the-hell-am-i/feed</wfw:commentRss>
		</item>
		<item>
		<title>New article - How inheritance, encapsulation and polymorphism work in C++</title>
		<link>http://www.alexandersandler.net/new-article-how-inheritance-encapsulation-and-polymorphism-work-in-c</link>
		<comments>http://www.alexandersandler.net/new-article-how-inheritance-encapsulation-and-polymorphism-work-in-c#comments</comments>
		<pubDate>Tue, 23 Sep 2008 09:03:24 +0000</pubDate>
		<dc:creator>Alexander Sandler</dc:creator>
		
		<category><![CDATA[News]]></category>

		<guid isPermaLink="false">http://www.alexandersandler.net/?p=637</guid>
		<description><![CDATA[This concludes a month of work. Somehow I, again, ended up with huge article and explains lots of different things.
In this article, I am explaining how the three whales of OOP implemented in C++. How encapsulation works. How overloading of methods works. What is the structure of C++ object compared to C structure. How polymorphism [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft size-medium wp-image-638" title="Inheritance icon" src="http://www.alexandersandler.net/wp-content/uploads/2008/09/inheritance_icon.png" alt="" width="100" height="100" />This concludes a month of work. Somehow I, again, ended up with huge article and explains lots of different things.</p>
<p>In this article, I am explaining how the three whales of OOP implemented in C++. How encapsulation works. How overloading of methods works. What is the structure of C++ object compared to C structure. How polymorphism works. What&#8217;s the structure of virtual methods table and how inheritance affects it. And many other things. Hope you will find it interesting. <a href="http://www.alexandersandler.net/how-inheritance-encapsulation-and-polymorphism-work-in-cpp">Read it here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.alexandersandler.net/new-article-how-inheritance-encapsulation-and-polymorphism-work-in-c/feed</wfw:commentRss>
		</item>
		<item>
		<title>Picotux - the smallest Linux Computer in the World</title>
		<link>http://www.alexandersandler.net/picotux-the-smallest-linux-computer-in-the-world</link>
		<comments>http://www.alexandersandler.net/picotux-the-smallest-linux-computer-in-the-world#comments</comments>
		<pubDate>Sun, 14 Sep 2008 07:49:12 +0000</pubDate>
		<dc:creator>Alexander Sandler</dc:creator>
		
		<category><![CDATA[Blog]]></category>

		<category><![CDATA[Reviews]]></category>

		<category><![CDATA[embedded]]></category>

		<category><![CDATA[linux]]></category>

		<guid isPermaLink="false">http://www.alexandersandler.net/?p=548</guid>
		<description><![CDATA[I ran into this beauty few days ago.

It is 2&#215;2x4cm. It is a 55MHz ARM7, with 8MB of RAM and 2MB of flash memory. It has serial and 100/10 Ethernet port. It runs ucLinux.
The price tag is biting a bit. It&#8217;s 100€, plus delivery (which can be pricy too). Yet for device so unique it [...]]]></description>
			<content:encoded><![CDATA[<p>I ran into this beauty few days ago.</p>
<p><img class="aligncenter" title="Picotux" src="http://www.picotux.com/pt112x.jpg" alt="" width="356" height="268" /></p>
<p>It is 2&#215;2x4cm. It is a 55MHz ARM7, with 8MB of RAM and 2MB of flash memory. It has serial and 100/10 Ethernet port. It runs <a title="ucLinux" href="http://www.uclinux.org/">ucLinux</a>.</p>
<p>The price tag is biting a bit. It&#8217;s 100€, plus delivery (which can be pricy too). Yet for device so unique it seems to be a fair deal <img src='http://www.alexandersandler.net/wp-content/plugins/smilies-themer/modern/smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p><a title="Link to picotux's web-site." href="http://www.picotux.com/indexe.html">Grab one here!</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.alexandersandler.net/picotux-the-smallest-linux-computer-in-the-world/feed</wfw:commentRss>
		</item>
		<item>
		<title>Todo list</title>
		<link>http://www.alexandersandler.net/todo-list</link>
		<comments>http://www.alexandersandler.net/todo-list#comments</comments>
		<pubDate>Sat, 13 Sep 2008 19:21:02 +0000</pubDate>
		<dc:creator>Alexander Sandler</dc:creator>
		
		<category><![CDATA[Blog]]></category>

		<guid isPermaLink="false">http://www.alexandersandler.net/?p=540</guid>
		<description><![CDATA[I usually have something like 10 different articles, on different subjects in stock. Every day I pick myself an article or a couple and add few paragraphs.
Because this is how I work, I often don&#8217;t release new stuff for months and then release several huge articles in just one week.
This way of working may make [...]]]></description>
			<content:encoded><![CDATA[<p>I usually have something like 10 different articles, on different subjects in stock. Every day I pick myself an article or a couple and add few paragraphs.</p>
<p>Because this is how I work, I often don&#8217;t release new stuff for months and then release several huge articles in just one week.</p>
<p>This way of working may make you think that I am not writing new stuff. To devaporate any misunderstandings on the subject, I decided to add a todo list. You can find it on the rightmost pane just below quick links.</p>
<p>In the list I have names of the articles I am working on. Some articles may be missing from there. It is either because I want to make them a surprise or because I am not sure if I will ever release them. But otherwise it&#8217;s all there.</p>
<p>I also think to add some sort of counter that would tell how many words I&#8217;ve written today and perhaps in a week period. I&#8217;ll need a plugin for that.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.alexandersandler.net/todo-list/feed</wfw:commentRss>
		</item>
		<item>
		<title>Marketing experiment</title>
		<link>http://www.alexandersandler.net/marketing-experiment</link>
		<comments>http://www.alexandersandler.net/marketing-experiment#comments</comments>
		<pubDate>Wed, 10 Sep 2008 12:57:36 +0000</pubDate>
		<dc:creator>Alexander Sandler</dc:creator>
		
		<category><![CDATA[Blog]]></category>

		<category><![CDATA[marketing]]></category>

		<guid isPermaLink="false">http://www.alexandersandler.net/?p=504</guid>
		<description><![CDATA[Marketing is the driving force behind many things. This includes web-sites of course.
Few weeks ago I decided that creating good content for the web-site is not enough. It is important to promote it as well. So I thought if I put so much time into writing stuff, why not spend couple of minutes making sure [...]]]></description>
			<content:encoded><![CDATA[<p>Marketing is the driving force behind many things. This includes web-sites of course.</p>
<p>Few weeks ago I decided that creating good content for the web-site is not enough. It is important to promote it as well. So I thought if I put so much time into writing stuff, why not spend couple of minutes making sure people actually read the stuff I write.</p>
<p>I found a <a href="http://www.guitarflame.com/">guy writing about guitars</a>. He opened a new blog and claimed that he will try to reach an average of 5000 visitors a day by the end of six month period. Unfortunately it seems that he has failed. Yet I think he will be pleased to know that he has inspired me to try something similar as well.</p>
<p>No, I will not reach 5000 visitors a day. But I will at least try to bring visitors not only by writing the content, but by promoting it as well.</p>
<p>After reading his blog couple of weeks ago I went down and did something I never did before. I posted <a href="http://www.alexandersandler.net/tcpdump-for-dummies">my most popular article</a> on the most popular social bookmarking services. Guess what. Nearly 1500 visitors has visited within 24 hours after that.</p>
<p>Today, few weeks after, I have something like 40 daily  visitors more than I had before bookmarking my article. All because I spent 5 minutes registering at digg.com and delicious.com. So I think the experiment was a success <img src='http://www.alexandersandler.net/wp-content/plugins/smilies-themer/modern/biggrin.gif' alt=':D' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.alexandersandler.net/marketing-experiment/feed</wfw:commentRss>
		</item>
		<item>
		<title>Wow! My tcpdump for Dummies is on the front page of delicious.com</title>
		<link>http://www.alexandersandler.net/wow-my-tcpdump-for-dummies-is-on-the-front-page-of-deliciouscom</link>
		<comments>http://www.alexandersandler.net/wow-my-tcpdump-for-dummies-is-on-the-front-page-of-deliciouscom#comments</comments>
		<pubDate>Thu, 28 Aug 2008 10:19:04 +0000</pubDate>
		<dc:creator>Alexander Sandler</dc:creator>
		
		<category><![CDATA[Blog]]></category>

		<category><![CDATA[delicious]]></category>

		<category><![CDATA[success]]></category>

		<category><![CDATA[tcpdump]]></category>

		<guid isPermaLink="false">http://www.alexandersandler.net/?p=442</guid>
		<description><![CDATA[

]]></description>
			<content:encoded><![CDATA[<p><a class="thickbox" href="http://www.alexandersandler.net/wp-content/gallery/tcpdump-for-dummies-on-delicious/delicious.jpg"><a href="http://www.alexandersandler.net/wp-content/gallery/tcpdump-for-dummies-on-delicious/delicious.jpg" title="" class="thickbox" rel="singlepic756" ><img class="ngg-singlepic ngg-center" src="http://www.alexandersandler.net/wp-content/plugins/nextgen-gallery/nggshow.php?pid=756&amp;width=512&amp;height=384&amp;mode=" alt="delicious.jpg" title="delicious.jpg" /></a><br />
</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.alexandersandler.net/wow-my-tcpdump-for-dummies-is-on-the-front-page-of-deliciouscom/feed</wfw:commentRss>
		</item>
		<item>
		<title>Copyright infringement tale continued</title>
		<link>http://www.alexandersandler.net/copyright-infringement-tale-continued</link>
		<comments>http://www.alexandersandler.net/copyright-infringement-tale-continued#comments</comments>
		<pubDate>Wed, 27 Aug 2008 12:23:27 +0000</pubDate>
		<dc:creator>Alexander Sandler</dc:creator>
		
		<category><![CDATA[Blog]]></category>

		<category><![CDATA[copyright]]></category>

		<category><![CDATA[dmca]]></category>

		<guid isPermaLink="false">http://www.alexandersandler.net/?p=431</guid>
		<description><![CDATA[This post concludes this story - some gentleman decided that he can make money by publishing my content.
Yesterday, something like a couple of weeks since I filed DMCA complaint against this guy, I received an email from blogspot&#8217;s tech support stating that all posts infringing my copyrights are gone.
I want to thank blogspot for taking [...]]]></description>
			<content:encoded><![CDATA[<p>This post concludes <a href="http://www.alexandersandler.net/copyright-infringement">this story</a> - some gentleman decided that he can make money by publishing my content.</p>
<p>Yesterday, something like a couple of weeks since I filed DMCA complaint against this guy, I received an email from blogspot&#8217;s tech support stating that all posts infringing my copyrights are gone.</p>
<p>I want to thank blogspot for taking care about this.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.alexandersandler.net/copyright-infringement-tale-continued/feed</wfw:commentRss>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.568 seconds -->
<!-- Cached page served by WP-Super-Cache -->
