void topsort(struct graph* p_graph) { if(!p_graph) { return; } int counter = 0; struct vertex* p_vertex = NULL; struct node* p_queue_header = NULL; struct node* p_queue_tail = NULL; queue_init(&p_queue_header,&p_queue_tail); int temp = 0; //Attention! Here we start from point 1 but not temp = 0 for(temp = 1; temp < p_graph->num_vertex; temp++) { if(p_graph->adjacent[temp].indegree == 0) { queue_enter(&p_queue_header, &p_queue_tail, temp); } } int vertex_index = 0; while(!!p_queue_header) { vertex_index = queue_out(&p_queue_header,&p_queue_tail); printf("\t%d",vertex_index); p_vertex = (struct vertex*)(&p_graph->adjacent[vertex_index]); p_vertex = p_vertex->next; for(; !!p_vertex; p_vertex = p_vertex->next) { if(--(p_graph->adjacent[p_vertex->value].indegree) == 0) { queue_enter(&p_queue_header, &p_queue_tail, p_vertex->value); } } } printf("\n"); queue_destory(p_queue_tail); }
int main(int argc, const char *argv[]) { srand(10086); sem_init(&full, 0, 0); sem_init(&empty, 0, BUFFERSIZE); pthread_mutex_init(&mutex, NULL); queue_init(&Q); pthread_t tid1, tid2; pthread_create(&tid1, NULL, producer, NULL); pthread_create(&tid2, NULL, consumer, NULL); pthread_join(tid1, NULL); pthread_join(tid2, NULL); queue_destory(&Q); pthread_mutex_destroy(&mutex); sem_destroy(&empty); sem_destroy(&full); return 0; }
int main(int argc, const char *argv[]) { srand(10086); pthread_mutex_init(&mutex, NULL); pthread_cond_init(&full, NULL); pthread_cond_init(&empty, NULL); queue_init(&Q); pthread_t t1, t2; pthread_create(&t1, NULL, producer, NULL); pthread_create(&t1, NULL, consumer, NULL); pthread_join(t1, NULL); pthread_join(t2, NULL); queue_destory(&Q); pthread_cond_destroy(&empty); pthread_cond_destroy(&full); pthread_mutex_destroy(&mutex); return 0; }