![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#51 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
What compiler are you using? What error messages do you get?
|
|
|
|
|
|
#52 | |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: here
Posts: 114
Rep Power: 0
![]() |
Quote:
And, as I'm sure you've taken the time to research threads, you understand their functionality. What do you think? A thread, simply put, allows you code to execute in a non-linear way. It allows for the appearence of simultaneous execution of two seperate sections of code. So, in your case, you could be executing a loop that is waiting for a message and one that sends a message (when appropriate) at the 'same time'. However, you now must deal with shared variables, mutexes and race conditions (among other things). You can do what you want (well, acheive the same result) without using threads. It wont be because of how you use select. Your design details what is required. If you wish to avoid threads, a redesign may be in order. And, as an aside, I would suggest you NOT use the timeout feature of select. 30 seconds worth of non-interaction isn't unheard of. You don't (at least I don't) want an error in that case. If I'm not mistaken, a chat window may stay open indefinately, if the user so desires.
__________________
"...and though our kids are blessed their parents let them shoulder all the blame." - The Quiet Things That No One Ever Knows [BrandNew] |
|
|
|
|
|
|
#53 |
|
Highly Adaptive Penguin
Join Date: May 2005
Location: United States
Posts: 249
Rep Power: 4
![]() |
i have researched threads, and decided that select would be a better choice to use because of the complexity of threads. what do you mean by design details what is required?
|
|
|
|
|
|
#54 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: here
Posts: 114
Rep Power: 0
![]() |
What I mean is, what you design dictates what you must create.
If you decide that threads will be too much to overcome for your project and your original design was best implemented with threads, you have two choices: Redesign --or-- muddle through painfully. I was suggesting that the former may be in order here.
__________________
"...and though our kids are blessed their parents let them shoulder all the blame." - The Quiet Things That No One Ever Knows [BrandNew] |
|
|
|
|
|
#55 |
|
Highly Adaptive Penguin
Join Date: May 2005
Location: United States
Posts: 249
Rep Power: 4
![]() |
i think ill try threads...is there a certain threads class that i use?
|
|
|
|
|
|
#56 |
|
Professional Programmer
Join Date: Mar 2005
Location: Glasgow, Scotland
Posts: 314
Rep Power: 4
![]() |
I think what messes with people with select() is the slightly itchy way you have to specify the file descriptors to watch. It's not really as tough as it looks, but you do have other options.
One 'soft option' which is OK for simple programs is to poll using non-blocking IO. You can use blocking IO for the send(), but investigate setsockopt() and set recv() to non-blocking; in cases where there's nothing to read you'll get an error return from recv(). Just make sure you don't end up thrashing, repeatedly polling without any delay between checks. Using signal-driven IO is also often easier than using select() where your application is nice and simple. Not sure how that translates to Windows, though; I suspect it doesn't. Quite early on, MJordan2nd suggested threads ... weirdly, this is actually one of the easiest and most intuitive methods. Threads are separate lightweight processes within a single process, and allow your program to do more than one thing at a time. The way you can do this sort of chat program with threads is just create one that always reads the socket and prints everything it receives, and another that always reads the terminal and writes whatever it finds to the socket. The two of them don't need to interact (though you can occasionally get in a bit of a mess on the terminal if you don't do any synchronisation between the two), so this is dead easy; when one is blocking on recv() the other is still free to read from the user. You do have to learn how to use threads to do it this way, but that'll be useful down the line anyway so I recommend it. |
|
|
|
|
|
#57 |
|
Highly Adaptive Penguin
Join Date: May 2005
Location: United States
Posts: 249
Rep Power: 4
![]() |
thanks...i dont understand how to create threads. how do you make programs do multiple process's at a time?
|
|
|
|
|
|
#58 |
|
Professional Programmer
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4
![]() |
You really should be asking Google these questions. The answers are out there, and succint.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|