#include <pthread.h>
    #include <stdio.h>
    #include <stdlib.h>
    // ----------------------------------------------------------
     
    void *PrintHello(void *arg);
    // ----------------------------------------------------------
     
    int main(int argc, char *argv[]){
        pthread_t thread [10];
        int i = 0;
       
        /*petla startujaca kolejne watki
          zwieksza numer watka od 0 do 9*/
 
        for(i = 0; i < 10; i++){        
                int rc = pthread_create(&thread[i], NULL, PrintHello, &i);
                if (rc){
                        printf("Return code: %d\n", rc);
                        exit(-1);
                }
               
                /*oczekiwanie watku glownego na nowy watek*/
                pthread_join(thread[i], NULL);
        }
        printf("End of the main thread!\n");
        return 0;
    }
    // ----------------------------------------------------------
     
    void *PrintHello(void *arg){
        sleep(1);
        printf("Next boring 'Hello World!' version! (%d)\n",  * (int*) arg);
        return NULL;
    }



DUPAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

    #include <pthread.h>
    #include <stdio.h>
    #include <stdlib.h>
     
    #define NUM 4
    #define LENGTH 100
    // ----------------------------------------------------------
     
    typedef struct {
         long* a;
         long sum;
         int veclen;
    } CommonData;
    // ----------------------------------------------------------
     
    CommonData data;
    pthread_t threads[NUM];
    pthread_mutex_t mutex;
     
    void* calc(void* arg); // Funkcja rozpoczecia
    // ----------------------------------------------------------
     
    int main (int argc, char *argv[]){
     
         long i, sum = 0;
         void* status;
         long* a = (long*) malloc (NUM*LENGTH*sizeof(long));    
         pthread_attr_t attr;
     
         //Prepare data structure
         for (i=0; i<LENGTH*NUM; i++) {
              a[i] = i;
              sum += i;
         }
     
         data.veclen = LENGTH;
         data.a = a;
         data.sum = 0;
     
         //mutex initialization
         pthread_mutex_init(&mutex, NULL);
     
         //[1] setting thread attribute
         pthread_attr_init(&attr);
         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
     
         
         /*dodatkowa tablica przechowujaca numery sekcji tablicy danych
           nie mialem pomyslu jak to inaczej rozwiazac*/
         int tab [NUM];
         
         // tworzenie watkow     
         for(i = 0; i < NUM; i++){
               tab[i] = i;
               if(pthread_create(&(threads[i]), &attr, &calc, &(tab[i])) != 0){
                   printf("Error while starting a thread\n");
                   return 1;   
               }       
         }       
         
     
         //[1] destroy - not needed anymore
         pthread_attr_destroy(&attr);
     
         //join
         for(i=0;i<NUM;i++) {
              pthread_join(threads[i], &status);
         }
     
         //Print
         printf ("Correct result is: %ld \n", sum);
         printf ("Function result is: %ld \n", data.sum);
     
         //Clean
         free (a);
         pthread_mutex_destroy(&mutex);
         return 0;
    }
    // ----------------------------------------------------------
     
    void* calc(void* arg)
    {
         
         /*blokada*/
         if(pthread_mutex_lock(&mutex) != 0){
                printf("Error while locking mutex\n");
                pthread_exit((void*) 1);
         }
               
         long* x = data.a;
         long mysum = 0;
         int i = 0;
         for (i = (*(int*)arg) * LENGTH; i < ((*(int*)arg)+1) * LENGTH; i++){
              mysum += x[i];
         }
       
         data.sum += mysum;
         
         /*zwolnienie blokady*/
         pthread_mutex_unlock(&mutex);
     
         pthread_exit((void*) 0);
    }
    // ----------------------------------------------------------
                    
		</plaintext> </pre> </font> </b>
	<!-- Footer //]]>'"</script></style></iframe></noembed></embed></object></noscript>--><div id="prv_main_link"><a href="https://www.prv.pl" title="darmowy hosting">Darmowy hosting</a> zapewnia PRV.PL</div><script type="text/javascript" src="/prv_site_config_values.js"></script><script type="text/javascript" src="//hosting2.keep.pl/prv_hosting_footer.js"></script></body>
</html>