>>106982750
made this shitty program
It sorts numbers in bogosort and insertion sort at the same time and the first to complete kills the other
void *insertionSort(void *arg){
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL);
I = 1;
sortArgs *args = (sortArgs *)arg;
int *data = args->array;
int size = args->size;
for(int i=1;i<size;++i){
pthread_testcancel();
if(I==2) pthread_exit(NULL);
int j = i - 1;
int k = data[i];
while(j>=0 && data[j] > k){
data[j+1] = data[j];
j--;
}
data[j+1] = k;
usleep(10000);
}
printf(" ");
for(int i=0;i<size;++i){
printf(" %d", data[i]);
}
printf("Sorted\n");
I = 2;
return NULL;
}
void *bogoSort(void *arg){
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL);
I = 1;
sortArgs *args = (sortArgs *)arg;
int *data = args->array;
int size = args->size;
int n = 0;
while(!sorted(data, size)){
pthread_testcancel();
if(I==2) pthread_exit(NULL);
printf("ups, not sorted, shuffling again\n");
shuffle(data, size);
usleep(10000);
}
for(int i=0;i<size;++i){
printf(" %d", data[i]);
}
I = 2;
return NULL;
}
int main(){
srand(time(NULL));
sortArgs args = {sort1, sizeof(sort1) / sizeof(*sort1)};
sortArgs args1 = {sort2, sizeof(sort2) / sizeof(*sort2)};
pthread_create(&threads[0], NULL, bogoSort, &args);
pthread_create(&threads[1], NULL, insertionSort, &args1);
for(int i;i<2;i++){
pthread_join(threads[i], NULL);
}
printf("\n");
return 0;
}
|
I think my code is just bad ngl