vendredi 30 octobre 2020

Two threads, how to implement communication between them. (Pseudocode)

I have two threads:

  1. Main thread :

    • Main thread is listening for HTTP requests,
    • Main thread registers handler to HTTP request used for long polling
  2. Second thread:

    • Getting data from different socket.
    • if it finds something in data from socket, updates thread local storage, and IF Main thread has HTTP request pending it sends data to it somehow.

How do I make Main thread and second thread communicate to each other?

Main Thread http handler pseudocode:

function mg_handler(request){

   var handle = parseHandle(request.data);
   var storage_name = parseStorageName(request.data);

   var response = WaitForResponse(handle,storage_name);

   mg_printf(rasponse);

   return;

}

Second thread pseudocode:

 var storage
 
 function t_run()
 {
   var buf
   while(1){
       recvfrom(socket,buf);
       var result;
       bool found_something = search(buf,result);
       if(found_something){
          update(storage,result);
          //if WaitForResponse is waiting let it continue by sending storage to it somehow.
          //???
       }
    }

    cleanup:

    return;
 }

Aucun commentaire:

Enregistrer un commentaire