WinApi C multithreading: how to wait for a thread to finish? -


i'm writing multithreaded program calculate fibonacci, power , factorial. instead of using sleep, wait threads finish, , i'd display ids of threads in order finish (first finished, first displayed). how should this?

    #include <windows.h>     #include <stdio.h>     #include <stdlib.h>     #include <conio.h>  unsigned int n = 0; int priorytety[3] = { thread_priority_below_normal,thread_priority_normal, thread_priority_above_normal}; handle watki[3];  dword winapi fibbonaci(void *argumenty){     unsigned long long int prevprev = 0;     unsigned long long int prev = 1;     unsigned long long int wynik = 1;     while (wynik <= n){         wynik = prev + prevprev;         prevprev = prev;         prev = wynik;     }     printf("fibbonaci : %llu \n", wynik);     exitthread(wynik);     //return wynik; }  dword winapi potegi(void *argumenty){     unsigned long long int wynik = 2;     while (wynik <= n){         wynik = wynik << 1;     }     printf("potegi : %llu \n", wynik);     return wynik; }  dword winapi silnia(void *argumenty){     //printf("%d", atoi(argv[argc-1]));     unsigned long long int wynik = 1;     unsigned long long int = 1;     while (wynik <= n){         wynik = wynik * i;         = + 1;     }     printf("silnia : %llu \n", wynik);     return wynik; }   int main(){     int i;     dword id;     system("cls");     scanf_s("%d", &n);     lpthread_start_routine winapi funkcje[3] = { fibbonaci, potegi, silnia };     (i = 0; < 3; i++)     {         watki[i] = createthread(             null, // atrybuty bezpieczeństwa             10000, // inicjalna wielkość stosu             funkcje[i] , // funkcja wątku             (void *)n,// dane dla funkcji wątku             0, // flagi utworzenia             &id);         if (watki[i] != invalid_handle_value)         {             //printf("utworzylem watek o identyfikatorze %x\n", id);             // ustawienie priorytetu             setthreadpriority(watki[i], priorytety[1]);         }     }     sleep(10000);     getchar(); } 

@whozcraig correct should use waitformultipleobjects() wait threads finish. read this post more information.

that, however, not tell order in ended, when have completed. adding code each function print thread id should (use getcurrentthreadid()). example:

printf("potegi : %llu, thread id %ld \n", wynik, getcurrentthreadid()); 

now must not forget there time between printf statement , when thread finishes. not doing work there, technically thread still active. in multithreaded environment, cannot predict how time elapse between printf , when thread terminates, no matter how little code appears there.

if difference important you, need join each thread independently , see 1 terminates first. repeatedly call waitforsingleobject() on each thread handle 0 timeout , detect 1 terminates first. yes, there slight race condition if third thread finishes before second while checking on first, , check second thread , notice has terminated. you'll miss fact third finished first. , polling technique alters experiment consuming lot of cpu while waiting.

personally, think better off recording time (based on system clock) when each thread finished computing result, not when thread terminated. use gettickcount() or queryperformancecounter() measure time.


Popular posts from this blog