Write a tennis game in console app. Program must monitor for user input while a ball is moving. Without multithreading it's not possible to monitor for user input and do something more at the same time.
I've done this a month or two ago and i was always losing against computer because i was too stupid to make my computer stupid...
But then you aren't really doing them simultaneously. It just seems like it because the processor is so fast. That's like saying just because someone can, for example, release the clutch in a car, and then change gear and apply the clutch again, with seemingly no pause in between, they're simultaneously doing all those things at the same time.
Ok that was a hideous example but you get my point. You aren't really doing two things at once, the CPU is just doing them so fast that it seems that way.
That's true. But threads only get you simultaneous execution on a multi-core processor.
Perhaps my intention was unclear. I am not a proponent of threaded programming in most cases. Nine out of ten times I hear people say they needed threads for some reason, in reality they weren't really necessary; in fact there were other single-threaded solutions equally viable.
Null's example above is one such case. Yes, it is quite true that if you want simultaneous execution, you need threads and a multi-core CPU, but in reality, as you stated, chrisname, those operations can be done so quickly so as to appear to be concurrent. If there is no observable difference to the user, then there is no need for a thread.
And I merely think it would be useful to know how to multi thread, but couldn't think of an example to do. i guess perfectly simultaneous dialog boxes every third second which annoy the hell out of the user would be easy enough.
Sarcasm not possible on the Net? I beg to differ. Look:
[sarcastic]You were right.[/sarcastic]
See? :-)
Perfect multithreading use: When you must block execution until something happens. I'm a Windows guy, so forgive me Unix/Linux lovers if this is possible in single-thread apps under your [sarcastic]marvelous[/sarcastic] operating systems. (haha, see? I did it again. LOL!!)
Seriously, though here's an example: An application that spawns an external application and must wait for it to finish, BUT you want to do some other stuff while waiting. You need to use WaitForSingleObject() on the process handle, and that blocks the thread until the monitored process ends.
Another example: Pipes. The ReadFile() function blocks execution until data is written to the pipe. Same goes with WriteFile() for writing to them.
I prefer Linux to Windows. I downloaded an Ubuntu live CD a few feeks ago and I've barely touched Windows since. I'm using it now, though. The partitioner is good and easy to use and I'm making a partition to install Linux Mint 7.
How's about a server/client game program? It's probably possible to create a single-threaded version of of a multiplayer game (in fact I'm almost certain), but reading sockets without waiting on them or utilizing timeouts sucks to implement as far as I can see. By the way, which Linux IDE are you using? I prefer KDevelop.
I don't think I could do anything with sockets as yet. I red "Beej's tutorial to socket programming" or whatever it's called... and it was quite confusing. I can do socket programming in Python, it's literally
[code]import socket
irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
port = 6667
server = "ic.inter.net"
irc.connect((port, server))
irc.send("JOIN #channel")
[code]
to join an irc server on "inter.net" and join a channel named "#channel".
But socket programming in C++ is difficult =[
Just use select() with a zero timeout. Alternatively you can set the socket fd to non-blocking mode and then read will not block if no data is available.