<?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>Visual C++ Tips &#187; interlocked singly linked list</title>
	<atom:link href="http://weseetips.com/tag/interlocked-singly-linked-list/feed/" rel="self" type="application/rss+xml" />
	<link>http://weseetips.com</link>
	<description>Gold mine of Visual C++ tricks!</description>
	<lastBuildDate>Thu, 11 Mar 2010 09:09:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>How to use Interlocked Singly Linked Lists?</title>
		<link>http://weseetips.com/2008/12/21/how-to-use-interlocked-singly-linked-lists/</link>
		<comments>http://weseetips.com/2008/12/21/how-to-use-interlocked-singly-linked-lists/#comments</comments>
		<pubDate>Sun, 21 Dec 2008 21:26:15 +0000</pubDate>
		<dc:creator>Jijo Raj</dc:creator>
				<category><![CDATA[Windows APIs]]></category>
		<category><![CDATA[InitializeSListHead()]]></category>
		<category><![CDATA[interlocked list]]></category>
		<category><![CDATA[interlocked singly linked list]]></category>
		<category><![CDATA[InterlockedFlushSList()]]></category>
		<category><![CDATA[InterlockedPopEntrySList()]]></category>
		<category><![CDATA[InterlockedPushEntrySList()]]></category>
		<category><![CDATA[linked list]]></category>
		<category><![CDATA[SLIST_ENTRY]]></category>
		<category><![CDATA[synchornized list]]></category>

		<guid isPermaLink="false">http://weseetips.com/?p=744</guid>
		<description><![CDATA[Well, STL can provide you almost all kind of containers you want. But only one problem exists &#8211; they are not thread safe. If multiple threads are accessing the container at once, you have to add synchronization to them, by using mutex or similar ones. Well, the good news is if you need a singly [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignnone size-medium wp-image-11" title="Icon Description" src="http://weseetips.wordpress.com/files/2008/03/icon_description.jpg?w=166" alt="" width="166" height="32" /><br />
Well, <span style="color:#0000ff;">STL </span>can provide you almost all kind of <span style="color:#0000ff;">containers </span>you want. But only one problem exists &#8211; they <span style="color:#0000ff;">are not thread safe.</span> If multiple threads are accessing the container at once, you have to add synchronization to them, by using mutex or similar ones. Well, the good news is <span style="color:#0000ff;">if you need a singly linked list &#8211; windows have a built in one which is already synchonized</span>.</p>
<p><img class="alignnone size-full wp-image-749" title="singlylinkedlist" src="http://weseetips.wordpress.com/files/2008/12/singlylinkedlist.jpg" alt="singlylinkedlist" width="360" height="360" /></p>
<p><img class="alignnone size-medium wp-image-12" title="Icon How Can I Do It?" src="http://weseetips.wordpress.com/files/2008/03/icon_howcanidoit.jpg?w=220" alt="" width="220" height="32" /><br />
First of all you have to <span style="color:#0000ff;">declare a structure to hold your data.</span> Keep in mind that the <span style="color:#0000ff;">first member should be of type &#8211; </span><span style="color:#0000ff;"><strong>SLIST_ENTRY</strong></span><span style="color:#0000ff;">.</span> Then only the api&#8217;s can work with it. Then you have to <span style="color:#0000ff;">initialize the list by calling <strong>InitializeSListHead()</strong>.</span> You can <span style="color:#0000ff;">push and pop</span> elements by calling <span style="color:#0000ff;"><strong>InterlockedPushEntrySList()</strong> and <strong>InterlockedPopEntrySList()</strong>.</span> And can <span style="color:#0000ff;">flush it by calling <strong>InterlockedFlushSList()</strong></span>. Check the code snippet below. Its taken from MSDN and modified accordingly.</p>
<pre>typedef struct _LIST_DATA
{
    SLIST_ENTRY ItemEntry; // SLIST_ENTRY should be first.
    int Data;               // Your data.
} LIST_DATA, *PLIST_DATA;

int _tmain(int argc, _TCHAR* argv[])
{
    PSLIST_ENTRY FirstEntry, ListEntry;
    SLIST_HEADER ListHead;
    PLIST_DATA pListData = 0;

    // Initialize the list header.
    InitializeSListHead(&amp;ListHead);

    // Insert 10 items into the list.
    ULONG Count;
    for( Count = 0; Count &lt; 10; ++Count )
    {
        // pListData = (PLIST_DATA)malloc(sizeof(*pListData));
        pListData = new LIST_DATA;
        pListData-&gt;Data = Count;
        FirstEntry = InterlockedPushEntrySList(&amp;ListHead,
                       &amp;pListData-&gt;ItemEntry);
    }

    // Remove 10 items from the list.
    for( Count = 0; Count &lt; 10; ++Count )
    {
        ListEntry = InterlockedPopEntrySList(&amp;ListHead);
        pListData = (PLIST_DATA)( ListEntry );
        cout &lt;&lt; "Item : " &lt;&lt; pListData-&gt;Data &lt;&lt; endl;
        // free( pListData );
        delete pListData;
    }

    // Flush the list and verify that the items are gone.
    ListEntry = InterlockedFlushSList(&amp;ListHead);
    FirstEntry = InterlockedPopEntrySList(&amp;ListHead);

    if (FirstEntry != NULL)
    {
        printf("Error: List is not empty.");
    }
}</pre>
<p><img class="alignnone size-medium wp-image-18" title="Icon Note" src="http://weseetips.wordpress.com/files/2008/03/icon_note.jpg?w=94" alt="" width="94" height="32" /><br />
I think the best name will be Interlocked stack, since its using push and pop and behaves like stack. I can&#8217;t find why its named as a singly linked list. What do you think?</p>
<p><img class="alignnone size-medium wp-image-51" title="beginnerseries" src="http://weseetips.wordpress.com/files/2008/03/beginnerseries.jpg?w=215" alt="" width="215" height="32" /><br />
Targeted Audience &#8211; Beginners.</p>
]]></content:encoded>
			<wfw:commentRss>http://weseetips.com/2008/12/21/how-to-use-interlocked-singly-linked-lists/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

