void producer(int id,int shmid,int full,int empty,int mutex){ struct sembuf semV,semP; initSemaphoreOperation(&semV,0,0,1); initSemaphoreOperation(&semP,0,0,-1); while(1){ // sleep(1); P(empty,semP); P(mutex,semP); produceItem(id,shmid); V(mutex,semV); V(full,semV); } return; }
void* producer(void* t) { long tid = (long) t; while (1) { int item = produceItem(); // Generate the random number while (itemCount == BUFFER_SIZE); // Spin if buffer is full putItemIntoBuffer(item); // Once buffer has room, add the item to the buffer printf("Producer added: %d\n", item); itemCount = itemCount + 1; } pthread_exit(NULL); }
//producer void* producer(void* arg) { /* Cast the cookie pointer to the right type. */ struct thread_args* p = (struct thread_args*) arg; int item; while (1) //do a while (true) for continous or while (n) to limit number of times ran { item = produceItem(); sem_wait(&emptyCount); //down (decrement) the emptyCount. when emptyCount is 0 thread will wait... putInWarehouse(item, p->size, p->ptr_wh, p->ptr_itemCount); sem_post(&fillCount); //up (increment) the fillCount } }
void* producer(void* parameters) { int item, im, itm, lc; lc = LOOP_COUNT; while (lc) { item = produceItem(); sem_wait(&emptyCount); pthread_mutex_lock(&mutex1); im = itemCount; itm = buffer[itemCount]; buffer[itemCount] = item; /* Put random number in buffer */ itemCount++; /* Bump the count */ pthread_mutex_unlock(&mutex1); sem_post(&fillCount); if (itm != -1) { printf("Error writing to buffer. It wasn't empty. \n"); } printf("Thread %d put a random number %d in the buffer at location %d.\n",(int) pthread_self(), item, im); lc--; } }
void specActionLibrary::readFromStream(QDataStream& in) { qint32 num, position ; in >> num >> position ; // TODO really rely on num? foreach(QObject * parent, parents) qDebug() << "Parent: " << parent->objectName() ; type t ; QVector<qint32> parentIndex(num, 0) ; int progressToGo = 0, progressStart = 0 ; if(progress) { progressStart = progress->value() ; progressToGo = progress->maximum() - progressStart ; } for(int i = 0 ; i < num ; ++i) { if(progress) progress->setValue(progressStart + (i * progressToGo) / num); in >> t >> parentIndex[i] ; specStreamable* streamable = produceItem(in) ; specUndoCommand* command = dynamic_cast<specUndoCommand*>(streamable) ; if(!command) { qDebug() << "Error reading command no." << i << "of type" << t ; undoStack->clear(); delete streamable ; // Reset loop invariants num -= i ; i = 0 ; parentIndex = QVector<qint32>(num,0) ; continue ; } ////// For manually discarding corrupted undo commands: #ifdef DEBUGCOMMANDREADER qDebug() << "Reading item:" << i << "total count:" << undoStack->count() << "/" << num ; if (QMessageBox::question(0,tr("Really read?"), tr("Really read command no. ") + QString::number(i) + tr("? Description is:\n") + command->text(), QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes) == QMessageBox::Yes) #endif undoStack->push(command) ; #ifdef DEBUGCOMMANDREADER else delete command ; #endif } undoStack->setIndex(position) ; for(int i = 0 ; i < undoStack->count() ; ++i) ((specUndoCommand*) undoStack->command(i))->setParentObject(parents[parentIndex[i]]) ; qDebug() << "to be read:" << num << "actually on stack:" << undoStack->count() ; undoStack->setClean(); }