static void test_basics(void) { Queue *q = QueueNew(free); assert_int_equal(0, QueueCount(q)); assert_true(QueueIsEmpty(q)); QueueEnqueue(q, xstrdup("hello")); assert_int_equal(1, QueueCount(q)); assert_false(QueueIsEmpty(q)); assert_string_equal("hello", QueueHead(q)); QueueEnqueue(q, xstrdup("world")); assert_int_equal(2, QueueCount(q)); assert_string_equal("hello", QueueHead(q)); char *head = QueueDequeue(q); assert_string_equal("hello", head); free(head); assert_string_equal("world", QueueHead(q)); head = QueueDequeue(q); assert_string_equal("world", head); free(head); QueueDestroy(q); }
void* Control_thread_function() { pthread_t threadFFmpeg; // declare FFmpeg thread object int thread_id, thread_arg; unsigned int sequenceCnt = 0, sequenceFlag = 0; // This loop ends when 'controlThreadEndFlag' is TRUE and the queue is empty as well while(!(getControlThreadEndFlag() && QueueIsEmpty())) { if(QueueIsEmpty() == FALSE) // there is something to read in the queue { // create and run a new thread thread_arg = Dequeue(); thread_id = pthread_create(&threadFFmpeg, NULL, Ffmpeg_thread_function, (void *)&thread_arg); if(thread_id < 0) // when thread doesnt work properly { perror("FFmpeg thread create error\n"); exit(-1); } pthread_join(threadFFmpeg, NULL); // remove the foremost media segment in playlist.m3u8 if(sequenceFlag < MAX_QUEUE_N) sequenceFlag++; else RemoveForemostMedia(sequenceCnt++); } // else{continue}; // there is nothing to read in the queue } pthread_exit(0); }
int main(void) { Queue que; char op[5]; QueueInit(&que); /* キューの初期化 */ //キューの中身を確認 if(!QueueIsEmpty(&que)){ printf("初期化失敗\n"); return (-1); } while (1) { int m, no; char name[NAMELEN]; Node *data; printf("Operation:"); printf("(1)データの追加(enqueue),(2)データの削除(dequeue), (0) 終了:"); scanf("%s", op); m=atoi(op); if (m == 0) break; switch(m) { case 1: data = AllocNode(); printf("追加するデータを入力してください。\n"); printf("番号:");scanf("%d", &no); printf("名前:");scanf("%s", name); SetNode(data,no,name,NULL); QueueEnque(&que, data); //キューの中身を確認 if(QueueIsEmpty(&que)){ printf("エンキュー失敗\n"); return (-1); } break; case 2: if(data = QueueDeque(&que)) { puts("デキューしたデータ:"); printf("番号:%d,名前:%s\n", data->no, data->name); // free(data); } else { puts("デキューできません。"); } break; } printf("キューの一覧<%d>:\n", QueueNo(&que)); PrintQueue(&que); } QueueFree(&que); return(0); }
VOID QueueMoveToBack(Queue *pQueue, QueueNode *pNode, QueueNode *pPrevNode) { ASSERT(pQueue != NULL); ASSERT(pNode != NULL); if (pQueue->lpCriticalSection != NULL) EnterCriticalSection(pQueue->lpCriticalSection); // If it is the first element. if (pPrevNode == NULL) { pQueue->pFirst = pNode->pNext; } // If it has a predecessor in the queue else { pPrevNode->pNext = pNode->pNext; } // If it is the last element. if (pNode->pNext == NULL) { if (pQueue->lpCriticalSection != NULL) LeaveCriticalSection(pQueue->lpCriticalSection); return; } // Put the subqueue onto the back of the queue. pNode->pNext = NULL; if (QueueIsEmpty(pQueue)) { pQueue->pFirst = pNode; pQueue->pLast = pNode; } else { pQueue->pLast->pNext = pNode; pQueue->pLast = pNode; } if (pQueue->lpCriticalSection != NULL) LeaveCriticalSection(pQueue->lpCriticalSection); }
/*宽度优先*/ void BFS ( LGraph Graph, Vertex S) { Queue Q; Vertex V; PtrToAdjVNode W; InitializeQueue(&Q); /* 访问顶点S:此处可根据具体访问需要改写 */ printf("% d",S); Printed[S] = true; /*本例中表示该节点已被输出*/ Visited[S] = true; /* 标记S已访问 */ EnQueue(S,&Q); /* S入队列 */ while ( !QueueIsEmpty(&Q) ) { DeQueue(&V,&Q); /* 弹出V */ for( W=Graph->G[V].FirstEdge; W; W=W->Next ) /* 对图中的每个顶点W */ if ( !Visited[W->AdjV] ) { /* 访问顶点W */ printf("% d",W->AdjV); Printed[W->AdjV] = true; Visited[W->AdjV] = true; EnQueue(W,&Q); /* W入队列 */ } } }
void *front(Queue *Q) { if (QueueIsEmpty(Q)) { printf("ERROR: Reading from empty Queue\n"); exit(0); } return Q->front->next->value; }
void EmptyQueue(Queue * pq) { Item dummy; while(!QueueIsEmpty(pq)) DeQueue(&dummy,pq); }
int main(int argc, char *argv[]) { char* names = argv[1]; char* name, **name_p; Queue* queue; struct Linked_list *iterator; queue = createQueue(sizeof(char*)); name = strtok(names, ","); while (name != NULL) { printf("enqueueing %s - %i\n", name, QueueSize(queue)); Enqueue(queue, name); name = strtok(NULL, ","); } for(iterator = queue->front->next; iterator != NULL; iterator = iterator->next) { printf("element %s\n", (char*)(iterator->value)); } // printf("front %s\n", (char*)((Linked_list*)(front(queue)))->value); while(!QueueIsEmpty(queue)) { printf("dequeued "); fflush(stdout); DequeueElement(queue, (void**)&name); // name = *name_p; printf("%s - %i\n", name, QueueSize(queue)); } }
void *QueueRemove(QUEUE *Queue) { void *Data; QUEUE_NODE *TempQueueNode; #if (QUEUE_SAFE_MODE == 1) if(QueueIsNull(Queue)) return (void*)NULL; if(QueueIsEmpty(Queue)) return (void*)NULL; #endif // end of QUEUE_SAFE_MODE // Set the temp node to the QUEUE's Head. TempQueueNode = (QUEUE_NODE*)(Queue->Head); // Iterate the QUEUE's Head to the next node (We don't know if it's NULL yet). Queue->Head = (QUEUE_NODE*)(Queue->Head->Next); // Get a handle on the data of the node we're about to remove. Data = (void*)(TempQueueNode->Data); // Free the old node. QueueMemDealloc(TempQueueNode); // MemDealloc defined in QueueConfig.h // Check to see if the Queue is now empty. if(Queue->Head == (QUEUE_NODE*)NULL) Queue->Tail = (QUEUE_NODE*)NULL; // No matter what, decrement the QUEUE's size by one. Queue->Size--; return (void*)Data; }
static void FASTCALL CloseTask(JSObject *obj) { TaskPrivate *pv = (TaskPrivate*)JL_GetPrivate(obj); if ( !pv ) return; JLMutexAcquire(pv->mutex); // -- pv->end = true; JLMutexRelease(pv->mutex); // ++ JLSemaphoreRelease(pv->requestSem); // +1 // unlock the thread an let it manage the "end" JLThreadWait(pv->threadHandle); // wait for the end of the thread JLThreadFree(&pv->threadHandle); JLSemaphoreFree(&pv->requestSem); JLSemaphoreFree(&pv->responseSem); JLEventFree(&pv->responseEvent); JLMutexFree(&pv->mutex); while ( !QueueIsEmpty(&pv->requestList) ) { SerializedData * ser = (SerializedData *)QueueShift(&pv->requestList); SerializerFree(&ser); } delete pv; }
UINT32 QueueGetSizeInBytes(QUEUE *Queue, UINT32 DataSizeInBytes) { UINT32 Size; #if (QUEUE_SAFE_MODE == 1) if(QueueIsNull(Queue)) return (UINT32)0; #endif // end of QUEUE_SAFE_MODE Size = (UINT32)sizeof(QUEUE); if(QueueIsEmpty(Queue)) return (UINT32)Size; /* If the user provided us with the size of their data in bytes then we must use it to calculate the size of the QUEUE in bytes. Otherwise we just don't include it in the calculation. */ if(DataSizeInBytes) Size += (Queue->Size * (DataSizeInBytes + (UINT32)sizeof(QUEUE_NODE))); else Size += (UINT32)(Queue->Size * (UINT32)sizeof(QUEUE_NODE)); return (UINT32)Size; }
BOOL QueueClear(QUEUE *Queue) { #if (QUEUE_SAFE_MODE == 1) if(QueueIsNull(Queue)) return (BOOL)FALSE; if(QueueIsEmpty(Queue)) return (BOOL)TRUE; #endif // end of QUEUE_SAFE_MODE while(Queue->Head != (QUEUE_NODE*)NULL) { // Iterate the Tail pointer to the node after the Head. Queue->Tail = (QUEUE_NODE*)(Queue->Head->Next); #if(USING_QUEUE_DEPENDENT_FREE_METHOD == 1) if(Queue->QueueFreeMethod) { Queue->QueueFreeMethod((void*)(Queue->Head->Data)); } #endif // end of USING_QUEUE_DEPENDENT_FREE_METHOD QueueMemDealloc((void*)(Queue->Head)); // MemDealloc defined in QueueConfig.h // Iterate the Head to the Tail. Queue->Head = (QUEUE_NODE*)(Queue->Tail); } // The size is now 0 since the QUEUE is empty. Queue->Size = (UINT32)0; return (BOOL)TRUE; }
unsigned int ABPEvenCount (PtABPNode proot) { PtABPNode node = proot; PtQueue queue; unsigned int count = 0; if (proot == NULL) { Error = ABP_EMPTY; return -1; } if ((queue = QueueCreate(sizeof(PtABPNode))) == NULL) { Error = NO_MEM; return -1; } QueueEnqueue(queue, &node); while (!QueueIsEmpty(queue)) { QueueDequeue(queue, &node); if (!(node->Elem & 1)) count++; if (node->PtLeft != NULL) QueueEnqueue(queue, &(node->PtLeft)); if (node->PtRight != NULL) QueueEnqueue(queue, &(node->PtRight)); } QueueDestroy(&queue); Error = OK; return count; }
/** Returns the data element at the front of the Queue This #does not# pop the data off the front of the Queue. It only returns a copy of it. If the Queue is empty, then this function will return NULL. \param Q The Queue to perform this function on \return Returns a copy of the first element of the Queue, returns NULL if Queue is empty */ void* QueueFront(Queue* Q) { if(!QueueIsEmpty(Q)) return Q->data[Q->front]; return NULL; }
void cQueueP::QueueInsert(queueItemType NewItem, bool& Success) { // create a new node ptrType NewPtr = new queueNode; Success = bool(NewPtr != NULL); // check allocation if (Success) { // allocation successful; set data portion of new node NewPtr->Item = NewItem; // insert the new node if (QueueIsEmpty()) // insertion into empty queue NewPtr->Next = NewPtr; else { // insertion into nonempty queue NewPtr->Next = BackPtr->Next; BackPtr->Next = NewPtr; } // end if BackPtr = NewPtr; // new node is at back } // end if } // end QueueInsert
VOID * QueueHashRemoveFromSubqueue(Queue *pQueue) { Queue * pSubQueue; QueueNode *pPrevNode; VOID * pData; ASSERT(pQueue != NULL); if (pQueue->lpCriticalSection != NULL) EnterCriticalSection(pQueue->lpCriticalSection); pSubQueue = NULL; #ifdef DEBUG2 DbgMsgRecord(TEXT("-> QueueHashRemoveFromSubqueue\n")); #endif // If the hash queue is not empty. if (pQueue->pFirst != NULL) { // Find the first nonempty subqueue. pPrevNode = NULL; for (QueueNode *pNode = pQueue->pFirst; pNode != NULL; pNode = pNode->pNext) { // Check if it has elements in it. if (!QueueIsEmpty((Queue*) ((QueueHashNode *)pNode->pData)->pValue)) { // Select it. We will extract an element off this subqueue // and put the subqueue into the end of the hash queue. pSubQueue = (Queue*) ((QueueHashNode *)pNode->pData)->pValue; // Take it off the hash queue and put it // on the back. QueueMoveToBack(pQueue, pNode, pPrevNode); break; } pPrevNode = pNode; } if (pSubQueue == NULL) { // We did not find any nonemoty subqueues. pData = NULL; } } else { // There are no nonempty subqueues. pData = NULL; } if (pSubQueue != NULL) { // Extract an element from the subqueue. pData = QueueRemove(pSubQueue); } #ifdef DEBUG2 DbgMsgRecord(TEXT("<- QueueHashRemoveFromSubqueue\n")); #endif if (pQueue->lpCriticalSection != NULL) LeaveCriticalSection(pQueue->lpCriticalSection); return pData; }
cQueueP::~cQueueP() { bool Success; while (!QueueIsEmpty()) QueueDelete(Success); // Assertion: BackPtr == NULL } // end destructor
int QueueFront(SEQQUEUE sq, DATATYPE *e) //将顺序循环队列sq的队头元素保存到e所指地址,成功返回1,失败返回0 { if (QueueIsEmpty(sq)) { printf("queue is empty!\n"); return 0;} else { *e = sq.data[(sq.front)]; return 1;} }
uint16_t lw_QueueMessagesWaiting (const tQueueHandle QueueId) { tQueueHeader *pQueue = (tQueueHeader *)QueueId; if ( QueueIsEmpty (pQueue)) return 0; else return 1; }
// Перебор узлов очереди void* QueueGet(queue* header){ if (QueueIsEmpty(header)){ return NULL; } void *uzel = header->next->uzel; header->next = header->next->next; free(header->next->prev); header->next->prev = header; return uzel; }
QUEUE_DAT_T QueueRemove (PQueue Queue) { QUEUE_DAT_T ret; if (QueueIsEmpty(Queue) == TRUE) return NULL; ret = Queue->Data[Queue->Head]; Queue->Head = (Queue->Head + 1) % Queue->Size; Queue->Used--; return ret; }
void WirelessSendHandler(void *CallBackRef, unsigned int EventData) { unsigned char c; wireless.send_in_progress = 0; if(!QueueIsEmpty(&(wireless.write_queue))) { wireless.send_in_progress = 1; c = QueuePop(&(wireless.write_queue)); XUartLite_Send(&wireless.uart, &c, 1); } }
unsigned char QueueDelete(ZQUEUE *q,unsigned char *item) { if(!QueueIsEmpty(q)) { *item=q->item[q->head]; q->head=(q->head+1)%QUEUE_MAX; return 1; } return 0; }
int SafeQueueIsEmpty(SafeQueue* _queue) { if(NULL == _queue) { return 0; } pthread_mutex_lock(&_queue->m_mutex); return QueueIsEmpty(_queue->m_queue); pthread_mutex_unlock(&_queue->m_mutex); }
/** * @brief * @param Q * @returns * * */ ElementType QueueDequeue(Queue Q) { if (QueueIsEmpty(Q)) { fprintf(stderr, "Dequeue: Queue is empty\n"); exit(1); } Q->front = succ(Q->front, Q); Q->size--; return Q->array[Q->rear]; }
void cQueueP::GetQueueFront(queueItemType& QueueFront, bool& Success) const { Success = bool(!QueueIsEmpty()); if (Success) { // queue is not empty; retrieve front ptrType FrontPtr = BackPtr->Next; QueueFront = FrontPtr->Item; } // end if } // end GetQueueFront
void Dequeue(Queue *Q) { if (QueueIsEmpty(Q)) { fprintf(stderr, "ERROR: Dequeueing from empty queue\n"); return; } else { Q->front->next = Q->front->next->next; if (Q->front->next == NULL) Q->rear = Q->front; } return; }
void *QueuePeek(QUEUE *Queue) { #if (QUEUE_SAFE_MODE == 1) if(QueueIsNull(Queue)) return (void*)NULL; if(QueueIsEmpty(Queue)) return (void*)NULL; #endif // end of QUEUE_SAFE_MODE return (void*)(Queue->Head->Data); }
void cQueueP::QueueDelete(queueItemType& QueueFront, bool& Success) { Success = bool(!QueueIsEmpty()); if (Success) { // queue is not empty; retrieve front ptrType FrontPtr = BackPtr->Next; QueueFront = FrontPtr->Item; QueueDelete(Success); // delete front } // end if } // end QueueDelete
int QueueOut(SEQQUEUE *sq) //将队列sq队首元素出队列,成功返回1,失败返回0 { if (QueueIsEmpty(*sq)) { printf("queue is empty!\n"); return 0; } else { sq->front = (sq->front + 1) % maxsize; return 1; } }