473,321 Members | 1,877 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,321 software developers and data experts.

break from getline

yuumei
5
Hi, I was wondering if there is any way of either breaking from or passing values to cin to get it to stop blocking. I have two threads, one blocking all the time and waiting for user input and the other acting on that input.

Thread 1:

Expand|Select|Wrap|Line Numbers
  1. while() {
  2.     if(getline(std::cin, sTemp)) {
  3.         ... // push event
  4.     }
  5. }
Thread 2:

Expand|Select|Wrap|Line Numbers
  1. while() {
  2.     if(pollEvent(&e)) {
  3.         switch(e.type) {
  4.             case 1:
  5.                 // somehow send something to cin or make it stop blocking
  6.     }
  7. }
Thank-you!
Dec 14 '07 #1
4 5091
weaknessforcats
9,208 Expert Mod 8TB
You are dead. Your program is in suspension until the user gets back from his vacation and presses the enter key. That is, it's not running.

This is the traditional PEBKAC error. (Problem Exists Between Keyboard And Chair.
Dec 17 '07 #2
oler1s
671 Expert 512MB
Getline is blocking i/o. Why explicitly choose blocking i/o, then wonder about how to make it non-blocking?
Dec 17 '07 #3
yuumei
5
Ah-ha, I'll go find a non-blocking way of getting the keyboard input.
Thanks!
Dec 18 '07 #4
RRick
463 Expert 256MB
Why do you need to tell cin to stop blocking?

You have 2 threads, one blocks on cin and the other does its work. As long as the blocking cin thread doesn't stop the whole process (it shouldn't) and the other thread does not need to access cin, you should have no deadlock issues.

You should be able to interrupt a thread in a blocking state (i.e. pthread_cancel), but I wouldn't trust using that thread for any further work.
Dec 18 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Vikram | last post by:
Hi, I don't remember if it happened previously, but nowadays I'm having problem with using cin.getline function and cin>> function simultaneously. I have Visual Studio 6. If I use cin.getline...
5
by: vknid | last post by:
Hello, I have a question. Its probably a very newbish question so please be nice hehe. =D I have been reading through C++ Programming Fundamentals, and have come a crossed an example program...
1
by: ma740988 | last post by:
Consider: ifstrem MyFile("extractMe.txt"); string Str; getline(MyFile, Str); getline above extracts the contents of MyFile and place into the string object. Deduced using FROM/TO logic I...
10
by: Skywise | last post by:
I keep getting the following error upon compiling: c:\c++ files\programs\stellardebug\unitcode.h(677) : error C2664: 'class istream &__thiscall istream::getline(char *,int,char)' : cannot convert...
14
by: KL | last post by:
I am so lost. I am in a college course for C++, and first off let me state I am not asking for anyone to do my assignment, just clarification on what I seem to not be able to comprehend. I have a...
18
by: Amadeus W. M. | last post by:
I'm trying to read a whole file as a single string, using the getline() function, as in the example below. I can't tell what I'm doing wrong. Tried g++ 3.2, 3.4 and 4.0. Thanks! #include...
2
by: jalkadir | last post by:
I am trying to get character string from the user, to do that I use getline(char_type*, streamsize), but I get a segmentation fault??!! Can anyone give me a hand, what am I doing wrong? --snip...
33
by: Chen shuSheng | last post by:
I have a code: --------------------------- #include <iostream.h> #include <stdlib.h> int main() { int max=15; char line; getline(line,max); system("PAUSE");
3
by: JackC | last post by:
Hi, How do i use stringstreams getline function to extract lines from an existing string? Say i have: string strlist = "line1\r\nLine2\r\nLine3\r\n"; I want to extract each line out into a...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.