在我的一个程序中,有多个客户端,每个客户端都有自己的缓冲区。在一个无限循环中,我检查是否有任何客户端有任何数据要写入磁盘。如果是这样,那么我也这样做并继续。
现在,因为客户端写入的数据实际上不在我的控制范围内(一些计算的结果),所以我需要一个动态缓冲区。所以伪代码看起来像这样:
//If data is ready
//Append(client_id, line)
void Append(int client_id, char *line) {
if(client_id.buffer == NULL) {
buffer = (char*)malloc(BUFFERSIZE * sizeof(char));
//Copy line into buffer
} else {
//Realloc the buffer if insufficient space and append this
//line to the existing buffer
}
}
或者另一种方法是使用简单的消息队列。我会继续将任何消息(字符串)添加到现有队列中,然后将它们读出。还有其他更好的方法吗?