terminate thread waiting on cin

M

Mr Dyl

I'm using Boost to run a couple of threads, one of which reads commands
via std::cin and another receives them through a socket. It's entirely
possible that during the life of the app, all commands will be sent
through the socket and the thread watching cin will never "see any
action". So the problem is, how do I terminate this thread that waits
for keyboard input?

Unfortunately, Boost::thread doesn't yet allow me to force the thread
to terminate. Even after I exit main(), the app doesn't die until
something is entered via cin. I'm trying to keep the code
cross-platform compatilible so I'd like to avoid any OS-specific
gobledigook.

Any ideas?

Thanks in advance!
 
D

Diego Martins

Are you sure Boost::thread does have nothing similar to
pthread_cancel() ?

This is the posix way to cancel threads, even the system-call blocked
ones
 
C

Calum Grant

Mr said:
I'm using Boost to run a couple of threads, one of which reads commands
via std::cin and another receives them through a socket. It's entirely
possible that during the life of the app, all commands will be sent
through the socket and the thread watching cin will never "see any
action". So the problem is, how do I terminate this thread that waits
for keyboard input?

Unfortunately, Boost::thread doesn't yet allow me to force the thread
to terminate. Even after I exit main(), the app doesn't die until
something is entered via cin. I'm trying to keep the code
cross-platform compatilible so I'd like to avoid any OS-specific
gobledigook.

Any ideas?

Nothing definitive. Terminating a thread is generally an extremely bad
idea. You could close the file handle/file descriptor of cin/STDIN, and
it should stop the read. But that's OS specific.

Assuming just one thread can use cin, why not run the ui in the main thread?
 
M

Mr Dyl

Yeah, I know killing the thread with brute force isn't a good idea
which is why I was hoping there'd be an elegant solution.
Unfortunately Boost doesn't have any equivalent to pthread_cancel() as
Diego was asking (although the hint that it is forthcoming).

I don't want cin in my main thread because the main thread has to be
able to respond to input from other sources (sockets and files).
Basically there are 3 ways to control the main thread, of which cin is
just one.

It's looking like I'm going to have to hack this somehow :(
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top