Ideas for a multithreaded program?

I want to learn how to write multithreaded programs; I just can't think of a use to put them to that I could do.

I've been trying to think of something that would benefit from being multithreaded as opposed to single-threaded; but I can't think of anything.

What could I do? As I've never touched on multithreading before, it'd have to be something relatively simple.
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...
Lol :P

I'll try it later.

... I just can't think of a use to put them to that I could do.


Now that's my kind of thinking!


Without multithreading it's not possible to monitor for user input and do something more at the same time.


Not true; it depends on your platform and libraries. In Unix it is very easy to write a simple game loop:


while( !gameOver )
{
MoveBall();
CheckAndProcessUserInput();
}


You just need a non-blocking way to check for user input (which is trivially doable in unix).
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.

Also, irony doesn't work on the internet...
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.

(BTW, where was the irony in my response?)
Actually, irony is not any more ambiguous on the Internet than it is IRL.
What doesn't work on the Internet is sarcasm.
Where was the irony [...]?

My mistake.

Also I have a multi-core processor :p

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.
Last edited on
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.
No, that's not sarcasm. Sarcasm would be "Oh, yeah, you were right!"
oic what u did thar.

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.
Ah, I see.

I mentioned linux/unix because I know nothing about Windows programming.

That it exists in unix implies that it is at least possible for it to exist in Windows, which is all that I was (implicitly) saying.
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'm not sur haven't got into that.

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 =[
Alaric:

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.
Topic archived. No new replies allowed.