Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. "__declspec(thread)" or "__declspec(thread) static"

"__declspec(thread)" or "__declspec(thread) static"

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    chenggong
    wrote on last edited by
    #1

    to make the nextBuf and workBuf in the following code multi-thread save, is it correct to replace "static" with "__declspec(thread)" in the code? or it should be "__declspec(thread) static" ? THANKS FOR THE HELP! ------ static int nextBuf ; static char workBuf[8][64] ; char * GetBuf() { nextBuf++ ; if( nextBuf >= 8 ) nextBuf = 0 ; return workBuf[nextBuf] ; }

    L C 2 Replies Last reply
    0
    • C chenggong

      to make the nextBuf and workBuf in the following code multi-thread save, is it correct to replace "static" with "__declspec(thread)" in the code? or it should be "__declspec(thread) static" ? THANKS FOR THE HELP! ------ static int nextBuf ; static char workBuf[8][64] ; char * GetBuf() { nextBuf++ ; if( nextBuf >= 8 ) nextBuf = 0 ; return workBuf[nextBuf] ; }

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      chenggong wrote:

      multi-thread save

      I guess you meant "safe"? You did not specify your problem. So deducing your intent from your code it seems you are trying to share the workbuf[] array across threads? If so, you need to synchronize access to the GetBuf() function, not put the array into TLS as _declspec(thread) would do. Not to mention putting the array but not the indexer into TLS would certainly produce undesired behavior. Also your design is, well less than [whatever]. Have you ever heard of the Singleton Design Pattern? Any Design Patterns? Are you sure you should be writing multi-threading code?

      1 Reply Last reply
      0
      • C chenggong

        to make the nextBuf and workBuf in the following code multi-thread save, is it correct to replace "static" with "__declspec(thread)" in the code? or it should be "__declspec(thread) static" ? THANKS FOR THE HELP! ------ static int nextBuf ; static char workBuf[8][64] ; char * GetBuf() { nextBuf++ ; if( nextBuf >= 8 ) nextBuf = 0 ; return workBuf[nextBuf] ; }

        C Offline
        C Offline
        cmk
        wrote on last edited by
        #3

        The static just says that the variables are local to the module (.cpp file). That is, they are not going to be directly used (by name) in any other module. Both will work, but since you have already marked them as static i would keep that. Also, you should init your global vars, even though in this case it still works without doing so. Also, since the variables are (should be) only used in the function you may want to put them there. i.e.

        char* gb( void ) {
        __declspec(thread) static int i = 0;
        __declspec(thread) static char b[8][64];

        if( i >= 8 )  i = 0;
        return( b\[i++\] );
        

        }

        ...cmk Save the whales - collect the whole set

        C 1 Reply Last reply
        0
        • C cmk

          The static just says that the variables are local to the module (.cpp file). That is, they are not going to be directly used (by name) in any other module. Both will work, but since you have already marked them as static i would keep that. Also, you should init your global vars, even though in this case it still works without doing so. Also, since the variables are (should be) only used in the function you may want to put them there. i.e.

          char* gb( void ) {
          __declspec(thread) static int i = 0;
          __declspec(thread) static char b[8][64];

          if( i >= 8 )  i = 0;
          return( b\[i++\] );
          

          }

          ...cmk Save the whales - collect the whole set

          C Offline
          C Offline
          chenggong
          wrote on last edited by
          #4

          Thank you cmk !

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups