Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <mutex>
- #include <iostream>
- class Pila_Enteros {
- private:
- int arr[1000];
- size_t cantidad_elementos;
- std::mutex m;
- public:
- Pila_Enteros(){
- for (size_t i = 0; i < 1000; i++){
- arr[i] = 0;
- }
- cantidad_elementos = 0;
- }
- void push(int valor){
- m.lock();
- if (cantidad_elementos < 1000) {
- arr[cantidad_elementos] = valor;
- cantidad_elementos++;
- }
- m.unlock();
- }
- int pop(){
- m.lock();
- if (cantidad_elementos == 0) {
- m.unlock();
- throw "Pila vacía";
- }
- int temp = arr[cantidad_elementos - 1];
- --cantidad_elementos;
- m.unlock();
- return temp;
- }
- size_t obtener_cantidad_elementos() {
- return cantidad_elementos;
- }
- };
- int main(){
- Pila_Enteros p;
- p.push(10);
- p.push(30);
- std::cout << p.pop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement