Click here to Skip to main content
15,880,796 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: I fear and loathe writing multithreaded code Pin
honey the codewitch21-Mar-20 2:55
mvahoney the codewitch21-Mar-20 2:55 
GeneralRe: I fear and loathe writing multithreaded code Pin
Greg Utas21-Mar-20 3:13
professionalGreg Utas21-Mar-20 3:13 
GeneralRe: I fear and loathe writing multithreaded code Pin
Jörgen Andersson21-Mar-20 6:53
professionalJörgen Andersson21-Mar-20 6:53 
GeneralRe: I fear and loathe writing multithreaded code Pin
Greg Utas21-Mar-20 7:12
professionalGreg Utas21-Mar-20 7:12 
GeneralRe: I fear and loathe writing multithreaded code Pin
kalberts21-Mar-20 12:04
kalberts21-Mar-20 12:04 
GeneralRe: I fear and loathe writing multithreaded code Pin
Greg Utas21-Mar-20 13:12
professionalGreg Utas21-Mar-20 13:12 
GeneralRe: I fear and loathe writing multithreaded code Pin
Rick York21-Mar-20 8:06
mveRick York21-Mar-20 8:06 
GeneralRe: I fear and loathe writing multithreaded code Pin
kalberts21-Mar-20 9:39
kalberts21-Mar-20 9:39 
The strong focus on threads indicate that our minds are basically rooted in the sequential, one-at-a-time, mindset. Which also means that we have not really grasped object orientation. The sooner we can detach our minds from this ordered first-things-first-then-the-next way of coding, parallelism becomes much easier.

In my early life as a programmer, I became familiar with two concepts that are essentially forgotten today:

An APL "workspace" is like a sand pit were you add your objects - data arrays, functions, ... - dynamically. Or remove them. The objects interact, like real-world objects. In APL, the interaction is "classical", by calling other functions in a traditional way. But the complete autonomy and independence of the objects when there is no interaction is only partially continued in OO languages of today.

The other concept came with GUIs, popularized by the classic (1984) MacOS and Windows: First, that everything that happens is in the form of atomic events. Handling of an event is (conceptually) instantaneous and brings the object from one consistent, well defined state to another consistent, well defined state.
Second, interaction betwen objects is by message passing, rather than by function calling. All normal messages are queued, non-blocking, asynchronous. While APL objects are definitionwise completely independent of each other, messages may (if used throughout) provide a similar run time independence of of each other. You completely avoid deadlocks: Sure, object A may wait indefinitely for a reply message from object B after sending a request, but that in no way prevents object A from handling all other kinds of messages. You need no locking; shared data resources are modelled as objects receiving request messages, either causing changes in its internal data structures or a reply message with data values. No locking is needed; the message queue provides the sequential order of atomic accesses.

What use are threads, really? If every object interacts with every other object through messages, and has an autonomous right to determine how it will handle them but do it one by one in an atomic fahsion, and is responsible for its own transition between consistent states, the need for threading within each object is very limited. If everything is modelled as objects, all interaction as messages, there is little need for thread mechanisms outside the objects, too.

Each independently executing object must "run on a thread", it has its own life. But that doesn't require threads as we use them today, with synchronization and resource ownership, and lots of bells and whistles, in some thread models also under the management of a higher level "process", wich may imply a more complex, two-level resource ownership model. If all resources are objects, owning itself, you don't need it.

When an object is waiting for the next message, all state information is preserved in its data members. Its runtime stack is empty, its program counter well defined - think of it as similar to an interrupt handler! The message handling system activates the handler by delivering a message (with appropriate queueing mechansism). To utilize several CPU cores, the message handler may run one low-level thread per core, and dispatch messages to as many objects as it has cores/threads. You can do the same using higher-level threads, but at a higher cost, and maybe less flexibility (e.g. if there is an ownership relationship from thread to object). Note that the total stack requirements are limited by the number of messages actively being processed simultaneously, one per core, each no deeper than that needed for processing a single message in an atomic operation.

This is not how we were taught to code in the pre-threading, pre-OO days, but neither threads nor OO has essentially modified our mental models: We still have not learnt to see objects as autonomous, and although we have multiple sequences of operation, they are still well-ordered, with loops but essentially still sequential.

Yet, you can create quite autonomous objects in most OO languages. You can encapsulate data in objects. Your "message" mechanism may be implemented as a function call (the only public one offered by the object), that returns nothing and conceptually returns immediately. The object may alternate among consistent, well defined states. Your thread worries would vanish. You would never more see a deadlock. Some resource requirements would go significantly down (e.g. for stack space).

I am trying to be a realist, though: There are no signs of neither autonomous objects nor message (/event) driven models entering into programming courses; they are too busy teaching students how to create deadlocks and racing conditions using threads Smile | :)
GeneralRe: I fear and loathe writing multithreaded code Pin
honey the codewitch21-Mar-20 10:34
mvahoney the codewitch21-Mar-20 10:34 
GeneralRe: I fear and loathe writing multithreaded code Pin
honey the codewitch21-Mar-20 10:38
mvahoney the codewitch21-Mar-20 10:38 
GeneralRe: I fear and loathe writing multithreaded code Pin
harold aptroot21-Mar-20 22:50
harold aptroot21-Mar-20 22:50 
GeneralRe: I fear and loathe writing multithreaded code Pin
honey the codewitch22-Mar-20 4:07
mvahoney the codewitch22-Mar-20 4:07 
GeneralI suddenly got back into Espresso again. Pin
OriginalGriff21-Mar-20 0:46
mveOriginalGriff21-Mar-20 0:46 
GeneralRe: I suddenly got back into Espresso again. Pin
lopatir21-Mar-20 0:49
lopatir21-Mar-20 0:49 
GeneralRe: I suddenly got back into Espresso again. Pin
glennPattonWork321-Mar-20 0:50
professionalglennPattonWork321-Mar-20 0:50 
GeneralRe: I suddenly got back into Espresso again. Pin
OriginalGriff21-Mar-20 0:54
mveOriginalGriff21-Mar-20 0:54 
GeneralRe: I suddenly got back into Espresso again. Pin
glennPattonWork321-Mar-20 0:56
professionalglennPattonWork321-Mar-20 0:56 
GeneralRe: I suddenly got back into Espresso again. Pin
honey the codewitch21-Mar-20 1:44
mvahoney the codewitch21-Mar-20 1:44 
GeneralRe: I suddenly got back into Espresso again. Pin
DRHuff21-Mar-20 7:05
DRHuff21-Mar-20 7:05 
GeneralTrouble with Teams... Pin
glennPattonWork321-Mar-20 0:34
professionalglennPattonWork321-Mar-20 0:34 
GeneralRe: Trouble with Teams... Pin
lopatir21-Mar-20 0:47
lopatir21-Mar-20 0:47 
GeneralRe: Trouble with Teams... Pin
glennPattonWork321-Mar-20 0:49
professionalglennPattonWork321-Mar-20 0:49 
GeneralRe: Trouble with Teams... Pin
lopatir21-Mar-20 1:12
lopatir21-Mar-20 1:12 
GeneralNot a COVID-19 death Pin
OriginalGriff20-Mar-20 21:01
mveOriginalGriff20-Mar-20 21:01 
AnswerRe: Not a COVID-19 death Pin
lopatir20-Mar-20 21:17
lopatir20-Mar-20 21:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.