Modo standard per implementare un flusso tamponato che risciacqua a un intervallo costante?
-
14-11-2019 - |
Domanda
Sto simulando i pacchetti da una fonte che produce pacchetti a un determinato pacchetto / secondo intervallo.Voglio effettuare una classe di stream che funziona come un oggetto ostream
, consentendo di utilizzare operator<<
per produrre le cose attraverso di essa, ma con il cavernatore che ogni valore inserito deve essere rilasciato dal buffer a un descrittore di file, in ordine, in uno specificatoIntervallo.
Quindi, ad esempio, potrei avere una presa con descrittore di file sockfd
e dire:
MyBuffer buffer(sockfd, 1000); //Interval of 1000 milliseconds
buffer << 1 << 2 << 3;
.
e l'uscita sarebbe temporizzata in modo tale da emettere
1
<1 second gap>
2
<1 second gap>
3
.
alla presa.Sto guardando Boost.Iostream in questo momento, sarebbe una buona soluzione?C'è una frase magica che posso Google che descrive questo problema che non sono a conoscenza?
Qualsiasi aiuto sarebbe apprezzato.
Grazie BRAD
Soluzione
One option for doing this that's completely orthogonal to building a custom streams class would be to maintain a queue of strings that's polled by a thread every second. Each time the queue is polled, the thread reads out the first element and sends it across the network.
This doesn't use the streams library, but I think that might be what you want. Internally, most streams just glob together all the input they get into a mass of text, losing the information about which parts of the text correspond to each object you inserted.
EDIT: I should have mentioned this the first time around, but please be sure to use the appropriate synchronization on this queue! You'll probably want to use a mutex to guard access to it, or to use a clever lock-free queue if that doesn't work. Just be sure not to blindly read and write to it from multiple threads.
Altri suggerimenti
Should the 1000ms be asynchronous ? If not, you could put a Sleep(1000) in your stream's operator<<. Depending on what you're trying to do, it could suit you.