logo
down
shadow

C++ console input blocks so i can't kill thread


C++ console input blocks so i can't kill thread

By : Koukouttou
Date : November 22 2020, 01:01 AM
should help you out What you could do is using futures to allow the user to input something with a time limit. You can then insert this code into your main loop
code :
#include <iostream>       // std::cout
#include <future>         // std::async, std::future
#include <chrono>         // std::chrono::milliseconds
#include <string>
using namespace std;


bool myAsyncGetline(string & result)
{
    std::cout<<"Enter something within the time limit"<<endl;
    getline(cin,result);
    return true;
}

int main()
{
  // call function asynchronously:
  string res;
  std::future<bool> fut = std::async (myAsyncGetline,res); 


  std::chrono::seconds span (20);
  if (fut.wait_for(span)==std::future_status::timeout)
    std::cout << "Too Late!";
  else
      cout<<"You entered "<<res<<" "<< endl;

  return 0;
}


Share : facebook icon twitter icon
does the one thread kill the other in a iphone application( both thread are main thread)?

does the one thread kill the other in a iphone application( both thread are main thread)?


By : user3110278
Date : March 29 2020, 07:55 AM
around this issue The Apple reference documents on the use of the method - (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait states
How Do I Kill A Thread In Racket? Kill-thread keeps using "proc" rather than thread

How Do I Kill A Thread In Racket? Kill-thread keeps using "proc" rather than thread


By : Adrian Lacheta
Date : March 29 2020, 07:55 AM
like below fixes the issue Right now, you're creating the new thread and deleting it immediately with (kill-thread (start)), while the old one is untouched.
If you only need to keep track of the single main game thread (aka thats the only thing passing through start and stop) you can use set! and a global variable to keep track of the thread:
code :
#lang racket

(define game
  (lambda ()
    (let loop ()
      (sleep 2)
      (printf "game running \n")
      (loop))))

(define GAME-THREAD (void))

(define start (lambda () (set! GAME-THREAD (thread game))))
(define stop (lambda () (with-handlers
                            ([exn:fail? 
                              (lambda (exn)
                                 (displayln (string-append "Failed to kill main game thread. " (exn-message exn)))
                                #f)])
                          (kill-thread GAME-THREAD))))
How to test a thread that reads from Console.ReadLine i.e. how do I write to the console's input?

How to test a thread that reads from Console.ReadLine i.e. how do I write to the console's input?


By : user3010170
Date : March 29 2020, 07:55 AM
like below fixes the issue I think you need to take a step back on this one. What you have done is couple your application to the console. Whereas you should really separate it... Here is some pseudo-code:
Your Application
code :
public interface IUserInput
{
    string ReadInput();
}

public class ConsoleInput : IUserInput
{
    public ReadInput()
    {
        return Console.ReadLine();
    }
}

public class YourClass
{
    IUserInput _userInput;

    // Can inject TEST or REAL input
    public YourClass(IUserInput userInput)
    {
        _userInput = userInput;
    }

    // ... Your code
    public void YourMethod()
    {
        var doSomething = _userInput.ReadInput();
    }
}
public class TestInput : IUserInput
{
    public ReadInput()
    {
        return "This is dummy data";
    }
}

[Test]
public void MyTest()
{
    var testInput = new TestInput();
    var systemUnderTest = new YourClass(testInput);
    // ...
}
Listening for input on a console app in a separate thread

Listening for input on a console app in a separate thread


By : sky112901
Date : March 29 2020, 07:55 AM
may help you . You might try setting Console.TreatControlCAsInput, which stops CTRL+C combination from even being processed at the OS-level. The caveat being that if the user truly does want to stop the application, they will either have to use CTRL+BREAK, or you will have to manually force the application to close after running your event handler.
What is the difference between a joined thread (blocks the calling thread until thread is terminated) and a normal funct

What is the difference between a joined thread (blocks the calling thread until thread is terminated) and a normal funct


By : user5089799
Date : March 29 2020, 07:55 AM
I wish did fix the issue. it will block when you call .join. between the creation of the thread and thread.join you can invoke many function. diagrammatically it looks like this:
Related Posts Related Posts :
  • What is the purpose of name mangling in C++?
  • C++ Why the void function is not working eventhough I called it?
  • How the pointer and the pointer's adress can share the same memory adress?
  • error C2220: warning treated as error - no 'object' file generated
  • exception of strcpy function in c++ console program
  • How to access USB barcode scanner data directly from USB port using C/C++ without driver
  • Under which circumstances will std::vector.clear() call a destructor?
  • Template partial specialisation and dependent names
  • best way to share data between c codes
  • C++ ignores if statement conditions
  • How to set the argv[ ] to be case-insensitive in a Win32 Console Application?
  • How to fix error "clang: error: linker (via gcc) command failed with exit code 1 (use -v to see invocation)"?
  • C++ How do I print a .txt file verbatim?
  • Creating two dimensional array of class
  • How do I correctly use COMMTIMEOUTS with OVERLAPPED IO mode reading from a Serial port
  • An assert macro which expands to static_assert when possible?
  • How to write a copyconstructor for a class which has HANDLE as a member in win32 c++ application?
  • C++ saving info such as tree in a file
  • Transforming an expression template tree
  • How to overload an operator with multiple parameters like a + b + c?
  • C++ 11 with Raspberry Pi performances
  • Make a C++ class look like a numpy array using swig
  • Postfix incrementer overloading for nested enumerated types
  • Is there a tidy way of associating metadata with functions in C++
  • QObject::installEventFilter(): Cannot filter events for objects in a different thread
  • LNK2005 error with Zxing C++
  • C++ Doubly Linked List with Pointers: Object of class isn't constructed properly
  • Using a random string generator in c++ constructor
  • What should I use instead of void as one of the alternative types in an variant?
  • C++ return value from multithreads using reference
  • How to connect multiple TCP IP clients to same server port using c++
  • Defaul compiler generates the reference operator (In C++)?
  • Unable to change directory time stamp after using FILE_FLAG_BACKUP_SEMANTICS
  • vector handling displaying output
  • WSAGetLastError returns WSAENOTSOCK - Cause?
  • C++: How to overload pow for user type?
  • C++ using arrays as multidimensional despite initalising it as 1D with pointer
  • How negate std::is_integral for use in tag dispatch?
  • Retrieve serial number from USB memory (Windows environment c++)
  • g++ error: invalid preprocessing directive #INCLUDE
  • C++ What is the std::for_each() function parameter type?
  • C++: Read individual lines from text file, sort words alphabetically
  • Saving 'this' address into a variable
  • c++ command line arguments in ubuntu terminal
  • Convert "Cartesian coordinates" to "polar coordinates with respect to user specified origin"
  • In what order are local scoped objects destructed?
  • How to use SDL_MapRGB with SDL 2.0
  • how compiler and interpreter work in case of array declaration
  • GSL integration behaves strange
  • Cropping an image with OpenCV and C
  • Find the last line in text file and select the first 10 char and print to a new file?
  • Created many CCSprits but when triggering ccTouchBegan gives the last one allways
  • seekp and seekg don't work with fstream
  • Taking input in Sublime Text 3
  • ld: -bind_at_load and -bitcode_bundle (Xcode setting ENABLE_BITCODE=YES) cannot be used together
  • C++ storing #define as std::string would give size as zero compile time
  • How to use static linking with OpenSSL in C/C++
  • What is the difference between a trap, an error, a failure and program abortion?
  • Dynamic members allocation in qt
  • How to reduce object file size when compiling for VxWorks 5.5.1?
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org