You're asking about non-blocking IO, and the question
is not at all trivial.
For many forms of multithreading, there's no problem.
One thread can perform blocking IO, through scanf() or
whatever, and the other threads can keep running while
the first thread waits. That's a major reason for
using threads in the first place.
Cheap Threads isn't like that. If one thread blocks
for any reason, all threads are blocked. If you want
to perform non-blocking IO, you'll have to use some
platform-specific way to do it, because Standard C
doesn't provide this capability.
I do most of my Cheap Threads development on an
ancient Borland compiler. Borland provides a kbhit
function that tells you whether there is a keypress
waiting in the keyboard buffer. By using that
function, I can read the next keypress (potentially a
blocking operation) only when I know there's
a keypress to be read. At one time Microsoft's
compiler provided a similar kbhit function, but I
don't know about their current offerings.
One of my main testbed programs has several input
fields, each run by a separate thread. Another thread
checks the keyboard buffer and, if it detects a
keypress, passes it in a message to whatever input
field currently has focus. It's actually a bit more
complicated than that, but the point is that the user
interface is event-driven, and every keypress is an
event. This approach is rather more tedious than
using scanf(), especially if you want to handle
backspaces, delete keys, tabs, and arrow keys
sensibly, but it does enable background threads to do
work between keypresses.
Depending on what's available on your platform, you
may be able to use a similar approach. I don't know
of any Unix equivalent to kbhit, but maybe someone
else in the group does. Otherwise you're probably
better off using some other form of multithreading
such as POSIX threads, or maybe a separate process for
the background work.
Scott McKellar
http://home.swbell.net/mck9/ct/
xcelcior <blaze01211983@...> wrote:
> GREETINGS
> forgive me but I'm kinda new to multithreaded
programming d=)
> I'm just wondering how to implement a user interface
in a
> multithreaded programming environment or using cheap
thread.
> For example i have 2 threads. Thread A runs the user
interface while
> Thread B does other things. I mean how do i get the
input from the
> user through thread A since the two threads are
switching controls.
> I cant use scanf since it will wait for user input
at the same time
> suspending the execution of the other thread Thread
B.
>How can I run thread B on the background while thread
A gets user
> input?
> Forgive me if my question is a bit trivial =)