multithreading - Filling a vector in order using threads in C++ -


i'm attempting fill huge double vector (1929x1341, might bigger) data, right takes around 10 seconds do.

for reference, code far:

vector<vector<float>> vector1; (int x = 0; x < mapwidth; x++) {     vector<float> vector2;     (int y = 0; y < mapheight; y++) {         int somenumber = calculatenumber(x,y);         vector2.push_back(somenumber);     }     vector1.push_back(vector2); } 

i'm thinking should able cut down on work-time dividing work on separate threads. separate second for-loop each own thread.

unfortunately, not threads. main issue vectors needs filled in order. can't separate second vector own threads , combine them later, put them semi-random order. i've looked mutex , condition variables, not able find solution specific problem.

would willing me out here?

you may like:

std::vector<std::vector<float>> vector1(mapwidth); std::vector<std::thread> threads;  (int x = 0; x < mapwidth; x++) {     threads.emplace_back([&, x]() {         (int y = 0; y < mapheight; y++) {             int somenumber = calculatenumber(x, y);             vector1[x].push_back(somenumber);         }     }); }  (int x = 0; x < mapwidth; x++) {     threads[x].join(); } 

Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -