473,545 Members | 1,821 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to make non-blocking call to cin?

is it even possible or/and there is a better alternative to accept
input in a nonblocking manner?
Sep 26 '08 #1
12 29816
puzzlecracker wrote:
is it even possible or/and there is a better alternative to accept
input in a nonblocking manner?
You could try using 'peek' member function. You should get 'eof' if no
input is available, I am guessing, but don't take my word for it, RTFM
and experiment.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 26 '08 #2
On Sep 26, 6:09 pm, Victor Bazarov <v.Abaza...@com Acast.netwrote:
puzzlecracker wrote:
is it even possible or/and there is a better alternative to accept
input in a nonblocking manner?
You could try using 'peek' member function. You should get
'eof' if no input is available, I am guessing, but don't take
my word for it, RTFM and experiment.
Peek will wait for a character is one isn't available.
Basically, peek() is like get(), except that it doesn't extract
the character from the stream.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Sep 26 '08 #3
James Kanze wrote:
On Sep 26, 6:09 pm, Victor Bazarov <v.Abaza...@com Acast.netwrote:
>puzzlecracke r wrote:
>>is it even possible or/and there is a better alternative to accept
input in a nonblocking manner?
>You could try using 'peek' member function. You should get
'eof' if no input is available, I am guessing, but don't take
my word for it, RTFM and experiment.

Peek will wait for a character is one isn't available.
Basically, peek() is like get(), except that it doesn't extract
the character from the stream.
Then it's back to the platform- or implementation-specific extensions to
the library, I guess.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 26 '08 #4
On Fri, 26 Sep 2008 08:50:28 -0700, puzzlecracker wrote:
is it even possible
I don't believe it is in an OS-independent way. I don't think C++ has a
notion of non-blocking I/O at all - a read failure is always simply
treated as an "error".

Maybe have a look at the Boost.Iostreams library:

http://www.boost.org/doc/libs/1_36_0...doc/index.html

and in particular section 3.6 (Asynchronous and Non-Blocking I/O). But I
get the impression it's not quite there yet.
or/and there is a better alternative to accept input
in a nonblocking manner?
I'm not sure what you mean here.

What are you actually trying to do? I can tell you how to put stdin into
non-blocking mode on a POSIX (e.g. Linux) terminal, if you're interested
(and also how to make it non line-buffered, which you probably want too
in that case).

--
Lionel B
Sep 26 '08 #5
On Sep 26, 5:55 pm, Lionel B <m...@privacy.n etwrote:
On Fri, 26 Sep 2008 08:50:28 -0700, puzzlecracker wrote:
is it even possible

I don't believe it is in an OS-independent way. I don't think C++ has a
notion of non-blocking I/O at all - a read failure is always simply
treated as an "error".

Maybe have a look at the Boost.Iostreams library:

http://www.boost.org/doc/libs/1_36_0...doc/index.html

and in particular section 3.6 (Asynchronous and Non-Blocking I/O). But I
get the impression it's not quite there yet.
or/and there is a better alternative to accept input
in a nonblocking manner?

I'm not sure what you mean here.

What are you actually trying to do? I can tell you how to put stdin into
non-blocking mode on a POSIX (e.g. Linux) terminal, if you're interested
(and also how to make it non line-buffered, which you probably want too
in that case).

--
Lionel B
Sure, I am actually building apps on Linux, hence posix works. Can't
use boost. Please demonstrate it(please don't flame me for OT)

Thanks
Sep 27 '08 #6
puzzlecracker wrote:
is it even possible or/and there is a better alternative to accept
input in a nonblocking manner?
Is what possible? Ah, I see you have hidden the question in your
subject line.

If you want non-blocking I/O, you probably don't want iostreams, at
least not using standard streambufs. How would you differentiate a
"would block" condition from an end of file?

You can use non-blocking I/O, but you'd have to provide your own
streambuf object.

--
Ian Collins.
Sep 27 '08 #7
On Sep 27, 3:26 am, Ian Collins <ian-n...@hotmail.co mwrote:

[...]
If you want non-blocking I/O, you probably don't want iostreams, at
least not using standard streambufs. How would you differentiate a
"would block" condition from an end of file?
You can use non-blocking I/O, but you'd have to provide your own
streambuf object.
Even then, you'd have to use it outside of the normal [io]stream
interface; [io]stream will memorize any "failure". Perhaps in
collaboration with istream::readso me.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Sep 27 '08 #8
On Fri, 26 Sep 2008 17:48:06 -0700, puzzlecracker wrote:
On Sep 26, 5:55 pm, Lionel B <m...@privacy.n etwrote:
>On Fri, 26 Sep 2008 08:50:28 -0700, puzzlecracker wrote:
is it even possible

I don't believe it is in an OS-independent way. I don't think C++ has a
notion of non-blocking I/O at all - a read failure is always simply
treated as an "error".

Maybe have a look at the Boost.Iostreams library:

http://www.boost.org/doc/libs/1_36_0...doc/index.html

and in particular section 3.6 (Asynchronous and Non-Blocking I/O). But
I get the impression it's not quite there yet.
or/and there is a better alternative to accept input in a nonblocking
manner?

I'm not sure what you mean here.

What are you actually trying to do? I can tell you how to put stdin
into non-blocking mode on a POSIX (e.g. Linux) terminal, if you're
interested (and also how to make it non line-buffered, which you
probably want too in that case).

Sure, I am actually building apps on Linux, hence posix works. Can't use
boost. Please demonstrate it(please don't flame me for OT)
I won't, but someone else possibly will ;)

Here's some pretty crude code - to put stdin into non-blocking mode:

#include <unistd.h>
#include <fcntl.h>

const int fd = fileno(stdin);
const int fcflags = fcntl(fd,F_GETF L);
if (fcflags<0) { ... /* handle error */}
if (fcntl(fd,F_SET FL,fcflags | O_NONBLOCK) <0) { ... /* handle error */} // set non-blocking

"man fcntl" for more info.

To un-line buffer stdin (i.e. set terminal to "raw" mode):

#include <unistd.h>
#include <termios.h>

const int fd = fileno(stdin);
termios tcflags;
if (tcgetattr(fd,& tcflags)<0) { ... /* handle error */}
tcflags.c_lflag &= ~ICANON; // set raw mode (unset canonical modes)
if (tcsetattr(fd,T CSANOW,&tcflags )<0) { ... /* handle error */}

"man termios" for more info.

Now calls to "getchar()" and friends won't be line buffered and won't block.

"man getchar" for more info.

There is a slightly simpler way to achieve both without the "fcntl" call too:

#include <unistd.h>
#include <termios.h>

// un-line buffer stdin and optionally set non-blocking
void set_stdin(const bool block /* false for non-blocking */)
{
const int fd = fileno(stdin);
termios flags;
if (tcgetattr(fd,& flags)<0) { ... /* handle error */}
flags.c_lflag &= ~ICANON; // set raw (unset canonical modes)
flags.c_cc[VMIN] = block; // i.e. min 1 char for blocking, 0 chars for non-blocking
flags.c_cc[VTIME] = 0; // block if waiting for char
if (tcsetattr(fd,T CSANOW,&flags)< 0) { ... /* handle error */}
}

Of course you need to reset all control flags to get back to normal after.
any of these calls.

HTH,

--
Lionel B
Sep 27 '08 #9
On Sep 27, 9:41 am, Lionel B <m...@privacy.n etwrote:
On Fri, 26 Sep 2008 17:48:06 -0700, puzzlecracker wrote:
On Sep 26, 5:55 pm, Lionel B <m...@privacy.n etwrote:
On Fri, 26 Sep 2008 08:50:28 -0700, puzzlecracker wrote:
is it even possible
I don't believe it is in an OS-independent way. I don't
think C++ has a notion of non-blocking I/O at all - a read
failure is always simply treated as an "error".
Maybe have a look at the Boost.Iostreams library:
>http://www.boost.org/doc/libs/1_36_0...doc/index.html
and in particular section 3.6 (Asynchronous and
Non-Blocking I/O). But I get the impression it's not quite
there yet.
or/and there is a better alternative to accept input in a
nonblocking manner?
I'm not sure what you mean here.
What are you actually trying to do? I can tell you how to
put stdin into non-blocking mode on a POSIX (e.g. Linux)
terminal, if you're interested (and also how to make it non
line-buffered, which you probably want too in that case).
Sure, I am actually building apps on Linux, hence posix
works. Can't use boost. Please demonstrate it(please don't
flame me for OT)
I won't, but someone else possibly will ;)
Well, since he knows it's off topic, and he knows the group
where it would be on topic (and where he's really more likely to
get a complete answer): comp.unix.progr ammer.

It's fairly tricky. I wouldn't try it from std::cin, at least
not to begin with. Just set up fd 0 and read from it. (If
you're reading one character at a time, with no buffering,
istream and streambuf really don't buy you anything but
portability. Which he'll have lost anyway.)

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Sep 27 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2190
by: JW | last post by:
I'm displaying product thumbnails with brief descriptions on web pages. Clicking on the product does a javascript popup with larger image and detailed description info passed to the javascript functions from the table data. Products can change frequently, and I want to make the maintenance of this info easier by non-techies (each product has...
7
5466
by: Mike | last post by:
Hi How can I make it so my web hosting server will redirect all hits to non existant web pages to the main index.html page? Thanks in advance
1
297
by: Dev | last post by:
Hello, We have (old) softwares that can only use non ActiveX DLLs. They are written in C today. I want to write the new DLLs with VB.NET, but they still have to be used with these old softs. Is it possible to make a non-ActiveX DLL with VB.NET ? If yes, how ? How can I test it ? is it possible to test it with VB6 ?
4
3036
by: Dave | last post by:
I need to add the ability to drag from a Windows Form and drop into a non dotNet application. For example, having a generated image in my app that I wish to drag out into explorer as a friendly way to save it. I have tried creating the object that I place into the DoDragDrop() by inheriting the COM interfaces IDropSource and IDataObject...
3
9731
by: Mauzi | last post by:
hi, this may sound odd and noob like, but what is the 'big' difference between static and non-static funcitons ? is there any performace differnce? what is the best way to use them ? thnx mauzi
11
3341
by: tshad | last post by:
I am setting up some of my functions in a class called MyFunctions. I am not clear as to the best time to set a function as Shared and when not to. For example, I have the following bit manipulation routines in my Class: ******************************************************************************* imports System NameSpace MyFunctions
13
2928
by: Academic | last post by:
I have a MDI form, sometimes child forms and sometimes forms that are neither If I close the app the child forms closing and closed event happens followed by the Mdi form receiving the events.. But the regular forms that are also open do not receive that event. This is true whether there are child forms open or not.
399
12671
by: =?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= | last post by:
PEP 1 specifies that PEP authors need to collect feedback from the community. As the author of PEP 3131, I'd like to encourage comments to the PEP included below, either here (comp.lang.python), or to python-3000@python.org In summary, this PEP proposes to allow non-ASCII letters as identifiers in Python. If the PEP is accepted, the...
2
5063
by: Elniunia | last post by:
Hi, I am new to python. I have to mapp fasta file into dictionary. There are around 1000 sequences in my fasta file. The problem is that there are some the same sequences under different sequence id. I can sorted them out by accession number which is unique. The first line of my fasta file looks as follows:...
0
7487
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7420
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7680
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7934
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7446
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6003
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5349
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3476
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1908
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.