using std::async appear error: "No viable overloaded '='" - c++

#include <future>
#include <thread>
#include <iostream>
#include <mutex>
void f(int n){
for(int i=0; i<n; i++){
printf("Thread[%d]: %d\n",std::this_thread::get_id(), i);
}
}
int main(int argc, const char * argv[]) {
int thCNT = 5;
std::future<int> mTH[thCNT];
for(int i=0; i<thCNT; i++){
mTH[i] = std::async(f, i+5);
}
for(int i=0; i<thCNT; i++){
mTH[i].get();
}
return 0;
}
code like above
xcode show an error like this, I dont know how to solve it.
I copy the code from an instruction, I hope to know if there is anything different between C++20 and C++11, since the instruction is made by C++11.

273K is right.
I should return int when use future like below:
int f(int n){
for(int i=0; i<n; i++){
printf("Thread[%d]: %d\n",std::this_thread::get_id(), i);
}
return 0;
}

Related

Different results in CodeBlocks and Visual Studio 2019

I used CodeBlocks to code this program:
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
int main()
{
ifstream is;
is.open("game.inp");
int n,a[100];
is>>n;
for (int i=0; i<n; i++)
is>>a[i];
is.close();
int game[100];
int kt=0;
for (int i=0; i<n; i++)
{
for (int j=3; j<a[i]+1; j++)
{
if ((a[i]%j)==0)
{
int *x = find(begin(game),end(game),j); //ktra uoc hien tai da co trong mang hay chua, k co thi ms them
if (x==end(game))
{
game[kt]=j;
kt++;
}
}
}
}
int kq=0;
for (int i=0; i<kt; i++)
{
int d=0;
for (int j=0; j<n; j++)
{
if ((a[j]%game[i])==0)
d++;
}
if (d>kq)
kq=d;
}
ofstream o;
o.open("game.out");
o<<kq;
o.close();
return 0;
}
and the result was not correct. Then I decided to copy these codes to Visual Studio 2019 and it gave me the correct result. I don't know what happened. I copied the same codes from CodeBlocks to VS and the results were completely different.
Welcome to the wonderful world of undefined behavior.
You use the contents of the array game before it's initialized. Local variables are not initialized, their contents is indeterminate and using indeterminate values lead to undefined behavior.
If you want the array to be initialized to all zeroes you need to do it explicitly:
int game[100] = { 0 };

Creating a vector of threads [duplicate]

i'm new in c++ programing and need some help to use a thread library with vector library...
first I follow this tutorial
but the compiler (visual studio 2013) show me errors and I don't know how correct it:
first declaration of function
void Fractal::calcIterThread(vector<vector<iterc>> &matriz, int desdePos, int hastaPos, int idThread){
...
}
in main loop
vector<vector<iterc>> res;
res.resize(altoPantalla);
for (int i = 0; i < altoPantalla; i++){
res[i].resize(anchoPantalla);
}
int numThreads = 10;
vector<thread> workers(numThreads);
for (int i = 0; i < numThreads; i++){ //here diferent try
thread workers[i] (calcIterThread, ref(res), inicio, fin, i)); // error: expresion must have a constant value
workers[i] = thread(calcIterThread, ref(res), inicio, fin, i)); // error: no instance of constructor "std::thread::thread" matches the argument list
}
...rest of code...
thanks for any help to clarify
Try this:
#include <functional>
#include <thread>
#include <vector>
// ...
int numThreads = 10;
std::vector<std::thread> workers;
for (int i = 0; i != numThreads; ++i)
{
workers.emplace_back(calcIterThread, std::ref(res), inicia, fin, i);
}
for (auto & t : workers)
{
t.join();
}
finally I can solve the problem with this change in my code...
workers.emplace_back(thread{ [&]() {
calcIterThread(ref(res), inicio, fin, i);
}});

Strange C++ matrix multiplication/initialization

I'm using Cygwin, Windows Vista, Norton anti-virus (someone asked me about this before). I previously asked a question about strange C++ behavior that no one could answer. Here's another. Simple matrix multiplication exercise. This form below gives strange (and wrong) results:
#include <iostream>
#include <string>
#include<cmath>
using namespace std;
int main(int argc, char* argv[])
{
int A[3][3]={{1,5,0},{7,1,2},{0,0,1}};
int B[3][3]={{-2,0,1},{1,0,0},{4,1,0}};
int D[3][3];
for (int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
for (int k=0;k<3;k++)
{
D[i][j]+=A[i][k]*B[k][j];
}
}
}
for (int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
cout<<D[i][j]<<"\n";
}}
return 0;
}
BUT a very small change gives correct results: (all I've done is move the initialized matrices outside main() ).
#include <iostream>
#include <string>
#include<cmath>
using namespace std;
int A[3][3]={{1,5,0},{7,1,2},{0,0,1}};
int B[3][3]={{-2,0,1},{1,0,0},{4,1,0}};
int D[3][3];
int main(int argc, char* argv[])
{
for (int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
for (int k=0;k<3;k++)
{
D[i][j]+=A[i][k]*B[k][j];
}
}
}
for (int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
cout<<D[i][j]<<"\n";
}}
return 0;
}
You forgot to initialize the array D to 0 in your first case. This is automatically done when the array is global, but not when it is local (simplified, but explains the behavior).

how to declare a vector of thread

i'm new in c++ programing and need some help to use a thread library with vector library...
first I follow this tutorial
but the compiler (visual studio 2013) show me errors and I don't know how correct it:
first declaration of function
void Fractal::calcIterThread(vector<vector<iterc>> &matriz, int desdePos, int hastaPos, int idThread){
...
}
in main loop
vector<vector<iterc>> res;
res.resize(altoPantalla);
for (int i = 0; i < altoPantalla; i++){
res[i].resize(anchoPantalla);
}
int numThreads = 10;
vector<thread> workers(numThreads);
for (int i = 0; i < numThreads; i++){ //here diferent try
thread workers[i] (calcIterThread, ref(res), inicio, fin, i)); // error: expresion must have a constant value
workers[i] = thread(calcIterThread, ref(res), inicio, fin, i)); // error: no instance of constructor "std::thread::thread" matches the argument list
}
...rest of code...
thanks for any help to clarify
Try this:
#include <functional>
#include <thread>
#include <vector>
// ...
int numThreads = 10;
std::vector<std::thread> workers;
for (int i = 0; i != numThreads; ++i)
{
workers.emplace_back(calcIterThread, std::ref(res), inicia, fin, i);
}
for (auto & t : workers)
{
t.join();
}
finally I can solve the problem with this change in my code...
workers.emplace_back(thread{ [&]() {
calcIterThread(ref(res), inicio, fin, i);
}});

How to pass two-dimensional array to the function so that code would compile

I'm trying to make a program which fill a quadratic matrix with some random values. So here the source code (which works fine):
#include <iostream>
#include <stdlib.h>
#include <iomanip>
using namespace std;
int main()
{
int const n = 5;
int matrix[n][n];
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
matrix[i][j] = rand()%10; // fill matrix
cout<<setw(2)<<matrix[i][j]; // matrix output
}
cout<<"\n"; // new line
}
}
But now I want to rewrite this code using my own function:
#include <iostream>
#include <stdlib.h>
#include <iomanip>
using namespace std;
void fillAndPrintArray (int **matrix, int n); // function prototype
int main()
{
int const n = 5;
int matrix[n][n];
fillAndPrintArray(matrix, n);
}
void fillAndPrintArray(int **matrix, int n)
{
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
matrix[i][j] = rand()%10; // fill matrix
cout<<setw(2)<<matrix[i][j];
}
cout<<"\n"; // new line
}
}
But I can't compile this code. I'm getting error:
||In function 'int main()':error: cannot convert 'int (*)[5]' to 'int**' for argument '1' to 'void fillAndPrintArray(int**, int)'.
I don't know how to fix this.
The problem is that you are passing a multidimensional array to a function that takes a pointer to a pointer. In regards to arrays this is a pointer to the first element of an array of pointers, not a two dimensional array.
If you want to pass multidimensional array to a function you can use a template function and take the array by reference.
template<std::size_t U, std::size_t V>
void func(const int (&arr)[U][V])
{
// do stuff
}
int main()
{
int arr1[10][10];
int arr2[15][10];
func(arr1);
func(arr2);
}
To do this without a template function just replace U and V with the desired dimensions. for instance to pass a 5x5 array as in your question you would do this.
void func(const int(&arr)[5][5])
{
}