-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
61 lines (53 loc) · 1.23 KB
/
main.cpp
File metadata and controls
61 lines (53 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
* main.cpp
*
* Created on: 25 oct. 2019
* Author: antonio
*/
#include <thread>
#include <iostream>
#include <ctime>
#include <cstdlib>
class Hilo{
int n;
int tiempo;
public:
Hilo(int n=5,int tiempo=1):n(n),tiempo(tiempo){}
void operator()(){
for(int i=0;i<this->n;i++){
std::cout << " Operator() mensaje "<< (i+1) << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(this->tiempo));
}
}
~Hilo(){}
};
void generarAleatorios (int n,int limite, int mili,int &suma){
int num;
suma=0;
for(int i=0;i<n;i++){
num=rand()%limite;
suma +=num;
std::cout << num << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(mili));
}
}
int main(){
int it=12;
int ml=150;
int suma;
std::srand(time(0));
Hilo obj(10,2);
std::thread hiloFuncion(generarAleatorios,10,250,500,std::ref(suma));
//std::thread hiloFuncion(generarAleatorios,10,250,500,suma); //ERROR
std::thread hiloObjeto (obj);
std::thread hiloLambda([it,ml](){
for(int i=0;i<it;i++){
std::cout << "Mensaje lambda: "<< (i+1)<< std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(ml));
}
});
hiloFuncion.join();
hiloObjeto.join();
hiloLambda.join();
std::cout << "La suma de aleatorios es: "<< suma << std::endl;
}