void *threadWork(void *args) { int slice = 0; bool found = false; int position = -1; int startIndex, endIndex = 0; struct threadArgs * newArgs; newArgs = (struct threadArgs *)args; while ( true ) { pthread_mutex_lock(&SVmutex); //LOCK if ( SV == NS + 1 ) { pthread_mutex_unlock(&SVmutex); pthread_exit(NULL); } slice = SV; //get the current slice to search SV++; //increment slice variable startIndex = computeStartIndex(slice, DATA_SIZE, NS); endIndex = computeEndIndex(slice, DATA_SIZE, NS); pthread_mutex_unlock(&SVmutex); //UNLOCK int i; for ( i = startIndex ; (i < endIndex) && !found ; i++ ) { if ( strcmp(newArgs->searchArray[i], newArgs->searchString) == 0) { found = true; position = i; } } pthread_mutex_lock(&consoleMutex); //Lock for console output if ( found ) { printf("thread %d,\t found yes,\t slice %d,\t position %d\n", newArgs->threadId, slice, position + 4); found = false; } else printf("thread %d,\t found no,\t slice %d,\t position %d\n", newArgs->threadId, slice, position); pthread_mutex_unlock(&consoleMutex); //Unlock for console output //sleep(1); //SOMETIMES THREAD 1 IS SO FAST IT FINISHES SEARCH, LETS OTHER THREADS WORK } }
void DayViewModel::assignDisplayValues() { int count = itemsList.count(); if(count == 0) return; QMultiHash<int,int> hashmap; //Counting how many items start at an index for(int i=0;i<count;i++) { int index = 0,itemCount=0; CalendarDataItem *calItem = ((CalendarDataItem*)(itemsList.at(i))); index = computeStartIndex(calItem->startTime); ((CalendarDataItem*)(itemsList.at(i)))->startIndex = index; ((CalendarDataItem*)(itemsList.at(i)))->xUnits = 0; ((CalendarDataItem*)(itemsList.at(i)))->yUnits = 0; ((CalendarDataItem*)(itemsList.at(i)))->widthUnits = 1.0; double htVal = (calItem->startTime.secsTo(calItem->endTime) / (60.0*30.0)); if(htVal<0.5) { htVal = 0.5; } htVal = round(htVal); ((CalendarDataItem*)(itemsList.at(i)))->heightUnits = htVal; qDebug()<<"xUnits="<<calItem->xUnits<<",yUnits="<<calItem->yUnits<<",widthUnits="<<calItem->widthUnits<<", heightUnits="<<calItem->heightUnits<<",startIndex="<<calItem->startIndex; for(int j=0;j<calItem->heightUnits;j++) { if(hashmap.count(index+j) == 0) { itemCount = 1; } else { itemCount = hashmap.value(index+j); itemCount++; } hashmap.replace(index+j,itemCount); } } //Assign width values based on number of items at an index and their height for(int i=0;i<count;i++) { CalendarDataItem *calItem = ((CalendarDataItem*)(itemsList.at(i))); int startIndex = calItem->startIndex; int htUnits = calItem->heightUnits; int maxCount=1,tmpVal=0; for(int j=0;j<htUnits;j++) { tmpVal = hashmap.value(startIndex+j); if(tmpVal>maxCount) { maxCount = tmpVal; } } calItem->widthUnits = (calItem->widthUnits)/maxCount; } //Assign xUnits value QMultiHash<int,int> xOffsetHashmap; for(int i=0;i<count;i++) { int itemCount=0; CalendarDataItem *calItem = ((CalendarDataItem*)(itemsList.at(i))); int startIndex = calItem->startIndex; int htUnits = calItem->heightUnits; int maxCount=0,tmpVal=0; for(int j=0;j<htUnits;j++) { tmpVal = xOffsetHashmap.value(startIndex+j); if(tmpVal>maxCount) { maxCount = tmpVal; } } calItem->xUnits=maxCount; for(int j=0;j<htUnits;j++) { if(xOffsetHashmap.count(startIndex+j) == 0) { itemCount = 1; } else { itemCount = xOffsetHashmap.value(startIndex+j); itemCount++; } xOffsetHashmap.replace(startIndex+j,itemCount); } } return; }