지속적인 간격으로 플러시하는 버퍼링 된 스트림을 구현하는 표준 방법은 무엇입니까?

StackOverflow https://stackoverflow.com/questions/5034202

문제

주어진 패킷 / 두 번째 간격으로 패킷을 생성하는 소스에서 패킷을 시뮬레이션합니다.ostream 객체와 같이 작동하는 스트림 클래스를 만들고, operator<<를 사용하여 사물을 출력하는 데 사용할 수 있지만 각 값이 삽입 된 각 값이 버퍼에서 파일 설명 자로 버퍼에서 파일 설명자로 해제되어야하는 경고가 지정됩니다.간격.

그래서, 예를 들어, 파일 디스크립터 sockfd가있는 소켓을 가질 수 있으며 다음과 같이 말한다 :

MyBuffer buffer(sockfd, 1000); //Interval of 1000 milliseconds
buffer << 1 << 2 << 3;
.

및 출력은 를 출력하도록 시간이 초과됩니다.

1
<1 second gap>
2
<1 second gap>
3
.

소켓에.지금 부스트를보고 있습니다. 지금 당장 좋은 해결책이 될 것입니까?내가 알지 못하는이 문제를 설명하는 Google을 할 수있는 마법의 구절이 있습니까?

도움이되는 모든 도움이 될 것입니다.

고맙습니다 브래드

도움이 되었습니까?

해결책

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.

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top