Professional Web Applications Themes

Per-thread globals - Mac Programming

Hello, Is there a way to tell CodeWarrior or XCode to instantiate global variables for each thread ? (something like the __declspec(thread) in Visual) ? Eric...

  1. #1

    Default Per-thread globals

    Hello,

    Is there a way to tell CodeWarrior or XCode to instantiate global variables
    for each thread ? (something like the __declspec(thread) in Visual) ?

    Eric

    Eric Guest

  2. #2

    Default Re: Per-thread globals

    In article <BC680AAB.1B649%fr>,
    Eric VERGNAUD <fr> wrote:
     

    You may be able to use __thread for this. I'm not sure if it's
    supported with Apple's GCC, but it might be. I think you'll have to
    enable C99 (-std=c99, I think) to get this.

    Hope this helps,
    Eric

    --
    Eric Albert stanford.edu
    http://rescomp.stanford.edu/~ejalbert/
    Eric Guest

  3. #3

    Default Re: Per-thread globals

    Eric Albert <stanford.edu> wrote in message news:<stanford.edu>... 
    >
    > You may be able to use __thread for this. I'm not sure if it's
    > supported with Apple's GCC, but it might be. I think you'll have to
    > enable C99 (-std=c99, I think) to get this.
    >
    > Hope this helps,
    > Eric[/ref]

    (1) In Pthreads, when you start a thread you pass in a void pointer.
    This can point to a data structure
    containing the globals for that thread.
    (2) There are also the concept of in-thread globals in Pthreads.
    (Per-Thread Context Routines) see the man page on pthread then
    Per-Thread Context Routines.

    --jim
    Jim Guest

  4. #4

    Default Re: Per-thread globals

    dans l'article google.com, Jim Schimpf
    net a crit le 1/03/04 5:10:
     
    >>
    >> You may be able to use __thread for this. I'm not sure if it's
    >> supported with Apple's GCC, but it might be. I think you'll have to
    >> enable C99 (-std=c99, I think) to get this.
    >>
    >> Hope this helps,
    >> Eric[/ref]
    >
    > (1) In Pthreads, when you start a thread you pass in a void pointer.
    > This can point to a data structure
    > containing the globals for that thread.
    > (2) There are also the concept of in-thread globals in Pthreads.
    > (Per-Thread Context Routines) see the man page on pthread then
    > Per-Thread Context Routines.
    >
    > --jim[/ref]

    Thanks, but since my threads are created through NSThreads, I cannot use
    this method. Also, what I'm looking for is a compile time instanciation.
    What I'm trying to do is make a library - for which I have the source code -
    thread safe, by simply redeclaring its global variables.

    Eric

    Eric Guest

  5. #5

    Default Re: Per-thread globals

    Eric VERGNAUD wrote: 

    NSThreads are a wrapper over the top of pthreads. therefore, you can do
    most anything in NSThread that you can do in a pthread.

    in this case, you can get the same functionality when you call:

    [NSThread detachNewThreadSelector:sel toTarget:targ withObject:arg];

    make 'arg' be an NSObject that contains your globals for that thread.
    or have it contain a void* to whatever, which would exactly mirror the
    way you start a pthread.
     

    beggars can't be choosers, pal! you've been shown several ways this can
    be made to work. so it doesn't work exactly the way it does in
    microsoft-land -- i'd consider that a plus, personally.
    Jhnny Guest

  6. #6

    Default Re: Per-thread globals

    On Mon, 1 Mar 2004, Eric VERGNAUD wrote:
     
    >
    > Thanks, but since my threads are created through NSThreads, I cannot use
    > this method.[/ref]

    NSThreads are very small wrappers around pthreads; everything that works
    from pthreads will work from NSThreads, because they *are* pthreads.

    Of course, that doesn't help you make compile-time instantiations, but
    this method is available to you if that doesn't turn out to be possible.
    Michael Guest

  7. #7

    Default Re: Per-thread globals

    dans l'article nashville.comcast.net, Jhnny
    Fvrt (it means "halo, then resonate") com a crit le
    1/03/04 15:09:
     
    >
    > NSThreads are a wrapper over the top of pthreads. therefore, you can do
    > most anything in NSThread that you can do in a pthread.
    >
    > in this case, you can get the same functionality when you call:
    >
    > [NSThread detachNewThreadSelector:sel toTarget:targ withObject:arg];
    >
    > make 'arg' be an NSObject that contains your globals for that thread.
    > or have it contain a void* to whatever, which would exactly mirror the
    > way you start a pthread.[/ref]

    No it wouldn't, because the parent thread would have to know about the
    globals used by the child thread
     
    >
    > beggars can't be choosers, pal! you've been shown several ways this can
    > be made to work. so it doesn't work exactly the way it does in
    > microsoft-land -- i'd consider that a plus, personally.[/ref]

    Well as Eric Albert mentioned, with C99 one should be allowed to use
    __thread. Unfortunately it s not supported by CodeWarrior.

    I really hate the idea of stuffing the code with things like writeglobal,
    readglobal etc... So I ended up changing the code to remove all globals.

    Eric

    Eric Guest

  8. #8

    Default Re: Per-thread globals

    dans l'article twistedsys.net, Michael Ash
    com a crit le 1/03/04 15:51:
     
    >>
    >> Thanks, but since my threads are created through NSThreads, I cannot use
    >> this method.[/ref]
    >
    > NSThreads are very small wrappers around pthreads; everything that works
    > from pthreads will work from NSThreads, because they *are* pthreads.
    >
    > Of course, that doesn't help you make compile-time instantiations, but
    > this method is available to you if that doesn't turn out to be possible.[/ref]

    What I meant is I couldn't start a pthread with a pointer.

    Thanks anyway.

    Eric

    Eric Guest

  9. #9

    Default Re: Per-thread globals

    Eric VERGNAUD wrote: 
    > >
    > > (1) In Pthreads, when you start a thread you pass in a void pointer.
    > > This can point to a data structure
    > > containing the globals for that thread.
    > > (2) There are also the concept of in-thread globals in Pthreads.
    > > (Per-Thread Context Routines) see the man page on pthread then
    > > Per-Thread Context Routines.
    > >
    > > --jim[/ref]
    >
    > Thanks, but since my threads are created through NSThreads, I cannot use
    > this method. Also, what I'm looking for is a compile time instanciation.
    > What I'm trying to do is make a library - for which I have the source code -
    > thread safe, by simply redeclaring its global variables.[/ref]

    See -threadDictionary.

    -jcr
    John Guest

Similar Threads

  1. Newbie:Using ASP.NET thread pool thread to dispatch TCP data, etc.
    By Navin Mishra in forum ASP.NET Web Services
    Replies: 0
    Last Post: March 22nd, 06:45 PM
  2. Replies: 0
    Last Post: October 2nd, 10:52 PM
  3. #25708 [WFx]: extract($GLOBALS, EXTR_REFS); can corrupt the $GLOBALS array.
    By cdragon at draconic dot com in forum PHP Development
    Replies: 0
    Last Post: October 1st, 01:39 AM
  4. Replies: 0
    Last Post: October 1st, 12:59 AM
  5. #25708 [NEW]: extract($GLOBALS, EXTR_REFS); can corrupt the $GLOBALS array.
    By cdragon at draconic dot com in forum PHP Development
    Replies: 0
    Last Post: September 30th, 10:25 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139