Datos personales

domingo, 24 de noviembre de 2013

C++ Threads(Hilos)

C++ Thread

Ejemplo de como implementar threads con la libreria thread en c++

El programa debe calcular el area de circulos y triangulos por separado.
/* Version g++ 4.6.3 - Linux/Ubuntu 12.04
 * Compilacion g++ qareas.cpp -o qarea -std=c++0x -pthread
 */
#include <iostream> 
#include <stdlib.h>
#include <thread>
#define CIRCLES 10
#define TRIANGLES 10
using namespace std;

class Circle
{
  public:
    Circle(int r) : radio(r) {}
    ~Circle() {}

    void circleArea(){ area = 3.14159 * (radio*radio); }
    void showArea(){ cout<<"C -"<<radio<<" - "<<area<<endl; }

  private:
    int radio;
    double area;
};

class Triangle
{
  public:
    Triangle(int b, int h) : base(b), high(h) {}
    ~Triangle(){  }

    void triangleArea(){ area = ( base * high) / 2; }
    void showArea(){ cout<<"T -"<<base<<" - "<<high<<" - "<<area; }
  private:
    int base, high;
    double area;
};

void TrianglesArea(){
  int i;
  srand (time(NULL));
  for(i = 0; i < TRIANGLES; i++){
    Triangle* t = new Triangle(rand()%10+1,rand()%15+10);
    t->triangleArea();
    t->showArea();
    delete t;
    sleep(1);
  }
}

void CirclesArea(){
  int i;
  srand (time(NULL));
  for(i = 0; i < CIRCLES; i++){
    Circle* c = new Circle(rand()%10+1);
    c->circleArea();
    c->showArea();
    delete c;
    sleep(1);
  }
}
int main(void){

  cout<<"Id Area"<<endl;
  thread circles(CirclesArea);
  thread triangles(TrianglesArea);

  circles.join();
  triangles.join();

  return 0;
}

Ruby Threads(Hilos) script

Threads Ruby

Simple script en ruby, que muestra el manejo de threads(hilos)

El programa debe calcular el area de circulos y triangulos por separado.

Mayor informacion Ruby Multithreading

class Triangle
  attr_accessor :attributes

  def initialize
    @attributes = {
      base:   Random.rand(1..10),
      high: Random.rand(10..15)
    }
  end

end

class Circle
  attr_accessor :attributes

  def initialize
    @attributes = {
      radio: Random.rand(1..10)
    }
  end
end

class QAreas

  @triangles = []
  @circles = []

  def self.generate_triangles
    10.times do |id|
      t = Triangle.new
      @triangles << [id, t.attributes[:base], t.attributes[:high] ]
    end
  end

  def self.triangle_area(b,h)
    return (b * h ) / 2
  end

  def self.generate_circles
    10.times do |id|
      c = Circle.new
      @circles << [id,c.attributes[:radio]]
    end
  end

  def self.circle_area r
    return Math::PI * (r*r)
  end

  def self.start
    QAreas::generate_triangles
    QAreas::generate_circles
    print 'Type-id Area'
    puts ''

    th_triangle = Thread.new do 
      @triangles.each do |id, base, high|
        area = QAreas::triangle_area(base,high)
        print 'T -', id, ' - ', base, '-', high,  '-',area
        puts ''
        sleep(0.2)
      end
    end

    th_cirlce = Thread.new do
      @circles.each do |id, r|
        area =  QAreas::circle_area r
        print 'C -', id, ' - ', r
        puts ''
        sleep(0.2)
      end
    end

    th_triangle.join
    th_cirlce.join
  end

end

QAreas.start