Loop for creating threads and joining with variable number of threads
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
add a comment |
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
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
add a comment |
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
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
c++ multithreading loops
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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
1
This is the right explanation of the error, but OP would probably be better off usingemplace_back
as int.emplace_back( workThreadProcess(); );
rather than creating astd::thread
object only to move it
– Ryan Haining
Nov 15 '18 at 23:58
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
1
This is the right explanation of the error, but OP would probably be better off usingemplace_back
as int.emplace_back( workThreadProcess(); );
rather than creating astd::thread
object only to move it
– Ryan Haining
Nov 15 '18 at 23:58
add a comment |
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
1
This is the right explanation of the error, but OP would probably be better off usingemplace_back
as int.emplace_back( workThreadProcess(); );
rather than creating astd::thread
object only to move it
– Ryan Haining
Nov 15 '18 at 23:58
add a comment |
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
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
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 usingemplace_back
as int.emplace_back( workThreadProcess(); );
rather than creating astd::thread
object only to move it
– Ryan Haining
Nov 15 '18 at 23:58
add a comment |
1
This is the right explanation of the error, but OP would probably be better off usingemplace_back
as int.emplace_back( workThreadProcess(); );
rather than creating astd::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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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