Razor Code
Rambling about code since quite recently

Topics

User Functions





    Don't have an account yet? Sign up as a New User
    Lost your password?

Events

There are no upcoming events

Older Stories

Sunday 14-Sep

  • ACM ICPC 2008 (0)

  • Monday 11-Aug

  • NZ Programming Comp (0)

  • Sunday 27-Jul

  • Blast from the past (0)

  • Monday 21-Jul

  • Timetable generator again (0)

  • Tuesday 08-Jul

  • Hosting (2)

  • Saturday 05-Apr

  • Sparse Volume (0)

  • Friday 28-Mar

  • Carmack (0)

  • Tuesday 04-Mar

  • Back to Uni (0)
  • Dell Kill Switch Direct (0)

  • Monday 18-Feb

  • Lappy (0)

  •  Thread Specific Pointer    
     Author: 
     Dated:  Wednesday, July 15 2009 @ 12:56 AM NZST
     Viewed:  113 times  
    Programming

    So in one of the logging articles, boost::thread_specific_ptr was mentioned. In my opinion, it is a very cool little thing. It's basically just a pointer which can have a different value in each thread. Why would you want one? Well, I used to have a bit of a problem. Angel script, the scripting language I use, requires a thing called a context when calling script functions. Contexts cannot be shared between threads and I wanted to cache the context between calls to the scripting language, since creating them is relatively expensive. Calls are made by a couple of different threads, so caching the context required a bit of hackery. A thread specific pointer, however, is a perfect fit. For each call to script I check if the context for the thread is null (unlike a normal pointer it is initialised to null by default) and if it is I create the context. Otherwise I just use the one already cached for this thread.

    Also, this is what my log files look like now:

    Log file

    They're quite colourful. Completely overkill, too. But the really great thing is that I'm now happy to look at log files. I want the program to go up in flames just so I have an excuse to read their christmasy goodness! Though I suppose the novelty will wear off.




    Trackback

    Trackback URL for this entry: http://razorcode.net/trackback.php/20090715005601852

    No trackback comments for this entry.
    Thread Specific Pointer | 2 comments | Create New Account
    The following comments are owned by whomever posted them. This site is not responsible for what they say.
    Thread Specific Pointer
    Authored by: Sreerambo on Monday, July 20 2009 @ 03:20 PM NZST
    Did you just reinvent the singleton pattern or some variation of it?
    Thread Specific Pointer
    Authored by: Razor on Monday, July 20 2009 @ 05:15 PM NZST
    No.