All pastes #817632 Raw Edit

Someone

public text v1 · immutable
·published 2007-12-15 19:20 UTC
rendered paste body
#include <iostream>
#include <windows.h>
#include <pthread.h>
using namespace std;

pthread_mutex_t muteks;
///////////////////////
struct element {
       int wartosc ;
       element *nastepny ;
       
element(int a=0,element * b=NULL) 
       {
       wartosc=a;            
       nastepny=b;
       } 
       };
/////////////////////////////
class lista {
   
public:
lista()
{
 inf.glowa=NULL ;     
};

void Insert(int x);
void Printlist();
 
struct info
{
   element *glowa ;
};      
info inf ;      



} sekcja, semafor;

/////////////////////////////////////////////
void lista::Insert(int x){

if(inf.glowa==NULL) {
                    inf.glowa=new element(x,NULL);
                    
                    return;
                    }
else {
 element * wskaznik;
 wskaznik=inf.glowa ;
    if(wskaznik->wartosc > x) {
                            inf.glowa=new element(x,wskaznik);
                            }
    else {
       while(wskaznik->nastepny!=NULL && wskaznik->nastepny->wartosc < x)
        {
        wskaznik=wskaznik->nastepny ;         
        }
        wskaznik->nastepny=new element(x,wskaznik->nastepny);
    };
};
};
////////////////////////////////////////////
void lista::Printlist() {
     element *wskaznik ;
     if(inf.glowa!=NULL){
     wskaznik=inf.glowa;
     while(wskaznik->nastepny!=NULL)
     {
       cout << wskaznik->wartosc << ", ";
       wskaznik = wskaznik->nastepny ;                               
     }
          cout << wskaznik->wartosc << endl ;
     }
     else cout << "Lista jest pusta" << endl ; 
};


long double getTime() {
  LARGE_INTEGER pc, freq;
  pc.QuadPart = 0;
  freq.QuadPart = 0;
  QueryPerformanceCounter(&pc);
  QueryPerformanceFrequency(&freq);
  if(freq.QuadPart == 0)
     return -1;
  return (long double)pc.QuadPart/(long double)freq.QuadPart;
} ;

void *watek_sekcja (void * arg) {
int x ;
for (int i=0;i<5000;i++) {
x=(int) rand() % 1000 ;

pthread_mutex_lock( &muteks );
sekcja.Insert(x);
pthread_mutex_unlock( &muteks );
};
return NULL;
};

int main()
{
     pthread_mutex_init( &muteks, NULL);  //inicjalizacja muteksu
     srand((unsigned)time(NULL));
   
     pthread_t watek_1;
     pthread_t watek_2;
     
double  czas1 = getTime() ;

pthread_create( &watek_1, NULL, watek_sekcja, NULL);
pthread_create( &watek_2, NULL, watek_sekcja, NULL);    
    pthread_join(watek_1, NULL );
    pthread_join(watek_2, NULL );
czas1 = getTime() - czas1;
    
 //test.Printlist();
 cout << endl << czas1 ;
     system("PAUSE"); 
     
};