Loop for creating threads and joining with variable number of threads










2















I am building a program that, for testing purposes can create N number of threads in C++.
I am relativity new to C++ and my current attempt so far is



//Create a list of threads
std::vector<std::thread> t;
for(i=0; i < THREADS; i ++)
std::thread th = std::thread(() workThreadProcess(); );
t.push_back(th);
printf("Thread started n");


for(std::thread th : t)
th.join();



I currently an error that says call to deleted constructor of 'std::thread'. I am unusre what this means or how to fix in



Note:

I have looked at:




  • Create variable number of std::threads


  • Variable number of threads c++


  • Array of threads and attempting to pass multiple arguments to function is not working?


  • vector of std::threads


  • Creating N number of threads

But I don't feel they answer my question. Most of them use pthreads or a a different constructor.










share|improve this question
























  • When asking about build errors, first of all try to create a Minimal, Complete, and Verifiable Example that you can show us. Then copy-paste (as text) the full and complete build log from that example into the question body. And please mark out the lines in the example where the errors are, with for example comments. Also please take some time to review how to ask good questions, as well as this question checklist.

    – Some programmer dude
    Nov 15 '18 at 23:04






  • 3





    A hint though: You loop joining the threads should probably not copy the thread objects.

    – Some programmer dude
    Nov 15 '18 at 23:05






  • 2





    The error message isn't lieing. std::thread is defined with 'thread(const thread&) = delete;' You cannot copy it. How would you write the copy constructor for a thread, if it was you? Can you identify the copy in your code and fix that somehow?

    – Christopher Pisz
    Nov 15 '18 at 23:22







  • 3





    Hint: Threads are not copyable.

    – NathanOliver
    Nov 15 '18 at 23:23















2















I am building a program that, for testing purposes can create N number of threads in C++.
I am relativity new to C++ and my current attempt so far is



//Create a list of threads
std::vector<std::thread> t;
for(i=0; i < THREADS; i ++)
std::thread th = std::thread(() workThreadProcess(); );
t.push_back(th);
printf("Thread started n");


for(std::thread th : t)
th.join();



I currently an error that says call to deleted constructor of 'std::thread'. I am unusre what this means or how to fix in



Note:

I have looked at:




  • Create variable number of std::threads


  • Variable number of threads c++


  • Array of threads and attempting to pass multiple arguments to function is not working?


  • vector of std::threads


  • Creating N number of threads

But I don't feel they answer my question. Most of them use pthreads or a a different constructor.










share|improve this question
























  • When asking about build errors, first of all try to create a Minimal, Complete, and Verifiable Example that you can show us. Then copy-paste (as text) the full and complete build log from that example into the question body. And please mark out the lines in the example where the errors are, with for example comments. Also please take some time to review how to ask good questions, as well as this question checklist.

    – Some programmer dude
    Nov 15 '18 at 23:04






  • 3





    A hint though: You loop joining the threads should probably not copy the thread objects.

    – Some programmer dude
    Nov 15 '18 at 23:05






  • 2





    The error message isn't lieing. std::thread is defined with 'thread(const thread&) = delete;' You cannot copy it. How would you write the copy constructor for a thread, if it was you? Can you identify the copy in your code and fix that somehow?

    – Christopher Pisz
    Nov 15 '18 at 23:22







  • 3





    Hint: Threads are not copyable.

    – NathanOliver
    Nov 15 '18 at 23:23













2












2








2








I am building a program that, for testing purposes can create N number of threads in C++.
I am relativity new to C++ and my current attempt so far is



//Create a list of threads
std::vector<std::thread> t;
for(i=0; i < THREADS; i ++)
std::thread th = std::thread(() workThreadProcess(); );
t.push_back(th);
printf("Thread started n");


for(std::thread th : t)
th.join();



I currently an error that says call to deleted constructor of 'std::thread'. I am unusre what this means or how to fix in



Note:

I have looked at:




  • Create variable number of std::threads


  • Variable number of threads c++


  • Array of threads and attempting to pass multiple arguments to function is not working?


  • vector of std::threads


  • Creating N number of threads

But I don't feel they answer my question. Most of them use pthreads or a a different constructor.










share|improve this question
















I am building a program that, for testing purposes can create N number of threads in C++.
I am relativity new to C++ and my current attempt so far is



//Create a list of threads
std::vector<std::thread> t;
for(i=0; i < THREADS; i ++)
std::thread th = std::thread(() workThreadProcess(); );
t.push_back(th);
printf("Thread started n");


for(std::thread th : t)
th.join();



I currently an error that says call to deleted constructor of 'std::thread'. I am unusre what this means or how to fix in



Note:

I have looked at:




  • Create variable number of std::threads


  • Variable number of threads c++


  • Array of threads and attempting to pass multiple arguments to function is not working?


  • vector of std::threads


  • Creating N number of threads

But I don't feel they answer my question. Most of them use pthreads or a a different constructor.







c++ multithreading loops






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 1:25









shizhen

3,76441235




3,76441235










asked Nov 15 '18 at 23:00









C. BegleyC. Begley

156




156












  • When asking about build errors, first of all try to create a Minimal, Complete, and Verifiable Example that you can show us. Then copy-paste (as text) the full and complete build log from that example into the question body. And please mark out the lines in the example where the errors are, with for example comments. Also please take some time to review how to ask good questions, as well as this question checklist.

    – Some programmer dude
    Nov 15 '18 at 23:04






  • 3





    A hint though: You loop joining the threads should probably not copy the thread objects.

    – Some programmer dude
    Nov 15 '18 at 23:05






  • 2





    The error message isn't lieing. std::thread is defined with 'thread(const thread&) = delete;' You cannot copy it. How would you write the copy constructor for a thread, if it was you? Can you identify the copy in your code and fix that somehow?

    – Christopher Pisz
    Nov 15 '18 at 23:22







  • 3





    Hint: Threads are not copyable.

    – NathanOliver
    Nov 15 '18 at 23:23

















  • When asking about build errors, first of all try to create a Minimal, Complete, and Verifiable Example that you can show us. Then copy-paste (as text) the full and complete build log from that example into the question body. And please mark out the lines in the example where the errors are, with for example comments. Also please take some time to review how to ask good questions, as well as this question checklist.

    – Some programmer dude
    Nov 15 '18 at 23:04






  • 3





    A hint though: You loop joining the threads should probably not copy the thread objects.

    – Some programmer dude
    Nov 15 '18 at 23:05






  • 2





    The error message isn't lieing. std::thread is defined with 'thread(const thread&) = delete;' You cannot copy it. How would you write the copy constructor for a thread, if it was you? Can you identify the copy in your code and fix that somehow?

    – Christopher Pisz
    Nov 15 '18 at 23:22







  • 3





    Hint: Threads are not copyable.

    – NathanOliver
    Nov 15 '18 at 23:23
















When asking about build errors, first of all try to create a Minimal, Complete, and Verifiable Example that you can show us. Then copy-paste (as text) the full and complete build log from that example into the question body. And please mark out the lines in the example where the errors are, with for example comments. Also please take some time to review how to ask good questions, as well as this question checklist.

– Some programmer dude
Nov 15 '18 at 23:04





When asking about build errors, first of all try to create a Minimal, Complete, and Verifiable Example that you can show us. Then copy-paste (as text) the full and complete build log from that example into the question body. And please mark out the lines in the example where the errors are, with for example comments. Also please take some time to review how to ask good questions, as well as this question checklist.

– Some programmer dude
Nov 15 '18 at 23:04




3




3





A hint though: You loop joining the threads should probably not copy the thread objects.

– Some programmer dude
Nov 15 '18 at 23:05





A hint though: You loop joining the threads should probably not copy the thread objects.

– Some programmer dude
Nov 15 '18 at 23:05




2




2





The error message isn't lieing. std::thread is defined with 'thread(const thread&) = delete;' You cannot copy it. How would you write the copy constructor for a thread, if it was you? Can you identify the copy in your code and fix that somehow?

– Christopher Pisz
Nov 15 '18 at 23:22






The error message isn't lieing. std::thread is defined with 'thread(const thread&) = delete;' You cannot copy it. How would you write the copy constructor for a thread, if it was you? Can you identify the copy in your code and fix that somehow?

– Christopher Pisz
Nov 15 '18 at 23:22





3




3





Hint: Threads are not copyable.

– NathanOliver
Nov 15 '18 at 23:23





Hint: Threads are not copyable.

– NathanOliver
Nov 15 '18 at 23:23












1 Answer
1






active

oldest

votes


















3














You can't copy threads. You need to move them in order to get them into the vector. Also, you can't create temporary copies in the loop to join them: you have to use a references instead.



Here a working version



std::vector<std::thread> t;
for(int i=0; i < THREADS; i ++)
std::thread th = std::thread(() workThreadProcess(); );
t.push_back(std::move(th)); //<=== move (after, th doesn't hold it anymore
std::cout<<"Thread started"<<std::endl;


for(auto& th : t) //<=== range-based for uses & reference
th.join();



Online demo






share|improve this answer


















  • 1





    This is the right explanation of the error, but OP would probably be better off using emplace_back as in t.emplace_back( workThreadProcess(); ); rather than creating a std::thread object only to move it

    – Ryan Haining
    Nov 15 '18 at 23:58










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53329078%2floop-for-creating-threads-and-joining-with-variable-number-of-threads%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














You can't copy threads. You need to move them in order to get them into the vector. Also, you can't create temporary copies in the loop to join them: you have to use a references instead.



Here a working version



std::vector<std::thread> t;
for(int i=0; i < THREADS; i ++)
std::thread th = std::thread(() workThreadProcess(); );
t.push_back(std::move(th)); //<=== move (after, th doesn't hold it anymore
std::cout<<"Thread started"<<std::endl;


for(auto& th : t) //<=== range-based for uses & reference
th.join();



Online demo






share|improve this answer


















  • 1





    This is the right explanation of the error, but OP would probably be better off using emplace_back as in t.emplace_back( workThreadProcess(); ); rather than creating a std::thread object only to move it

    – Ryan Haining
    Nov 15 '18 at 23:58















3














You can't copy threads. You need to move them in order to get them into the vector. Also, you can't create temporary copies in the loop to join them: you have to use a references instead.



Here a working version



std::vector<std::thread> t;
for(int i=0; i < THREADS; i ++)
std::thread th = std::thread(() workThreadProcess(); );
t.push_back(std::move(th)); //<=== move (after, th doesn't hold it anymore
std::cout<<"Thread started"<<std::endl;


for(auto& th : t) //<=== range-based for uses & reference
th.join();



Online demo






share|improve this answer


















  • 1





    This is the right explanation of the error, but OP would probably be better off using emplace_back as in t.emplace_back( workThreadProcess(); ); rather than creating a std::thread object only to move it

    – Ryan Haining
    Nov 15 '18 at 23:58













3












3








3







You can't copy threads. You need to move them in order to get them into the vector. Also, you can't create temporary copies in the loop to join them: you have to use a references instead.



Here a working version



std::vector<std::thread> t;
for(int i=0; i < THREADS; i ++)
std::thread th = std::thread(() workThreadProcess(); );
t.push_back(std::move(th)); //<=== move (after, th doesn't hold it anymore
std::cout<<"Thread started"<<std::endl;


for(auto& th : t) //<=== range-based for uses & reference
th.join();



Online demo






share|improve this answer













You can't copy threads. You need to move them in order to get them into the vector. Also, you can't create temporary copies in the loop to join them: you have to use a references instead.



Here a working version



std::vector<std::thread> t;
for(int i=0; i < THREADS; i ++)
std::thread th = std::thread(() workThreadProcess(); );
t.push_back(std::move(th)); //<=== move (after, th doesn't hold it anymore
std::cout<<"Thread started"<<std::endl;


for(auto& th : t) //<=== range-based for uses & reference
th.join();



Online demo







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 '18 at 23:52









ChristopheChristophe

41k43578




41k43578







  • 1





    This is the right explanation of the error, but OP would probably be better off using emplace_back as in t.emplace_back( workThreadProcess(); ); rather than creating a std::thread object only to move it

    – Ryan Haining
    Nov 15 '18 at 23:58












  • 1





    This is the right explanation of the error, but OP would probably be better off using emplace_back as in t.emplace_back( workThreadProcess(); ); rather than creating a std::thread object only to move it

    – Ryan Haining
    Nov 15 '18 at 23:58







1




1





This is the right explanation of the error, but OP would probably be better off using emplace_back as in t.emplace_back( workThreadProcess(); ); rather than creating a std::thread object only to move it

– Ryan Haining
Nov 15 '18 at 23:58





This is the right explanation of the error, but OP would probably be better off using emplace_back as in t.emplace_back( workThreadProcess(); ); rather than creating a std::thread object only to move it

– Ryan Haining
Nov 15 '18 at 23:58



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53329078%2floop-for-creating-threads-and-joining-with-variable-number-of-threads%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

政党

天津地下鉄3号線