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
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Simpel thread question(I hope:)

Simpel thread question(I hope:)

Scheduled Pinned Locked Moved C / C++ / MFC
questiondesignlearning
6 Posts 5 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.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    Hi everyone this is my first post so be gentle;) How do you best share data between threads? For instance if you have two arrays and the main plus another thread has to have access to them what is then the best way to create the thread? As a worker or UI thread and what about parameters and so on? Of course one could do it with global vars but I dont like globals an I will do anything to avoid them:) Any suggestions? Tnx in advance /Frank:)

    K N A 3 Replies Last reply
    0
    • L Lost User

      Hi everyone this is my first post so be gentle;) How do you best share data between threads? For instance if you have two arrays and the main plus another thread has to have access to them what is then the best way to create the thread? As a worker or UI thread and what about parameters and so on? Of course one could do it with global vars but I dont like globals an I will do anything to avoid them:) Any suggestions? Tnx in advance /Frank:)

      K Offline
      K Offline
      KarstenK
      wrote on last edited by
      #2

      You can give an pointer to the arrays, so the others thread can access the data. Dont ferget syncronization with Critical Section. A good idea is to work with Events.:) Or you can send Messages between the threads.:~

      1 Reply Last reply
      0
      • L Lost User

        Hi everyone this is my first post so be gentle;) How do you best share data between threads? For instance if you have two arrays and the main plus another thread has to have access to them what is then the best way to create the thread? As a worker or UI thread and what about parameters and so on? Of course one could do it with global vars but I dont like globals an I will do anything to avoid them:) Any suggestions? Tnx in advance /Frank:)

        N Offline
        N Offline
        Nish Nishant
        wrote on last edited by
        #3

        If three threads [your primary thread + the two worker threads] are accessing the same array, then you'll be better off implementing some kind of data synchronization methods. Perhaps crictical sections would suite you well in this case. As for passing the data across threads, threads are usually passed an LPVOID parameter. You can pass a pointer to a struct that wraps all your shared arays. And this pointer can be passed to all your worker threads. But as I already said, you have to implement synchronization mechanisms else your data will get corrupted quite easily. And it won't be easy to debug as well. Regards Nish


        Author of the romantic comedy Summer Love and Some more Cricket [New Win] Buy it, read it and admire me :-)

        L 1 Reply Last reply
        0
        • N Nish Nishant

          If three threads [your primary thread + the two worker threads] are accessing the same array, then you'll be better off implementing some kind of data synchronization methods. Perhaps crictical sections would suite you well in this case. As for passing the data across threads, threads are usually passed an LPVOID parameter. You can pass a pointer to a struct that wraps all your shared arays. And this pointer can be passed to all your worker threads. But as I already said, you have to implement synchronization mechanisms else your data will get corrupted quite easily. And it won't be easy to debug as well. Regards Nish


          Author of the romantic comedy Summer Love and Some more Cricket [New Win] Buy it, read it and admire me :-)

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Tnx for the quick responce both of ya! Got it then there is just one more qeustion:) So I have to use a worker thread! Because of the LPVOID param right? Can you tell me why it not possible to do that in a UI thread...why is there no LPVOID param i the UI thread? (it dont make no sence to me):confused: Am I missing something here...

          F 1 Reply Last reply
          0
          • L Lost User

            Hi everyone this is my first post so be gentle;) How do you best share data between threads? For instance if you have two arrays and the main plus another thread has to have access to them what is then the best way to create the thread? As a worker or UI thread and what about parameters and so on? Of course one could do it with global vars but I dont like globals an I will do anything to avoid them:) Any suggestions? Tnx in advance /Frank:)

            A Offline
            A Offline
            Alexandru Savescu
            wrote on last edited by
            #5

            Read this article about worker threads and how you should pass data among them. Best regards, Alexandru Savescu

            1 Reply Last reply
            0
            • L Lost User

              Tnx for the quick responce both of ya! Got it then there is just one more qeustion:) So I have to use a worker thread! Because of the LPVOID param right? Can you tell me why it not possible to do that in a UI thread...why is there no LPVOID param i the UI thread? (it dont make no sence to me):confused: Am I missing something here...

              F Offline
              F Offline
              Frank Christensen
              wrote on last edited by
              #6

              Got it:) Just read something from Alexpro's post:) Tnx again everyone Regards Frank

              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