Вопрос

I am trying to solve a multiple producer single consumer problem with the producers generating integers and the consumer making a sorted list out of all these integers(using AVL tree). How should I be locking my tree data structure? Or is it unnecessary? The queue has proper locking for push and pop operations.

void consumer(NaiveQueue<nodeQ> &obj)
{
    while(1)
    {
    nodeQ *temp;
    temp=NULL;
    temp=obj.pop();
    if(temp)
      {
        cout << "\nRemoved : " << temp->data;
        root=insert(root,temp->data); //****lock this??????
        delete temp;
      }
   }  
}

Producer(Multithreaded producers => threads made in main):

void makeChildThread(int &newsockfd,char msg[MAXSZ],NaiveQueue<nodeQ> &obj)
{
   //receive from client
   while(1)
   {
    int n=recv(newsockfd,msg,MAXSZ,0);
    if(n==0)
    {
     close(newsockfd);
     break;
    }
    //msg[n]=0;
    //send(newsockfd,msg,n,0);
    int val=atoi(msg);
    if(val == 0)
    {
        break;
        exit(0);
    }

    nodeQ *temp;
    temp=new nodeQ();
    temp->data=val;
    obj.push(temp);
    cout<<"\nPushed : "<<val;

    cout<<"\nReceived :"<<msg;
   }//close while
    cout<<"\nExit from thread";
}
Это было полезно?

Решение

Since the queue will be accessed by all producers and consumer, the access needs to synchronize. But if the tree is used by the single consumer, why lock it? Locking is unnecessary and will cause extra overhead.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top