LWN.net Logo

A look at C++14, part 1

A look at C++14, part 1

Posted Mar 31, 2013 9:31 UTC (Sun) by simlo (subscriber, #10866)
In reply to: A look at C++14, part 1 by jwakely
Parent article: A look at C++14, part 1

What I am opposed to is multi threading!

My experience have shown me that it is the most misunderstood and amused feature in programming. Everybody learn about it in school and think they have to use it and impose it on everybody else. Once a program, framework or API relies on multithreading it is impossible to avoid. But in 95% of the cases the multi threading was complete unneeded if just the API were available in non-blocking versions.

Take Java: when you implement a TCP protocol you have a thread for receive, a thread for transmit and a timer to detect inactivity. Using select() you can do this in one thread avoiding a lot potential race conditions. I have seen instabilities and odd problems popping up in old Java programs due to this.

When using this model in C++ you get into further problems due to missing garbage collector.

In the beginning UNIX user space did not have multi threading. It is mostly a bad habit coming from other OS'es lacking proper processes.

I am not totally against multi threading: realtime system and system with heavy scale able algorithms. But 95% of programs only becomes unstable and much harder to maintain due to threads.


(Log in to post comments)

A look at C++14, part 1

Posted Mar 31, 2013 13:59 UTC (Sun) by Cyberax (✭ supporter ✭, #52523) [Link]

I haven't seen real Java apps using separate threads to receive and send data. That's simply stupid in most cases. However, I did see asynchronous servers die under a high load with all but one CPU completely idle.

A look at C++14, part 1

Posted Mar 31, 2013 21:08 UTC (Sun) by simlo (subscriber, #10866) [Link]

Well, as I work with _real_ Java programs (written before select() came to Java), I know it is needed.

For instance: I have some data I have to send to different clients via TCP sockets. Naive solution: Loop through the clients and perform write() on each socket. Problem: If one client is not taking data the write() operation will block halting data for any other client as well.
Therefore I have to make a thread in which to execute write() for each client. And a message queue for each client to get data from the data producing thread(s) to the write thread.
Similarly, the read option is blocking so I have to have a receive thread per client.
And all this is hard to make work in all cases where clients can close the connection, the link is lost or what ever where the different thread are in different states.

On a proper OS (i.e. UNIX) in C++, I can use select() and therefore contain everything in one thread and have a clear picture of my possible states.

A look at C++14, part 1

Posted Apr 2, 2013 4:28 UTC (Tue) by akeane (subscriber, #85436) [Link]

>What I am opposed to is multi threading!

Hooray, finally, a sane voice in the wilderness!

>the most misunderstood and _amused_ feature in programming.

This may be a typo, but yet it is brilliant somehow, I have always regarded pthreads as some evil entity laughing maliciously as some hapless programmer stumbles into it's wake...

If only I had a dollar for every sleep() I have seen to try to push that pesky race condition further into the sunset, and the amount of times I have seen assumptions being made about thread startup time and executions.

Or the nested locks, Oh God, the nested locks!

But back to pipelines in C++, surely the UNIX way would be to write some kind of general program which you could tell the system to take the output of one program and "pipe" it into another. I would call it:

/bin/M*A*S*H

Merry Easter

A look at C++14, part 1

Posted Apr 2, 2013 8:25 UTC (Tue) by jwakely (subscriber, #60262) [Link]

> But back to pipelines in C++, surely the UNIX way would be to write some kind of general program which you could tell the system to take the output of one program and "pipe" it into another.

Shameless plug: http://pstreams.sourceforge.net/

Copyright © 2013, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds