<?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>Fishing Cactus development blog &#187; Programming</title>
	<atom:link href="http://blog.fishingcactus.com/index.php/tag/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.fishingcactus.com</link>
	<description>We don't create games that look like others; we don't create games that play like others</description>
	<lastBuildDate>Wed, 04 Jan 2012 11:46:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
		<item>
		<title>AiGameDev Conference 2010: EVE Online Characters</title>
		<link>http://blog.fishingcactus.com/index.php/2010/07/19/674/</link>
		<comments>http://blog.fishingcactus.com/index.php/2010/07/19/674/#comments</comments>
		<pubDate>Mon, 19 Jul 2010 08:44:45 +0000</pubDate>
		<dc:creator>christophe.bernard</dc:creator>
				<category><![CDATA[Game Design]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[ai]]></category>
		<category><![CDATA[aigamedev]]></category>
		<category><![CDATA[game]]></category>

		<guid isPermaLink="false">http://blog.fishingcactus.com/?p=674</guid>
		<description><![CDATA[UPDATE: post is now under the right author name. During the AiGameDev Conference 2010 at Paris Claudio Pedica, a researcher at CADIA Labs, Reykjavik University, currently collaborating with CCP Games presented us their new social interaction system in a talk titled Human Territoriality for EVE Online&#8217;s Socially Smart Characters. This was a short preview on how the space [...]]]></description>
			<content:encoded><![CDATA[<p><em>UPDATE: post is now under the right author name.</em></p>
<p>During the <a href="http://aigamedev.com/" target="_blank">AiGameDev Conference 2010</a> at Paris Claudio Pedica, a researcher at <a href="http://cadia.ru.is/" target="_blank">CADIA Labs</a>, Reykjavik University, currently collaborating with <a href="http://www.ccpgames.com" target="_blank">CCP Games</a> presented us their new social interaction system in a talk titled Human Territoriality for EVE Online&#8217;s Socially Smart Characters.</p>
<p style="text-align: center;"><img class="aligncenter" style="border: 2px solid black;" src="http://files.aigamedev.com/coverage/GAIC10h_ClaudioPedica.large.jpg" alt="" width="216" height="324" /></p>
<p style="text-align: left;"><span id="more-674"></span>This was a short preview on how the space stations are going to be populated in the next EVE online expansion.</p>
<p>The main focus of the research conducted by CCP is based on the lack of feedback in games when it comes to interactions. For example if you pass besides a game character, he doesn’t notice you or in worse cases, if you bump into him, the most common reaction is that he won&#8217;t even step aside and your character stops dumbly like when facing a wall! Fortunately, not all games react like this. In GTA when you hit someone or pass close to him he steps aside or even says something to you.</p>
<p>What the guys at CCP are trying to do is to recreate an interaction system based on how humans practically cope with interactions and how they react to other people’s physical presence.</p>
<p>In the demos shown we could see the﻿ character move close to a group of NPCs; when close enough, the group understands that this character wants to be part of their group so they take a step aside to form a bigger but still homogeneous group.<img src="file:///C:/Users/FishingCactus_Andrea/Desktop/ccp01.jpg" alt="" /></p>
<p style="text-align: center;"><a href="http://blog.fishingcactus.com/wp-content/uploads/2010/07/ccp01.jpg"><img style="border: 2px solid black;" title="ccp01" src="http://blog.fishingcactus.com/wp-content/uploads/2010/07/ccp01-300x225.jpg" alt="" width="300" height="225" /></a></p>
<p>The “circle” itself is, in this system, the interaction unit; each character and group possesses one and each unit is designed for a particular type of interaction.</p>
<p>1) First there is the personal circle which defines the limit of the body area.</p>
<p>2) The external circle is used for the close social interactions like dialogs or people walking near (also true for groups).</p>
<p>3) An inner circle is also defined by the participants of the group: a circle defines its body and the external circle of it. In another example we saw one participant moving a few steps from his former position and the rest of the group following him to recreate the circle.</p>
<p><img src="file:///C:/Users/FishingCactus_Andrea/Desktop/ccp01.png" alt="" /></p>
<p>I actually don&#8217;t play EVE Online at all and I don&#8217;t really know what they are planning to do with the new space stations system but having this feature is über-cool and I think it would really improve the experience of the players (at least contributing to a more immersive experience).</p>
<p>Having people walking from one spot to another or joining conversations or even reacting to the players&#8217; presence will surely make us feel more in a real living world. When you think that some games only feature static NPCs, this talk gets even more impressing!</p>
<p>Human Territoriality for EVE Online&#8217;s Socially Smart Characters by Claudio Pedica can be downloaded from the <a href="http://aigamedev.com/" target="_blank">AiGameDev website</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.fishingcactus.com/index.php/2010/07/19/674/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Linux version of Mojito Engine on its way!</title>
		<link>http://blog.fishingcactus.com/index.php/2010/01/22/linux-version-of-mojito-engine-on-its-way/</link>
		<comments>http://blog.fishingcactus.com/index.php/2010/01/22/linux-version-of-mojito-engine-on-its-way/#comments</comments>
		<pubDate>Fri, 22 Jan 2010 13:50:38 +0000</pubDate>
		<dc:creator>Christophe Clementi</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[mojito]]></category>

		<guid isPermaLink="false">http://blog.fishingcactus.com/?p=274</guid>
		<description><![CDATA[With the mondial crysis, it&#8217;s important to think about the people who are in need. They maybe can&#8217;t buy a fully functional Mac or an expensive Windows licence&#8230; Luckily, they can discover the joy of working with the open-source &#8220;OS for the poor&#8221;, Linux! We have already improved the most current platforms on the market [...]]]></description>
			<content:encoded><![CDATA[<p>With the mondial crysis, it&#8217;s important to think about the people who are in need. They maybe can&#8217;t buy a fully functional Mac or an expensive Windows licence&#8230;</p>
<p>Luckily, they can discover the joy of working with the open-source &#8220;OS for the poor&#8221;, Linux!</p>
<p>We have already improved the most current platforms on the market with our kickass Mojito Engine, and we are now currently working on the Linux version.</p>
<p>And just because we like you guys, readers of this blog, we will offer you the exclusivity of the year : the first screenshot of a not-so-mysterious project, running on Ubuntu!</p>
<p style="text-align: center"><a href="http://blog.fishingcactus.com/wp-content/uploads/2010/01/first-screen-linux1.jpg"><img class="size-medium wp-image-281 aligncenter" style="border: 1px solid black" src="http://blog.fishingcactus.com/wp-content/uploads/2010/01/first-screen-linux1-300x149.jpg" alt="Awesome Mojito on Linux" width="300" height="149" /></a></p>
<p>I know how much it&#8217;s beautiful, but don&#8217;t cry yet, keep your tears when it will be fully functional so stay tuned!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.fishingcactus.com/index.php/2010/01/22/linux-version-of-mojito-engine-on-its-way/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Cross Platform Thread Local Storage (cont&#8217;d)</title>
		<link>http://blog.fishingcactus.com/index.php/2009/12/21/cross-platform-thread-local-storage-contd/</link>
		<comments>http://blog.fishingcactus.com/index.php/2009/12/21/cross-platform-thread-local-storage-contd/#comments</comments>
		<pubDate>Mon, 21 Dec 2009 16:36:55 +0000</pubDate>
		<dc:creator>Julien Hamaide</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[parallel]]></category>
		<category><![CDATA[thread local storage]]></category>

		<guid isPermaLink="false">http://blog.fishingcactus.com/?p=95</guid>
		<description><![CDATA[In the first post, I introduced the thread local storage we use across all platforms. The main goal was to provide similar interface and features as __declspec( thread ) variables provided by some compilers. The previous post reproduced in pure C++ what gcc and cl.exe allowed as extension, but on every platform that supports at [...]]]></description>
			<content:encoded><![CDATA[<p>In the first post, I introduced the thread local storage we use across all platforms. The main goal was to provide similar interface and features as __declspec( thread ) variables provided by some compilers. The previous post reproduced in pure C++ what gcc and cl.exe allowed as extension, but on every platform that supports at least 1 thread local storage. The goal of this post is to extend the existing system to all types including complex classes.</p>
<p><span id="more-95"></span><br />
Compilers only allow TLS on POD ( Plain Old Data ) types, no object can be used. This extension allows to use all types of objects, as long as it has a default constructor.</p>
<p>Allowing complex types in TLS leads to 2 challenges : allocating enough memory to contain the object and calling constructors and destructors on thread startup and exit.</p>
<p>In the <a href="http://blog.fishingcactus.com/index.php/2009/07/20/cross-platform-thread-local-storage/trackback/">previous version</a>, the memory used for TLS was a table of 4 bytes-long items. We could have used this system to store a pointer to the complex object, but we tried to avoid dereferences as much as possible. The system is then changed to contain any size needed. Each TLS instance contains the index inside a buffer. This buffer is created for each new thread and contains the memory needed to store all objects. The base class constructor now takes the size of the object and tracks the total size in a static variable. When the thread starts, the buffer is allocated and all objects are constructed inside this buffer. But more on this later. This technique has the advantage of allocating a single chunk of memory for a thread. It avoids memory waste of multiple allocations.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">&nbsp;
<span style="color: #0000ff;">class</span> ThreadLocalStorageBase
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">int</span> m_buffer_size <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">static</span> TLSIndex m_tls_identifier<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">int</span> m_index<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">protected</span><span style="color: #008080;">:</span>
    ThreadLocalStorageBase<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">int</span> object_size <span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        object_size <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span> object_size <span style="color: #000040;">+</span> Alignment <span style="color: #000040;">-</span> <span style="color: #0000dd;">1</span> <span style="color: #008000;">&#41;</span> <span style="color: #000040;">&amp;</span> ~<span style="color: #008000;">&#40;</span> Alignment <span style="color: #000040;">-</span> <span style="color: #0000dd;">1</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        m_index <span style="color: #000080;">=</span> m_buffer_size<span style="color: #008080;">;</span>
        m_buffer_size <span style="color: #000040;">+</span><span style="color: #000080;">=</span> object_size<span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0000ff;">void</span> <span style="color: #000040;">*</span> GetThreadLocalStorage<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span> 
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span> local_storage_table <span style="color: #000080;">=</span> <span style="color: #0000ff;">reinterpret_cast</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">char</span><span style="color: #000040;">*</span><span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span> TLSGet<span style="color: #008000;">&#40;</span> m_tls_identifier <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        <span style="color: #0000ff;">return</span> <span style="color: #000040;">&amp;</span>local_storage_table<span style="color: #008000;">&#91;</span> m_index <span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">template</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> T<span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">class</span> ThreadLocalStorage<span style="color: #000080;">&lt;</span>T<span style="color: #000080;">&gt;</span> <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> ThreadLocalStorageBase
<span style="color: #008000;">&#123;</span>
    ThreadLocalStorage<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008080;">:</span> ThreadLocalStorageBase<span style="color: #008000;">&#40;</span> <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span> T <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>OS TLS has the property to initialize the value to zero the first time a thread accesses it. As we support any class, we need to provide a way to construct the objects. We decided to initialize all variables before entering the thread function. If variables are not used frequently, a lazy initialization mechanism can be implemented. In Mojito, a class encapsulates the OS thread object. We have then the place to call this initialization. The best mechanism depends on the way you will use TLS variables. </p>
<p>In both cases, when the thread is about to end, the allocated memory must be freed. So a call to the TLS finalizer must be done. ( pthread allows a destructor function to be registered, so you can take advantage of it ). Once again, in Mojito, the thread class is taking care of it.</p>
<p>A question that might have arose : how do you have access to all variables spread across the code? The trick is quite simple : in the constructor, register the variable into a single-linked list. This technique ensures that as soon as <em>main</em> is entered, the linked list is complete. I discovered this trick in the command line option package of <a href="http://llvm.org/">LLVM</a></p>
<p>As we have a class for each type, a base class has been created. It takes care of creating the list and provides the functions to initialize and finalize the variables. It also keeps track of accessing the thread local buffer.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> ThreadLocalStorageBase
<span style="color: #008000;">&#123;</span>
    ...
    <span style="color: #007788;">ThreadLocalStorageBase</span> <span style="color: #000040;">*</span> m_next<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">static</span> ThreadLocalStorageBase <span style="color: #000040;">*</span> m_first <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
    ThreadLocalStorageBase<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">int</span> object_size <span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        ...
        <span style="color: #007788;">m_next</span> <span style="color: #000080;">=</span> m_first<span style="color: #008080;">;</span>
        m_first <span style="color: #000080;">=</span> <span style="color: #0000dd;">this</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
     <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> StartupOnThreadEnter<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
     <span style="color: #008000;">&#123;</span>
        <span style="color: #0000ff;">void</span> <span style="color: #000040;">*</span> local_storage_table<span style="color: #008080;">;</span>
&nbsp;
        local_storage_table <span style="color: #000080;">=</span> <span style="color: #0000dd;">malloc</span><span style="color: #008000;">&#40;</span> m_buffer_size <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        <span style="color: #0000dd;">memset</span><span style="color: #008000;">&#40;</span> local_storage_table, <span style="color: #0000dd;">0</span>, m_buffer_size <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
        TLSSet<span style="color: #008000;">&#40;</span> m_tls_identifier, <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span>local_storage_table <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
        ThreadLocalStorageBase <span style="color: #000040;">*</span> current <span style="color: #000080;">=</span> m_first<span style="color: #008080;">;</span>
&nbsp;
        <span style="color: #0000ff;">while</span><span style="color: #008000;">&#40;</span> current <span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            current<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>InitializeObject<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
            current <span style="color: #000080;">=</span> current<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>m_next<span style="color: #008080;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> CleanupOnThreadExit<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
       ThreadLocalStorageBase <span style="color: #000040;">*</span> current <span style="color: #000080;">=</span> m_first<span style="color: #008080;">;</span>
&nbsp;
        <span style="color: #0000ff;">while</span><span style="color: #008000;">&#40;</span> current <span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            current<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>FinalizeObject<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
            current <span style="color: #000080;">=</span> current<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>m_next<span style="color: #008080;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0000dd;">free</span><span style="color: #008000;">&#40;</span> TLSGet<span style="color: #008000;">&#40;</span> m_tls_identifier <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">protected</span><span style="color: #008080;">:</span>
&nbsp;
    <span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">void</span> InitializeObject<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">void</span> FinalizeObject<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>

<p>When the base class is in place, here are the modifications made to the subclass</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">template</span><span style="color: #000080;">&lt;</span> <span style="color: #0000ff;">typename</span> T <span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">class</span> ThreadLocalStorage <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> ThreadLocalStorageBase
<span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
    operator <span style="color: #0000ff;">const</span> T<span style="color: #000040;">&amp;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0000ff;">void</span> <span style="color: #000040;">*</span> thread_local_storage <span style="color: #000080;">=</span> GetThreadLocalStorage<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        <span style="color: #0000ff;">return</span> <span style="color: #000040;">*</span><span style="color: #0000ff;">reinterpret_cast</span><span style="color: #000080;">&lt;</span>T<span style="color: #000040;">*</span><span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span> thread_local_storage <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    ThreadLocalStorage <span style="color: #000040;">&amp;</span> operator<span style="color: #000080;">=</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> T <span style="color: #000040;">&amp;</span> value <span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0000ff;">void</span> <span style="color: #000040;">*</span> thread_local_storage <span style="color: #000080;">=</span> GetThreadLocalStorage<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        <span style="color: #000040;">*</span><span style="color: #0000ff;">reinterpret_cast</span><span style="color: #000080;">&lt;</span>T<span style="color: #000040;">*</span><span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span> thread_local_storage <span style="color: #008000;">&#41;</span> <span style="color: #000080;">=</span> value<span style="color: #008080;">;</span>
        <span style="color: #0000ff;">return</span> <span style="color: #000040;">*</span><span style="color: #0000dd;">this</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">protected</span><span style="color: #008080;">:</span>
&nbsp;
    <span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">void</span> InitializeObject<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0000dd;">new</span><span style="color: #008000;">&#40;</span> GetThreadLocalStorage<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span> T<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">void</span> FinalizeObject<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0000ff;">static_cast</span><span style="color: #000080;">&lt;</span>T<span style="color: #000040;">*</span><span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span> GetThreadLocalStorage<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span><span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>~T<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>

<p>We now have a complete implementation of TLS that supports all types of object. But what if you wanted to initialize your variable to a specific value. This last modifications uses the container object to store a default value and use it when initializing the TLS instance.</p>
<p>The code is very simple. We just include a constant instance of the variable&#8217;s type, that we fill using the constructor. We then provide this instance to the constructor.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">template</span><span style="color: #000080;">&lt;</span> <span style="color: #0000ff;">typename</span> T <span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">class</span> ThreadLocalStorage <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> ThreadLocalStorageBase
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">const</span> T m_default_value<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
    ThreadLocalStorage<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> T <span style="color: #000040;">&amp;</span> default_value <span style="color: #000080;">=</span> T<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span> <span style="color: #008080;">:</span>
        ThreadLocalStorageBase<span style="color: #008000;">&#40;</span> <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span> T <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span>,
        m_default_value<span style="color: #008000;">&#40;</span> default_value <span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">protected</span><span style="color: #008080;">:</span>
&nbsp;
    <span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">void</span> InitializeObject<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0000dd;">new</span><span style="color: #008000;">&#40;</span> GetThreadLocalStorage<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span> T<span style="color: #008000;">&#40;</span> value <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">void</span> FinalizeObject<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0000ff;">static_cast</span><span style="color: #000080;">&lt;</span>T<span style="color: #000040;">*</span><span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span> GetThreadLocalStorage<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span><span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>~T<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>This concludes this pair of posts on Thread Local Storage as we use them in Mojito. Comments are warmly welcomed. Special thanks to <a href="http://www.bjoernknafla.com/Home.html">Bjoern Knafla</a> for his comments.</p>
<p>The source code can be found <a href="http://www.fishingcactus.com/blog/TLS2.zip">here</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.fishingcactus.com/index.php/2009/12/21/cross-platform-thread-local-storage-contd/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

