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. copy/swap vector between threads...

copy/swap vector between threads...

Scheduled Pinned Locked Moved C / C++ / MFC
graphicstutorialquestion
6 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.
  • R Offline
    R Offline
    RobJones
    wrote on last edited by
    #1

    Is it possible to swap vectors between threads using std::swap() instead of iterating one vector and copying each element (given that both vectors contain the exact same type of elements)? For example I have a structure/vector in my CMainFrame and in my CWinThread...

    const struct s_list
    {
    CString strTimeStamp,
    strStatus,
    strID,
    strIPRange,
    strIPAddress,
    strName,
    strGUID,
    strOS,
    strResults;

    BOOL	bUDPReply,
    	bAgent;
    

    };
    std::vector<s_list> v_list;

    I am assuming it would be more efficient if I could swap the elements... The iteration/copy method works but it takes a good 30 seconds and hangs the application while its plowing through all the data. Any ideas? Thanks, Rob

    Whoever said nothing is impossible never tried slamming a revolving door!

    S 1 Reply Last reply
    0
    • R RobJones

      Is it possible to swap vectors between threads using std::swap() instead of iterating one vector and copying each element (given that both vectors contain the exact same type of elements)? For example I have a structure/vector in my CMainFrame and in my CWinThread...

      const struct s_list
      {
      CString strTimeStamp,
      strStatus,
      strID,
      strIPRange,
      strIPAddress,
      strName,
      strGUID,
      strOS,
      strResults;

      BOOL	bUDPReply,
      	bAgent;
      

      };
      std::vector<s_list> v_list;

      I am assuming it would be more efficient if I could swap the elements... The iteration/copy method works but it takes a good 30 seconds and hangs the application while its plowing through all the data. Any ideas? Thanks, Rob

      Whoever said nothing is impossible never tried slamming a revolving door!

      S Offline
      S Offline
      Stuart Dootson
      wrote on last edited by
      #2

      Why swap or copy? Why not share the state? Pointer to the vector that you give to the thread.

      Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

      R 1 Reply Last reply
      0
      • S Stuart Dootson

        Why swap or copy? Why not share the state? Pointer to the vector that you give to the thread.

        Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

        R Offline
        R Offline
        RobJones
        wrote on last edited by
        #3

        I have 2 threads that are pulling different portions of the data set. I wanted to merge the data into a single place to make it easier to work with and it seemed like a good idea to copy the whole vector from ThreadA (it does most the work) then iterate it and match it against the data in ThreadB. It may be easier to just break up the data and leave it in the threads. Thanks for the reply, - Rob

        Whoever said nothing is impossible never tried slamming a revolving door!

        CPalliniC S 2 Replies Last reply
        0
        • R RobJones

          I have 2 threads that are pulling different portions of the data set. I wanted to merge the data into a single place to make it easier to work with and it seemed like a good idea to copy the whole vector from ThreadA (it does most the work) then iterate it and match it against the data in ThreadB. It may be easier to just break up the data and leave it in the threads. Thanks for the reply, - Rob

          Whoever said nothing is impossible never tried slamming a revolving door!

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #4

          You haven't to actually break the data: you may give to each thread a vector of pointers to the proper elements of the original vector. :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          In testa che avete, signor di Ceprano?

          R 1 Reply Last reply
          0
          • CPalliniC CPallini

            You haven't to actually break the data: you may give to each thread a vector of pointers to the proper elements of the original vector. :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
            [My articles]

            R Offline
            R Offline
            RobJones
            wrote on last edited by
            #5

            That sounds like an interesting solution, I may have to give that a go... Thanks, Rob

            Whoever said nothing is impossible never tried slamming a revolving door!

            1 Reply Last reply
            0
            • R RobJones

              I have 2 threads that are pulling different portions of the data set. I wanted to merge the data into a single place to make it easier to work with and it seemed like a good idea to copy the whole vector from ThreadA (it does most the work) then iterate it and match it against the data in ThreadB. It may be easier to just break up the data and leave it in the threads. Thanks for the reply, - Rob

              Whoever said nothing is impossible never tried slamming a revolving door!

              S Offline
              S Offline
              Stuart Dootson
              wrote on last edited by
              #6

              RobJones wrote:

              I have 2 threads that are pulling different portions of the data set.

              How about passing the thread an iterator pair (i.e. a range[^]) it can operate on. Just need to make sure the thread alters data in place and doesn't modify (i.e. add or delete elements) the vector.

              Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

              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