break from getline

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • yuumei
    New Member
    • Jun 2007
    • 5

    break from getline

    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:

    [CODE=cpp]while() {
    if(getline(std: :cin, sTemp)) {
    ... // push event
    }
    }[/CODE]

    Thread 2:

    [CODE=cpp]while() {
    if(pollEvent(&e )) {
    switch(e.type) {
    case 1:
    // somehow send something to cin or make it stop blocking
    }
    }[/CODE]

    Thank-you!
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    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.

    Comment

    • oler1s
      Recognized Expert Contributor
      • Aug 2007
      • 671

      #3
      Getline is blocking i/o. Why explicitly choose blocking i/o, then wonder about how to make it non-blocking?

      Comment

      • yuumei
        New Member
        • Jun 2007
        • 5

        #4
        Ah-ha, I'll go find a non-blocking way of getting the keyboard input.
        Thanks!

        Comment

        • RRick
          Recognized Expert Contributor
          • Feb 2007
          • 463

          #5
          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.

          Comment

          Working...