Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <pthread.h>
- #include <errno.h>
- #include <stdlib.h>
- void *square(void *x) //funkcja obliczająca wartość kwadratu
- {
- int threadRetValue = *((int*) x) * *((int*) x);
- return (void*)threadRetValue;
- }
- int main(int argc, char** argv)
- {
- pthread_t thread[argc]; // deklaracja wątków
- void *readThread; // przechowuje wskaźnik na adres
- int EndRes = 0; // deklaracja zmiennej przechowującej wynik końcowy
- int tab[argc]; // deklaracja tablicy w celu odczytania wartosci na wejsciu standardowym
- int i;
- for(i=0 ; i<argc ; i++) // petla w ktorej odbywa sie przypisywanie argumentow do tablicy
- {
- tab[i] = atoi(argv[i]);
- }
- for( i=1; i<argc ; i++){ // stworzenie odpowiedniej liczby watkow, tyle watkow ile jest argumentow
- if(pthread_create(&thread[i], NULL, &square, &tab[i])) //stworzenie wątków i przypisanie im funkcji którą mają wykonać (square)
- {
- printf("Error when starting a thread."); //Komunikat wyświetlany w wypadku niepowodzenia przy tworzeniu wątku
- };
- }
- int w; // to nie wiem czy potrzebne
- for( i=1; i<argc ; i++){
- if(pthread_join(thread[i], &readThread)) //czekanie na zakończenie wątku
- {
- printf("Error when ending a thread."); //Komunikat wyświetlany w wypadku niepowodzenia przy kończeniu pracy wątku
- };
- EndRes += (int)readThread; // obliczanie sumy końcowej
- printf("%d:%ld:%d:%s:%d\n",i, thread[i] ,getpid(), argv[i], (int)readThread);
- }
- printf("%s:%ld:%d:%d\n","thread_glowny", pthread_self() ,getpid(), EndRes); // wyświetlanie informacji i sumy końcowej
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement