The program starts to hang after I pass the variable to search. What could be the problem? Thread function

#include <windows.h> #include <stdio.h> #include <stdbool.h> #include <stdlib.h> #include <malloc.h> #include <conio.h> #include <math.h> #include <ctype.h> int *a; int z,n,b1; int dyrak1(); int dyrak2(); int dyrak3(); void SortMass(int*, int); DWORD WINAPI Thread(LPVOID* ptr ); int main(void) { int i,y1; printf("Enter the number of items:\n"); n = dyrak1(); a = (int*) malloc(n*sizeof(int)); printf("Enter item: \n"); for(i=0; i<n; i++) { printf("a[%d] = ",i+1); a[i] = dyrak2(); } printf("You want to sort?\n"); printf("Enter 1 if yes \n"); printf("Enter 0 if not \n"); while(scanf("%d", &y1)!=1||(y1!=1 && y1!=0)) { printf("Error! Enter 1 if yes or 0 if no\n"); fflush(stdin); } if (y1==1) { SortMass(a,n); for(i=0; i<n; i++) { printf("%d ", a[i]); } printf("\n"); } else { for(i=0; i<n; i++) { printf("%d ", a[i]); } printf("\n"); } printf("Input variable 'a' for search: "); z = dyrak3(); DWORD idThread; // дСскриптор ΠΏΠΎΡ‚ΠΎΠΊΠ° HANDLE firstThread; firstThread = CreateThread(NULL // Π½Π΅Ρ‚ Π·Π°Ρ‰ΠΈΡ‚Ρ‹ ΠΏΠΎΡ‚ΠΎΠΊΠ° , 0 // Ρ€Π°Π·ΠΌΠ΅Ρ€ стСка , Thread // эта функция ΠΏΠΎΡ‚ΠΎΠΊΠ° , (LPVOID)NULL // пСрвая ΠΏΠΎΠ»ΠΎΠ²ΠΈΠ½Π° , CREATE_SUSPENDED // ΠΏΠΎΡ‚ΠΎΠΊ Π½Π΅ запускаСтся , &idThread // ΠΈΠ΄Π΅Π½Ρ‚ΠΈΡ„ΠΈΠΊΠ°Ρ‚ΠΎΡ€ ΠΏΠΎΡ‚ΠΎΠΊΠ° ); WaitForSingleObject(firstThread, INFINITE); if (b1 != -1) { printf("The index of the element is %d\n", b1+1); } else printf("The element isn't found!\n"); free(a); getchar();getchar(); return 0; } void SortMass(int* m, int n) { for(int i = n - 1; i >= 1; i--) for(int j = 0; j < i; j++) { if(m[j] > m[j+1]) { int foo = m[j]; m[j] = m[j+1]; m[j+1] = foo; } } } DWORD WINAPI Thread(LPVOID* ptr ) { int low, high, middle; low = 0; high = n - 1; while (low <= high) { middle = (low + high) / 2; if (z < a[middle]) high = middle - 1; else if (z > a[middle]) low = middle + 1; else { b1=middle; return 0; } } b1 = -1; return 0; } 

    1 answer 1

    The stream is created with the CREATE_SUSPENDED attribute, which means it will not start itself. To start, you need to call the ResumeThread function, or remove this flag.