StreamReader 线程安全问题?可能吗?
我猜这不起作用,因为 StreamReader 不是线程安全的,(不知道如何解决这个问题,谷歌没有帮助)
无论如何,我一直在试图弄清楚这段代码到底出了什么问题,它可以工作 80 %的时间,其他时候它无法解析传入的数据包,只会丢弃它们。
对于我正在编写的类似 http 的 tcp 服务器来说,这是一个空白。它的工作方式与 http 数据包完全相同,但“CONTENT-LENGTH”标头告诉它数据包数据(有效负载)的长度。这就是问题发生的地方。谁能向我建议如何改进和解决这个问题?因为我完全迷路了。
void InternalStart()
{
bool continueWhile = true;
while (continueWhile)
{
if (SR.EndOfStream)
{
continueWhile = false;
break;
}
if (par_ReadStatus != ReadStatusEnum.WaitingForPayload)
{
int charCode = SR.Peek();
if (charCode == -1)
{
continueWhile = false;
break;
}
string outputLine = "";
outputLine = SR.ReadLine();
ReadLine(outputLine);
}
else if (par_ReadStatus == ReadStatusEnum.WaitingForPayload)
{
int length = int.Parse(par_ParsingPacket.Attributes["CONTENT-LENGTH"]);
char[] array = new char[length];
for (int i = 0; i < length; i++)
{
array.SetValue(Convert.ToChar(SR.Read()), i);
}
string payload = new string(array);
ReadLine(payload);
}
}
if (ReadEnd != null)
{
ReadEnd();
}
}
I'm guessing this doesn't work because of the StreamReader being non thread safe, (don't know howto fix that, google is no help)
Anyway I've been trying to figure exactly whats wrong with this code, it works 80% of the time, other times it fails to parse incoming packets and will just drop them.
This is a void for a http-like tcp server im writing. it works exactly like an http packet, but the "CONTENT-LENGTH" header tells it the length of the packets data (payload). This is where the problem is happening. Can anyone suggest to me howto improve this and fix this? because I'm completely lost.
void InternalStart()
{
bool continueWhile = true;
while (continueWhile)
{
if (SR.EndOfStream)
{
continueWhile = false;
break;
}
if (par_ReadStatus != ReadStatusEnum.WaitingForPayload)
{
int charCode = SR.Peek();
if (charCode == -1)
{
continueWhile = false;
break;
}
string outputLine = "";
outputLine = SR.ReadLine();
ReadLine(outputLine);
}
else if (par_ReadStatus == ReadStatusEnum.WaitingForPayload)
{
int length = int.Parse(par_ParsingPacket.Attributes["CONTENT-LENGTH"]);
char[] array = new char[length];
for (int i = 0; i < length; i++)
{
array.SetValue(Convert.ToChar(SR.Read()), i);
}
string payload = new string(array);
ReadLine(payload);
}
}
if (ReadEnd != null)
{
ReadEnd();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简单。初级程序员级别:不要从多个线程读取 StreamReader。尝试这样做的设计无法理解流是什么以及多线程编程的工作效率如何。
根本不需要多个线程访问单个流读取器。您必须先隔离线程,然后在处理数据时将流读取器专门分配给特定线程。如果你想变得专业和快速,你可以像 IIS 一样工作,并在基础设施线程中吸出数据,然后将工作数据包送入工作队列,多个线程进行工作。
根据性能要求,您可能希望关闭套接字并使用异步套接字机制,以确保您不会为正在进行的 1000 个操作浪费 1000 个线程,成本高昂,却没有任何好处。
啊——不错的尝试。遗憾的是,您既没有告诉我们您真正遇到的问题,也没有使用线程显示任何代码,因此最终您的问题和代码组合起来没有任何意义。
Simple. Beginner programmer level: Do not read the StreamReader from more than one thread. A design trying to do so is a failure to understand what a stream is and how efficient multi thread programming works.
There is no need to have multiple threads hit a single stream reader at all. You have to isolate threads before and assin a stream reader exclusively to a specific thread for the time of handling the data. If you want to get professional and fast you work like IIS and suck data out in infrastructure threads that then feed of work packets into a worker queue multiple threads work off.
And dependingo n performance requriements you may want to work off sockets and use the async socket mechanisms to make sure you are not wasting 1000 threads for 1000 operaions in progress at a great cost without any benefit.
Ah - nice try. Sadly you neither tell us what problem you really have nor does your code show anything using threads, so at the end your question and the code fail to make any sense in combination.