Take multiple arguments to the thread function c++11 - c++

I have an array of threads and in for loop I should create a thread to all of them.
The problem is that one of the parameters is std::move(promise_var) and another one is structure. When I try to compile it compile gives me an error:
error: no matching function for call to ‘std::thread::thread(void (&)(Function), Structure [nNumThreads], std::remove_reference<std::promise<const char*>&>::type)’
So, here is the simplified version of code...
func(struct Structure, std::promise<const char *> && v_Promise){
//doing work
}
main(){
std::thread a_Threads[5];
for(int8_t i = 0; i < 5; i++){
a_Threads[i] = std::threads(func, Structure, std::move(v_promise[i]));
}
}

(Not putting in a comment so that I can append the code properly) I completed the code you submitted so that it compiles and I can confirm that the code below compiles and runs fine with the command g++ test.cpp -std=c++11 -pthread:
#include <iostream>
#include <thread>
#include <future>
using namespace std;
struct Structure{
int el1;
int el2;
};
void func(struct Structure, std::promise<const char *> && v_Promise){
cout<<"Hello"<<endl;
//doing work
}
int main(){
std::thread a_Threads[5];
Structure my_struct;
std::promise<const char*> v_promise;
for(int8_t i = 0; i < 5; i++){
a_Threads[i] = std::thread(func, my_struct, std::move(v_promise));
}
}
Please cross-reference this against any typos or information that you might have omitted.

Related

Receiving error when using push_back with a struct data type c++

I have the following code, where I am generating a deck of cards.
First, I determine the suit with the function convertIntToSuit, which then goes into the next loop to populate the Card deck. But I keep getting the following error:
Running /home/ubuntu/workspace/A5/Assignment5/main.cpp
make: Entering directory `/home/ubuntu/workspace/A5/Assignment5'
Compiling main.o
g++ -Wall -g -O3 -std=c++11 -I/usr/include -c -o main.o main.cpp
main.cpp: In function ‘int main()’:
main.cpp:39:33: error: no matching function for call to ‘std::vector<Card>::push_back(<brace-enclosed initializer list>)’
deck.push_back({i,s});
Here is my code:
// The include section adds extra definitions from the C++ standard library.
#include <iostream> // For cin, cout, etc.
#include <iomanip> // For text formatting (setprecision, setw, etc.)
#include <cmath> // For math functions (sin, cos, pow, etc.)
#include <cstdlib>
#include <vector>
#include <string>
#include"convertIntToSuit.h"
#include"printCard.h"
//#include"shuffleDeck.h"
using namespace std;
struct Card {
string suit;
int rank;
};
int main() {
vector<Card> deck;
for(int j = 0; j<4; ++j){
string s = convertIntToSuit(j);
for(int i=1; i<=13; ++i){
deck.push_back({i,s});
}
}
return 0;
}
The push_back function does not understand what you are sending to it. Enclosing the two properties of Card in curly brackets is not enough. You must construct the Card struct before sending it to push_back.
There are 2 options here:
Either to construct Card object explicitly: deck.push_back(Card{s,i});
Construct card object in-place inside vector. That could eliminate unneeded object copies though in this case the benefit is negligible. But for that you need to define an explicit c'tor that takes both arguments.
c++11 does not allow implicit construction of objects with just arguments.
Once you have the c'tor - you can use emplace_back member function which does exactly what you want: it takes the arguments to the constructor and constructs the object in-place with those arguments. In your case:
struct Card {
string suit;
int rank;
Card(string s = string(), int r = 0) : suit(std::move(s)), rank(r) {}
};
for (int i=1; i<=13; ++i){
deck.emplace_back(i,s);
}
Please note that you do not need curly brackets when using emplace_back.
Do it like this for every iteration:-
Card c1;
c1.suit=s;
c1.rank=i
deck.push_back(c1);
Or
what you are doing will be:-
deck.push_back({s,i});
string will come first before int because of your struct declaration.

std::complex in struct that makes compile slow

I noticed that the following code is extremely slow to compile: (It can't even finish on my computer)
#include <complex>
struct some_big_struct {
std::complex <double> a[1000000][2];
};
some_big_struct a;
int main () {
return 0;
}
Out of curiosity, I've also tried other alternatives of the code. However, these codes seem to compile just fine on my computer :
#include <complex>
struct some_big_struct {
double a[1000000][2];
};
some_big_struct a;
int main () {
return 0;
}
and
#include <complex>
std::complex <double> a[1000000][2];
int main () {
return 0;
}
I wonder if anyone can share some insight on why such is the case. Thanks!
The compiler is probably running the default std::complex constructor while compiling, so that it can put the initialized values of all the array members into the executable, rather than generate code that performs this loop when the program starts. So it's calling the constructor 2 million times while compiling.

Code compiles in Eclipse but not with g++ on Linux

#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
int main ()
{
struct process
{
int burst;
int ar;
};
vector<process> a;
// insert two processes
a.push_back({21, 42});
a.push_back({10, 20});
queue <process> names;
names.push(a[1]);
names.push(a[2]);
cout<<names.front().ar;
return 0;
}
The above code is working fine in eclipse but when i compile it on linux it give many errors. invalid argument and many more. i'm executing it with command:
g++ -o file_name file_name.cpp
does anyone know the reason behind these errors? any solution?
You need to move the definition of struct process out of the main function:
struct process
{
int burst;
int ar;
};
int main ()
{
...
}
Additionally, since you use initializer lists you must enable C++11 suport in GCC:
g++ -std=c++11 -o file_name file_name.cpp
You can not define a struct within a function. In your case, the struct process is defined within the main function.
Also, you need to declare the type of struct you wish to append to the vector.
the '{21, 42}' does not declare a 'process' type (Unless you use C++11).
Using local classes as template arguments and passing braced-init-list to functions are only possible as of C++11. Pass -std=c++11 or -std=c++14 option to g++ and all the errors will disappear. Note that all the letters in the option are in lower case.

pushing structure variable in queue

#include <iostream>
#include <queue>
using namespace std;
int main () {
struct process {
int burst;
int ar;
};
int x=4;
process a[x];
queue <string> names; /* Declare a queue */
names.push(a[1]);
return 0;
}
I'm trying to pushing struct variable in queue but its not taking it and gives errors
no matching function for #include queue and invalid argument
how can I do that?
C++ is a strongly typed language. In the line names.push(a[1]); you are trying to push a struct (from your process a[x]; array) into a queue<string>. Your struct is not a string, so the compiler will emit an error. You at least need a queue<process>.
Other issues: variable length arrays are not standard C++ (process a[x];). Use a std::vector<process> instead. Here is some simple example that works:
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
int main () {
struct process // move this outside of main() if you don't compile with C++11 support
{
int burst;
int ar;
};
vector<process> a;
// insert two processes
a.push_back({21, 42});
a.push_back({10, 20});
queue <process> names; /* Declare a queue */
names.push(a[1]); // now we can push the second element, same type
return 0; // no need for this, really
}
EDIT
Locally defined classes/structs used to instantiate templates are valid only in C++11 and later, see e.g. Why can I define structures and classes within a function in C++? and the answers within. If you don't have access to a C++11 compliant compiler, then move your struct definition outside of main().

Error compiling with <list> C++

I don't know why it doesn't compile of I erase the comment in line
/*******************************/
waitThread.push_front(workerID);
/******************************/
Only if I leave the comment, it compiles...otherwise, I get a long exception ending with "declared here"...
/usr/include/c++/4.6/thread:126:5: error: declared here
maybe there is some problem with the definition of ...
Can you explain me?
/* g++ -std=c++0x -o manyThreads manyThreads.cpp -pthread */
#include <thread>
#include <iostream>
#include <mutex>
#include <time.h>
#include <list>
std::list<std::thread::id> myList;
std::mutex mutex;
std::list<std::thread> waitThread;
void insertList(std::thread::id identifier) {
mutex.lock();
myList.push_front(identifier);
mutex.unlock();
}
int main() {
std::list<std::thread::id>::iterator id;
std::list<std::thread>::iterator threadsIter;
int counter;
for(counter=0; counter<6; counter++) {
std::thread workerID(insertList, workerID.get_id());
/*******************************/
waitThread.push_front(workerID);
/******************************/
}
for(threadsIter=waitThread.begin(); threadsIter !=waitThread.end();threadsIter++) {
threadsIter->join();
}
for(id=myList.begin(); id != myList.end(); id++) {
std::cout << *id << "\n";
}
return 0;
}
std::thread is not copyable so you can't call push_front with it. It makes no sense to copy a thread, what would it do?
You can perhaps move the thread onto the list using
waitThread.push_front(std::move(workerID));
which will of course invalidate the thread object after that line.
However this line looks strange too :-
std::thread workerID(insertList, workerID.get_id());
I doubt it's valid to call get_id on an object that isn't constructed at that point.
std::thread is not copyable so you would have to move it in:
waitThread.push_front(std::move(workerID));
alternatively, you can move it by passing a temporary:
waitThread.push_front(std::thread(insertList, workerID.get_id());
It's not a comment, but a valid and (probably) essential statement in your program:
/*******************************/ -- comment
waitThread.push_front(workerID); -- statement
/******************************/ --comment