I need to run two functions at the same time, read temperature and open Menu.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
int main() {
setlocale(LC_ALL, "Portuguese");
int n = 2, operacao; //Porta serial inicial e Valor do Menu;
float qt_leituras, b, intervalo = 2;
ListaEncadeada<Estado> lista;
Interface _interface(n);
c_clrscr();
_interface.mostraTemperatura(intervalo);
// thread first{mostraTemperatura, intervalo};//Execução das threads
// first.join();
while (true) { //Menu e Programa Principal
//There are any cases here, but not important in the question
}
return 0;
}
while (true) { //Menu e Programa Principal
c_textcolor(BLACK);
c_textbackground(WHITE);
cout << "\t\t\tMenu Principal" << endl;
cout << "\t0\tLer a temperatura e armazenar na lista encadeada" << endl;
cout << "\t1\tSalvar dados em txt" << endl;
cout << "\t2\tAjustar intervalo de exibição da temperatura" << endl;
cout << "\t3\tAjustar limiar de temperatura" << endl;
cout << "\t4\tLeituras no formato datalog" << endl;
cout << "\t5\tExportar os dados para o Excel" << endl;
cout << "\t6\tLimpar todos os dados" << endl;
cout << "\t7\tLevantamento de ocorrências de uma data" << endl;
cout << "\t8\tCálculo de valores estatísticos" << endl;
cout << "\t9\tExportar dados no formato de gráfico" << endl;
cout << "\t10\tEscolher a porta serial da comunicação" << endl;
cout << "\t11\tSair" << endl;
cout << "Digite uma opção do menu: " << endl;
cin >> operacao;
switch (operacao) {
case 0: {
c_clrscr();
Estado _estado = _interface.getEstado();
lista.insereF(_estado);
break;
}
case 1: {
c_clrscr();
SalvaDados(lista);
break;
}
case 2: {
c_clrscr();
cout
<< "Entre o intervalo de tempo em que a temperatura é mostrada: "
<< endl;
cin >> intervalo;
break;
}
case 3: {
c_clrscr();
_interface.setLimites();
_interface.Controle();
break;
}
case 4: {
c_clrscr();
cout << "Digite o número de leituras desejado:";
cin >> qt_leituras;
cout << endl << "Escolha o tempo entre cada leitura: ";
cin >> b;
// Datalog g(qt_leituras, b, &s);
break;
}
case 5: {
c_clrscr();
EscreveDadosExcel(lista);
break;
}
case 6: {
c_clrscr();
LimpaExcel(lista);
break;
}
// case 7:
// c_clrscr();
//break;
// case 8:
// c_clrscr();
//break;
// case 9:
// c_clrscr();
//break;
case 10: {
c_clrscr();
cout << "Digite o número da porta serial: " << endl;
cin >> n;
_interface.setPorta(n);
break;
}
case 11: {
c_clrscr();
SalvaDados(lista);
exit(0);
break;
}
c_clrscr();
cout << "\nAperte qualquer tecla, para voltar ao menu" << endl;
}
}
return 0;
}
the problem I have with your code is that it isn't in english, try to write code always in english primarly because of other people.
now to the problem, the problem I see is that you have to give std::async an object instance because mostraTemperatura is a class non static member function.
normaly you would do this with std::bind
for example: std::async(std::bind(&_interface::mostraTemperatura, this, intervalo);
this will work only if you call it "inside" the object.
otherwise supply object instance for example:
The errors now is:
invalid use of incomplete type 'class std::future<void>'
"'mutex' was not declared in this scope";
It's hard to tell what you did wrong without seeing the code, here are only suggestions.
note that std::async returns std::future object.
that means to retrieve the result of a thread (if you return a result) you need something like this:
1 2
auto ret = async(launch::async, &Interface::mostraTemperatura,_interface, intervalo);
auto result = ret.get(); // to get the result of a thread if you need it.
your std::mutex variable need to be declared inside the class probably, or somewhere where all the threads can access it, that means it should be visible to all the threads that use it.
It all depends on how and where you use the result of async operation. in either case return value is std::future which has member get() to get the value, which blocks until thread is done.
also try not to forget including std headers where needed, ie. in your own headers.
maybe if you share more complete example code someone could give you more info.