Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <pthread.h>
- #include <time.h>
- typedef struct _SearchParams {
- int target;
- int subArraySize;
- int* list;
- int totalSize;
- } SearchParams;
- int found = 0;
- int currentThread = 0;
- int indx = -1;
- void* search(void* args) {
- SearchParams* paramStruct = (SearchParams*) args;
- int target = paramStruct->target;
- int* list = paramStruct->list;
- int subArraySize = paramStruct->subArraySize;
- int totalSize = paramStruct->totalSize;
- int num = currentThread++;
- int i;
- for (i = num * subArraySize; i <= num * subArraySize + subArraySize - 1; i++) {
- if (list[i] == target) {
- found = 1;
- indx = i;
- pthread_exit(NULL);
- }
- if (i == totalSize - 1) {
- break;
- }
- }
- pthread_exit(NULL);
- }
- int startSearch(int target, int* list, int size, int subArraySize, int timesRan) {
- int i;
- int numThreads = size / subArraySize;
- if(size % subArraySize == 0) {
- numThreads++;
- }
- SearchParams* parameters = (SearchParams*) malloc(sizeof(SearchParams));
- parameters->list = list;
- parameters->subArraySize = subArraySize;
- parameters->target = target;
- parameters->totalSize = size;
- pthread_t thread[numThreads];
- for (i = 0; i < numThreads; i++) {
- pthread_create(&thread[i], NULL, search, (void*) parameters);
- }
- for (i = 0; i < numThreads; i++) {
- pthread_join(thread[i], NULL);
- }
- free(parameters);
- return indx;
- }
- // int main() {
- // int list[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
- // int indx = startSearch(5, list, 15, 5, 0);
- // printf("found at %d", indx);
- // }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement