I created dispatch_queue_thread_t struct in the headerfile.
This code assign the thread, task and queue to the dThread struct
dispatch_queue_thread_t *dThread;
dThread = (dispatch_queue_thread_t *) malloc(sizeof(dispatch_queue_thread_t));
pthread_t dispatcher_thread;
if(pthread_create(&dispatcher_thread, NULL, dispatcher_threadloop, (void *)dispatch_queue)){
perror("ERROR creating thread."); exit(EXIT_FAILURE);
}
dThread->task=NULL;
dThread->queue=dispatch_queue;
dThread->thread=dispatcher_thread;
This code is the thread functions for dispatcher_thread.
I need to use thread in dThread to check if there is any task is assigned to it and if not need to assign the task to it.
How do I do that?
Is my code correct?
void *dispatcher_threadloop(void * queue){
//thread loop of the dispatch thread- pass the tast to one of worker thread
dispatch_queue_t *dQueue;
dQueue=queue;
//can I do this?
dispatch_queue_thread_t *dThread;
printf("message-boss1");
dQueue = (dispatch_queue_t *)queue;
if (dQueue->HEAD!=NULL){
for(;;){
sem_wait(dQueue->queue_task_semaphore);
dThread->task = dQueue->HEAD;
dQueue->HEAD = dQueue->HEAD->next;
dQueue->HEAD->prev = NULL;
sem_post(dQueue->queue_task_semaphore);
//TODO
}
}
printf("message-boss2");
}
No. The
dThreadvariable indispatcher_threadloop()isn’t initialised, so it’s an error to dereference it.It seems like you should be passing
dThreadto the thread function instead ofdispatchQueue, as the thread function can obtain the latter from the former. Something like this (note that casting to and fromvoid *is unnecessary):then in the thread function: