Click here to Skip to main content
15,868,098 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: Electron? Pin
raddevus28-Feb-21 12:35
mvaraddevus28-Feb-21 12:35 
GeneralAnd when you depend on multithreading to be unpredictable, it isn't! Pin
honey the codewitch27-Feb-21 13:10
mvahoney the codewitch27-Feb-21 13:10 
GeneralRe: And when you depend on multithreading to be unpredictable, it isn't! Pin
Marc Clifton27-Feb-21 13:15
mvaMarc Clifton27-Feb-21 13:15 
GeneralRe: And when you depend on multithreading to be unpredictable, it isn't! Pin
honey the codewitch27-Feb-21 13:15
mvahoney the codewitch27-Feb-21 13:15 
GeneralRe: And when you depend on multithreading to be unpredictable, it isn't! Pin
Greg Utas27-Feb-21 13:38
professionalGreg Utas27-Feb-21 13:38 
GeneralRe: And when you depend on multithreading to be unpredictable, it isn't! Pin
Gary R. Wheeler27-Feb-21 13:31
Gary R. Wheeler27-Feb-21 13:31 
GeneralRe: And when you depend on multithreading to be unpredictable, it isn't! Pin
honey the codewitch27-Feb-21 13:45
mvahoney the codewitch27-Feb-21 13:45 
GeneralRe: And when you depend on multithreading to be unpredictable, it isn't! Pin
Member 133016791-Mar-21 20:33
Member 133016791-Mar-21 20:33 
Quote:
Absolutely true. To that end my library provides you access to *none* of those. Laugh | Laugh | :laugh: Seriously though, it offers you a message passing system in the alternative


Message passing is probably the only way to rein in the complexity of threads. Message-queues is the easiest message-passing interface you will find.

I recently did a simple message queueing library (based on pthreads) for a personal project and still managed to get the system to deadlock eventually (only happened on Windows due to different scheduling algorithm[1]). After fixing it I realised that there was no value in a linked-list queue.

I implemented my message-queue library as a double-linked list, so that any thread taking a message off of the queue does not block any thread trying to put a message onto the queue. My intention was that threads removing messages from the queue would never hold a lock that threads posting messages to the queue would need (and vice versa).

Unfortunately all threads still have to lock the entire queue just to check if (head==tail) in case there is only one item in the queue (then that item is both the head and the tail).

This is the stupid way of doing this. Don't do what I did. Instead, do one of the following:

1. Use a fixed-length message queue (either fixed at runtime or fixed at compile-time). This removes quite a lot of the unnecessary complexity; you're going to lock the entire queue for any posting or removal, but you're going to do that anyway with linked-lists too, so no big deal.

2. Address the fixed-length queue using modulus of the length (with appropriate locks); this gives you a circular buffer with no if statements.
C++
message_t messages[BUFLEN];
...
messages[index % BUFLEN] = new_message;   // Posting a message
...
message_t mymessage = messages[index % BUFLEN]; // taking a message off the queue

The problem with doing this is that it would automatically drop old messages (which, strategically, may be something you want, actually). Also, if you're not using C++ (no smart pointers) that's going to be a memory leak.

3. If your target platform and implementation allows (which it will), use #defines to define a CMPXCHG macro that expands to the assembly of the cmpxchg opcode. You can then use that for a superfast single lock with a sleep in nanoseconds or milliseconds that gradually decrements by a fixed amount so that no thread will starve. Doing this using your platforms mutex could be a lot slower than you think.

And, in case you're wondering, I am intending to update my implementation of a message queue to use fixed-length queues (probably settable at by the caller at runtime, with a sane default set at compile-time).

I'm not so happy about the cmpxchg thing as my queue library is supposed to be working on all pthreads platforms, and I'll need an implementation of cmpxchg for each platform. Not too much of a problem for things like ARM7 and later devices as I'm an embedded dev and am surrounded by various boards, but still a problem for platforms I don't have easy access to (one of my open source projects recently received a bug report for a bug that occurs on Z/OS).




[1] Which is why it's important to test code on multiple platforms, even if you never intend to ship on any of them - different platforms shake out different bugs.
GeneralRe: And when you depend on multithreading to be unpredictable, it isn't! Pin
honey the codewitch2-Mar-21 2:58
mvahoney the codewitch2-Mar-21 2:58 
GeneralRe: And when you depend on multithreading to be unpredictable, it isn't! Pin
Member 133016792-Mar-21 3:20
Member 133016792-Mar-21 3:20 
GeneralRe: And when you depend on multithreading to be unpredictable, it isn't! Pin
honey the codewitch2-Mar-21 3:24
mvahoney the codewitch2-Mar-21 3:24 
GeneralRe: And when you depend on multithreading to be unpredictable, it isn't! Pin
Member 133016792-Mar-21 3:38
Member 133016792-Mar-21 3:38 
GeneralRe: And when you depend on multithreading to be unpredictable, it isn't! Pin
honey the codewitch2-Mar-21 3:54
mvahoney the codewitch2-Mar-21 3:54 
GeneralRe: And when you depend on multithreading to be unpredictable, it isn't! Pin
Member 133016792-Mar-21 10:58
Member 133016792-Mar-21 10:58 
GeneralRe: And when you depend on multithreading to be unpredictable, it isn't! Pin
honey the codewitch2-Mar-21 11:02
mvahoney the codewitch2-Mar-21 11:02 
GeneralRe: And when you depend on multithreading to be unpredictable, it isn't! Pin
Member 133016792-Mar-21 11:17
Member 133016792-Mar-21 11:17 
GeneralRe: And when you depend on multithreading to be unpredictable, it isn't! Pin
honey the codewitch2-Mar-21 13:22
mvahoney the codewitch2-Mar-21 13:22 
GeneralRe: And when you depend on multithreading to be unpredictable, it isn't! Pin
Member 133016792-Mar-21 13:28
Member 133016792-Mar-21 13:28 
GeneralRe: And when you depend on multithreading to be unpredictable, it isn't! Pin
honey the codewitch2-Mar-21 13:30
mvahoney the codewitch2-Mar-21 13:30 
GeneralRe: And when you depend on multithreading to be unpredictable, it isn't! Pin
Member 133016792-Mar-21 13:39
Member 133016792-Mar-21 13:39 
GeneralRe: And when you depend on multithreading to be unpredictable, it isn't! Pin
honey the codewitch2-Mar-21 14:26
mvahoney the codewitch2-Mar-21 14:26 
GeneralRe: And when you depend on multithreading to be unpredictable, it isn't! Pin
Member 133016792-Mar-21 19:59
Member 133016792-Mar-21 19:59 
GeneralRe: And when you depend on multithreading to be unpredictable, it isn't! Pin
honey the codewitch3-Mar-21 0:41
mvahoney the codewitch3-Mar-21 0:41 
GeneralRe: And when you depend on multithreading to be unpredictable, it isn't! Pin
Member 133016793-Mar-21 0:46
Member 133016793-Mar-21 0:46 
GeneralRe: And when you depend on multithreading to be unpredictable, it isn't! Pin
Greg Utas27-Feb-21 13:43
professionalGreg Utas27-Feb-21 13:43 

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.