getline和线程的问题 [英] Problem with getline and threads

查看:155
本文介绍了getline和线程的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有客户端工作在2个线程。一个是发送数据,另一个是接收数据。在发送数据我有 std :: getline(std :: cin,string)。现在在这个线程中,我有无限循环,如果第二线程结束或者如果用户放EXIT命令结束,一切工作伟大的期望当第二线程结束和无限循环结束,程序仍然等待按钮,因为 std :: getline

现在有问题:

I have client that works on 2 threads. One is sending data and second one is receiving data. In sending data I have std::getline(std::cin,string). Now in this thread I have infinite loop which ends if second threads end or if user put EXIT command, everything work great expect that when second thread is ended and infinite loop is ended, program still waits for pressing button because of std::getline.
Now to question:

如何发送数据到getline到假按钮,所以我不必按按钮结束程序?

How can I send data to getline to "fake" pressing button so I don't have to press button to end program?

第二个问题我有服务器为每个客户端运行另一个线程和服务器的主线程接受新的连接,它也工作伟大。客户端存储在 std :: list 中。每个线程都有从客户端接收数据的inifnite循环。如果客户端诅咒太多,他被从服务器踢,但这里的问题,当用户做的,我只是从infite循环和线程结束,但我也想删除客户端从列表,所以在主线程的程序我必须检查

Second question I have server which for every client runs another thread and main thread of server accepts new connections, it is also working great. Client are stored in std::list. Every thread has inifnite loop which receive data from clients. If client curse too much he is getting kicked form the server but here is the problem when user do that I just break from infite loop and thread is ending but I would also like to erase client from list so in main thread of program I must check everytime if any of thread returned right value, and if it did erasing element from list?

推荐答案


每一次,如果任何线程返回正确的值,一个是发送数据,另一个是接收数据。在发送数据我有std :: getline(std :: cin,字符串)。

One is sending data and second one is receiving data. In sending data I have std::getline(std::cin,string).

这似乎不对。 cin 接受输入;它不发送东西。

That doesn't seem right. cin takes input; it doesn't send stuff. You have it blocking, in fact, which is going to cause problems.


当第二个线程结束时,它会阻塞 无限循环结束,程序仍然等待按下按钮,因为std :: getline

when second thread is ended and infinite loop is ended, program still waits for pressing button because of std::getline

啊,我们走!

Ah, there we go!


如何发送数据到getline按fake按钮,所以我不必按按钮结束程序?

How can I send data to getline to "fake" pressing button so I don't have to press button to end program?

你不能。一个更好的方法是尝试找到一种方法来取消流提取请求( getline 调用),但你也不能这样做。

You can't. A better approach would be to try to find a way to "cancel" the stream extraction request (the getline call), but you can't do this either.

代替阻止 getline 调用,使用无限循环, sleep 并在 cin 缓冲区中定期 peek ,以查看是否有要读取的数据。然后用 get 读取可用的字节。

Instead of blocking getline calls, use an infinite loop, sleep and periodic peeks into the cin buffer to see whether there's data to be read. Then read the available bytes with get.


从infite循环和线程结束,但我也想删除客户端从列表中,所以在主线程的程序,我必须检查每一次,如果任何线程返回正确的值,如果它确实擦除元素从列表?

when user do that I just break from infite loop and thread is ending but I would also like to erase client from list so in main thread of program I must check everytime if any of thread returned right value, and if it did erasing element from list?

或者客户端线程调用一些将客户端标记为要删除的成员函数。在主线程的下一个循环迭代开始时,从 std :: list 中删除​​任何标记为要删除的客户端。

Or the client thread invokes some member function that marks the client as "to delete". At the start of the next loop iteration in the main thread, delete from the std::list any client marked as "to delete".

你可以用第二个容器做这个标记,在两个线程之间仔细共享。

You could do this marking with a second container, carefully shared between the two threads.

这篇关于getline和线程的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆