- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
This is the best argument in favor of occupational licenses for coders that I have seen in a while. Letting an inexperienced Jerry spawn threads is like giving beer and car keys to teenagers.
Admin
You're right. Every good gangsta knows to use a (g)lock. Anything else might jam up on ya.
Admin
The correct pattern is for the listening thread to add the message to a message queue. There would then be a pool of threads whose job it is to grab messages out of the queue and process them. They would start at the beggining of the application, and run until the application was terminated. More threads could be added, or existing ones could be removed if you wanted to scale the application at runtime. This is known as a threadpool. There are a pool of threads that run, all of which have the same job. If the message queue is empty, the threads would simply wait (hopefully on a semaphore, rather than a while(queue.isEmpty()); // do nothing). This way, you have a nice multithreaded app that doesn't thrash threads.
And, it can be scaled. Have a machine with tons of RAM, 8 CPUs, and is expected to handle many messages per second? Fine, increase the threads in the thread pool. Running this on a 486 with 64MB of RAM? Knock it down to one thread.
Admin
Because he used *no* form of data synchronization ... Pure and utter race conditions ... In running benchmarks and data transmission tests, I had results number in the hundreds of thousands of messages lost, and dozens of crashes because of simple things like trying to access the .front() on an std::queue, while another thread just did a final .pop(), causing access violations, etc.
Admin
The correct pattern is for a listener thread to spawn workers. That way you can track and properly shutdown worker threads. Another good pattern for this is for the listener to utilze a thread-pool to do the work, that way you aren't creating and destroying threads which is an expensive operation.
The wtf is that the implemented solution is backwards from the normal way people spawn workers, and that there is no easy way to track or throttle the threads that could be created. Additionally there is no protection of data from access by multiple threads, as was stated in other comments, that the guy was really clueless in general, and somehow got by cutting and pasting samples that he got from the huge system of tubes in the sky.
Admin
Excellent explanation, and this is essentially what I made the replacement application do. It took 2 months and 150,000 lines of code to make the replacement and a company standard C++ library that takes full advantage of the C++ Standard Library and Boost, but our customers are a whole lot happier now that the program doesnt crash, and they actually get all the alarms coming in.
Admin
Well d'uh. It's obviously a crappy implementation of the std::queue. It should either work or not...
Admin
yay! threads... raider i feel your pain man
even for the most wizard programmer with the best programming and design practices threads are absolutely nightmares on single processors, not to even get into multi-processor beasts - debugging them is a downright pain in the ass.
CAPTCHA: perfection
Admin
oh.. and btw - this is the way i deal with multiple connection servers
I loop and check if data is ready on the listen connection, if so i accept the connection and add it to my connection list. I then loop through the connection list checking for data ready and processing any communication if data is ready.
pseudo-code
while (1)
{
check-for-connections and accept them
loop on connection list
{
check for communications and process
}
check for shutdown command - if bound break
}
disconnect all clients politely.
If i ever get into a large enough connection volume situation I would create a pool of threads where each thread is assigned a certain number of connections to handle.
Admin
How is this not spawning a new thread for every message?
I mean, the method name is "PrecessIncomingMessage" not "AcceptConnection" or something like that. There's a comment there that says "now process the message" after the new listener is spawned.
What if messages arrive over a UDP socket? That would be fun.
Captcha = clueless.
Admin
Ha, that explination totally reminded me of school and the whole producer/consumers thread model. Now i totally remember how to do it.
Admin
I meant "ProcessIncomingMessage".
Admin
You must have overlooked the reply I put in that stated the code example Alex used, is just an example to illustrate the scenario, it is not an actual code snippet from the program.
Admin
because as he already said the code in the article is anonymized and isn't the real code - he's the submitter of the non-anonymized version
CAPTCHA: craptastic like your understanding of this thread
Admin
Eternal Father, Eternal One! Exceptions eternally? Absolute none! Doctor Bronner's 18-in-1 Pure Castile Soap to clean mind-body-spirit instantly uniting one! CAPTCHA "pacman"! ALL-ONE! ALL-ONE! ALL-ONE!
Admin
<offtopic>
I didn't even think anyone else even REMEMBERED CP/M. It was on the first computer I ever used- as a child. It was on what was considered the first 'portable' computer, only it weighed in at over 50 pounds. It was called an Osbourne. </offtopic>
Heh. Nothing like the smell of burning server as it tries to maintain thousands of threads over YEARS, (I did read that was a possiblity above right?) It could be a mecca where thousands of programmers come every day to actually watch the poor unix box weep tears of blood.
Admin
Yes, you read that right .. There was a potential for thousands of threads to be running, for potentially years.
Admin
This is a "for sure" wtf! Even without the multi-threading. I cannot imagine any MFC coder that wouldn't know VC code is windows platform specific. It is unfathomable to me.
Good one Brian!
Admin
Well I see that you enjoy the rich text editor...
YEP!
While MS-DOS was influenced by CP/M and CP/M was influcenced by UNIX, they are seperate systems with seperate aims.
MS Windows has absolutely no relation to the X Windowing System in any way.
They DO share the same basic concept. I was just showing how dumb the often accepted claim IS!
(And it's not called "X Windows" dammmit!).
OK, Sorry!
And WTF does the IEEE have to do with anything?
One "word".... P.O.S.I.X.!
PLUS, it is based on UNIX/C, and M/S HAS claimed to aspire to it.
Admin
Ever heard the expression "Ain't the sharpest tool in the shed" ? Hehe ... Honestly I'm really surprised the guy ever even got hired here ... I think he only squeaked by because the boss here is a VB developer, not C++, so he couldn't read "Jerry's" code and make any sense of it ... Once he saw the list of everything I found wrong with it "Jerry" immediately went on the "chopping" block .. It took him just over a year to make the app, and it sucked. I guess I put my own foot in my mouth when I made the replacement in just 2 months though, cause now that type of production is expected of me all the time :P
Admin
OK IEEE 1003 (aka POSIX) was ratified in the mid-80's. Microsoft was set up in the mid-70's. Microsoft predates POSIX by about 10 years.
http://en.wikipedia.org/wiki/IEEE_1003#History
http://en.wikipedia.org/wiki/Microsoft#1975.E2.80.931985:_The_founding_of_Microsoft
Not that it has any bearing. Yes UNIX was a very powerful server/workstation OS from the late 70's onwards. However, it is only comparatively recently that ordinary PC's have been capable of running UNIX well. In the past, less powerful, simpler OS's were needed. MS-DOS, MS Windows, CP/M, etc, were/are all OS's that were less powerful than UNIX purposely.
It has often been the case that an advanced technology is invented, but it is not practical for everyday purposes, and simpler, less powerful "copies" of the technology are invented and marketed.
Admin
I was thinking either that or tek-tips
Good places for syntax, bad for code.
Reminds me of an interviewee that brought in some code samples. He didn't even take the time to remove the credits from the comments (who were obviously not him)! Not to mention the samples were so old that they wouldn't even compile.
Admin
What would be the point of running UNIX on a workstation? Isn't the whole point to have a certailized mainfame and dummy terminals (TTYs).
That's just about as dumb as using Windows for a server! Ha ha ha
Admin
I've heard of a similar porting layer from Oracle to SQLServer....
Admin
That brought to mind a little story I was told during a job interview (I was the applicant):
A previous applicant had a very impressive resume but the interviewer (a fellow developer) wanted to validate it. He asked this question: "Describe the Internet".
After a pause and what appeared to be serious thought the response was, "Object Oriented"
It is obvious he was not considered for the job!
Admin
This story reminds me of a "real life" incident my son experienced during a C++ class in college recently.
Another student was accused, by most of the rest of the class, of stealing code from the Internet. It seemed obvious; he did not change any variable names, function names or even the comments.
Ultimately, the instructor reprimanded him for "COPYING COMMENTS". (not code)
Huh?
Admin
I've used a number of applications built with that product. It really works, too--it reproduces all the bugs, hangs, crashes, and crappy performance of early versions of Windows perfectly...
Admin
Tubes are objects...
Admin
An ancillary wtf is the disdain against people who see portions of a wtf above, but likely don't have a good grasp on threads and so are trying to understand the problems involved in the sample above.
Assuming people are stupid because they don't know what you know or what you expect "everybody who knows anything" to know is presumptuous at best. Why not take the opportunity to point out flaws in the code, not the observers, and give the world one less opportunity for another wtf.
Admin
The other month I reviewed some code from an inveterate Windows developer who is so reliant on VisualDevThingy that he is single-handedly porting our flagship Linux application suite to Windows just so he can use VDT to edit code. He uses cut+paste and automatic member/method name completion so heavily that even though he generates a huge number of typos, the code still compiles because the misspellings get copied to all the different places they need to be copied to for C++ to be satisfied. So far he hasn't gotten a virtual method name in a derived class wrong, but at this rate it's only a matter of time before one of these typos silently and subtly changes code behavior...
So I'd say that having a global variable (or an instance variable, or a base class instance variable, or a macro, or ...) named "messateid" is very likely indeed, even if Alex hadn't typo'd it. ;-)
Admin
I have a co-worker who is similarly in love with multi-threading. I was maintaining one of his apps that pretty much did everything by creating new threads. I got kind of fed up with the difficulties involved in stepping through the buggy code, and I asked him why he did this with a different thread. His response was "why not?"
I think people do this because they want to be able to say that their application is "multi-threaded", as if that makes it intrinsically a better application.
Admin
ARRRRRRRRRGGGGGGGGGGGGHHHHHHHHHHHHHHHHARRRGGGGGGGGGGHHHHHHHHHHHHHHHHHHHHARRRRGGGGGGGGGHHHHHHHHHHHHHH!
CodeProject is one of my favourite sites for getting advice!!!!!!!!! ARRRRRRRGGGGGGGGGHHHHHHHHHHHHH!!!!!!!!!!!!!!
MANNNNNNNNNNNNNN!
What chance does a young man, having no formal training, being self taught in all aspects have when his favourite coding site is rubbished by his favourite "how not to do things" site!
ARRRRRRRRRGGGGGGGGGHHHHHHHHHHHHHHHHHHHHHH!!!!!!!
Admin
I find that the site in question is not that bad if you just look at what they are basically doing, then close the page. It can give you a start in the right (although sometimes wrong) direction. If I'm using a feature I've never used before then codeproject does help, but I also use it along side other code examples doing the same thing.... After that it's pretty obvious what's kosher and what's not.
Admin
Threads are slow. Any OS that actually has a decent thread implementation dies horribly in the marketplace because no software can be ported to or from that OS because the prevailing assumptions are so different. Even if you have a decent thread implementation, the overhead of constantly grabbing and releasing locks that you may or may not need to protect critical sections eats away at your performance. Sure, if you have a decent thread implementation, you might rearchitect your software to take advantage of it and replace the performance drain with performance gain--but who these days redesigns their software just to change platforms? It's about as hard as trying to get people to rewrite their software for BeOS.
Threads are decades old, but decent, commercially practical implementations of threads (outside of the embedded market) have been available for only a few dozen months.
The Boost library in particular, like many C++ libraries, aims to have zero overhead (i.e. the CPU spends zero clock cycles doing anything that isn't absolutely necessary for performing the documented tasks in the spec). Not "low" or "minimal" overhead--zero. Of course they'll never actually achieve that goal, but adding thread support is a giant step away from it.
Qt, on the other hand, is a popular C++ library full of wonderful thread-safe code. It also allocates multiple objects on the heap when you construct a simple list iterator--so expensive this is, that Qt itself avoids using list iterators in special cases like lists of one element. It has a very different notion of "high performance" than I do...
Admin
Can I get a WTF for how a large percentage of the recent posts have all been about new employees who have been on the job for 3 days?
Is 3 days some magical number for a coder? Or is it that it takes 3 days to get a computer and cubicle set up and get all the right passwords and the WTFs just jump out at good coders like they have on WTF-magnetic undershorts?
Admin
Some people just can't be taught ... I explained all the problems I found with his code, to him first ... Made recommendations, gave him links to standard specifications ... Even offered to let him borrow some of my library of C++ books, he was one of those "My code is the best, I don't care what you think" people ...
Admin
Actually it took a day and a half for my desk and development machines to get set up ... But atleast I got the laptop the first day, loaded with the guys source code that I took home to review to learn since I figured we've both be working on the same code.
Admin
Was it this website?
Admin
I think the overall worst thing about it was he was using seemingly random code snippets and just slapping them in a production application that is a major part of a Security System used in galleries, museums, banks, etc ... He knew almost nothing about the code he used other than that the website said it worked ... About the only thing I can really give him credit for is being good at taking random code, hacking it together and getting it to work just good enough that the boss didn't fire him for over a year ...
Admin
IIRC, in Java up to version 1.3, one-thread-per-connection (actually, two threads if you want bidirectional communication) was the only way you could code such applications. No non-blocking system calls, no select or poll...but the runtime and JIT mechanisms were supposed to make threads really cheap and fast.
Admin
What OS are you using where creating a thread doesn't cause a context switch...?
Admin
And people wonder why daemons written in older Java were so slow and such resource hogs :) ... The only way I'd ever do one thread per connection is if there would be less than 10 connections on a single or dual core/cpu system and the connections have to be synchronous (Including ethernet, RS232, USB, etc, connections), otherwise I use a connection pool and if I feel it necessary, a thread pool to manage the connections.
Admin
I dunno about mainsoft, but I myself did port big portions of MFC to run under unix, and everything went well. So you can use MFC if you really like and want. I certainly wouldn't do it with a greenfield project or one that is known to be ported right at the start, as there are better options today.
For this whole WTF, I can't make much sense of it. Well, the guy was not really u to the task; is that rare, fun or news-worthy? And yet another management took someone at face value without doing the most basic evaluation. Guess they deserve it then.
Admin
The WTF isn't who HE was, it was what he was DOING :)
Admin
Admin
2 months and 150 lines of code... I hope that includes what was in the third party stuff, otherwise with no days off and working 10 hours a day you'd be writing a little over 4 lines of code a minute. That's not only fast typing but fast thinking. And I call it suspect.
Admin
Sure. You pick up a chop who ever in his life worked on VC++/Win, single threaded. And put him on a task writing multithreaded unix code. Without mentoring and supervision too.
Having done that I would NOT expect anything but code like presented here. And would have to blame myself. Like the king explained to the Little prince about issuing only *reasonable* orders...
Admin
You mean, like Linux?
Admin
Erm ... I don't what what kind of code you write or have seen, but I'd hardly call that a lot ... It included the application itself, and the library I wrote for the company ... Generally I worked a minimum of 10 hours a day during the weekdays, and quite often more than that on the weekends ... Thats what happens when it becomes crunch time. Its was more or less a situation where I brought up all the faults in the previous app, and decided I could get it done faster and make it far more efficient if I rewrote it from scratch than to try and repair everything wrong with the original. Ever had one of those moments where you sat down, got an idea, and started writing code, and before you knew it the entire work day had gone by and you had generated 10000+ LOC ?? ... Happens quite often, especially on new projects where the entire design and purpose of the app is already laid out and it boils down to just writing the code.
Admin
Thats not exactly the case ... He said he could do it, he said he could make it work, etc, etc ... Its not like the boss said "Here, do this, and do it right, I don't want to hear anything more about it until its done" ... As close to everything as possible gets worked out in whiteboard sessions .. The problem isnt so much that he lacked the experience in developing for *nix as he lacked the knowledge of even the slightest differences between Win32 and *nix.