All pastes #818511 Raw Edit

Something

public text v1 · immutable
·published 2007-12-16 10:21 UTC
rendered paste body
#include <iostream>
#include <windows.h>
#include <pthread.h>
#include <semaphore.h>

using namespace std;

pthread_mutex_t muteks;
sem_t my_sem;
unsigned int my_sem_cntr=1;    
///////////////////////
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 );
};
};

void *watek_semafor (void * arg) {
int x ;

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

	
//cout << "\nsemafor\n";
sem_wait( &my_sem );
//cout << "\nsemafor zamkniety!\n";
semafor.Insert(x);
sem_post( &my_sem );	

};
};



int main()
{
     pthread_mutex_init( &muteks, NULL);  //inicjalizacja muteksu
     srand((unsigned)time(NULL));
   
     pthread_t watek[4];
  
     
     
double  czas1 = getTime() ;

        for(int i=0;i<4;i++)
                pthread_create( &watek[i], NULL, watek_sekcja, NULL);
        for(int i=0;i<4;i++)
                    pthread_join(watek[i], NULL );

czas1 = getTime() - czas1;
cout << endl << "Sekcja: " << czas1 ;
///////////#########################SEMAFOR  
		 /* "my_sem is a semaphore." */

  sem_init( &my_sem,0,my_sem_cntr );


czas1 = getTime() ;

        for(int i=0;i<4;i++)
                pthread_create( &watek[i], NULL, watek_semafor, NULL);
        for(int i=0;i<4;i++)
                    pthread_join(watek[i], NULL );

czas1 = getTime() - czas1;
cout << endl << "Semafor: " << czas1 << endl;
     system("PAUSE"); 
     
};