in reply to Re^8: OT: Why does malloc always give me 24?
in thread OT: Why does malloc always give me 24? [SOLVED]
So, against that backdrop, I usually choose not to have any synchronization built-in with my data structures or objects. Instead, I plan out very specific APIs for the data that truly needs shared, and put the mutexes in the API functions that access it. Then, don't share anything that doesn't need shared.
The shared data structures like queues (where one thread is sending a message to another) can be done using pipes just like how you would do them if you had separate processes. It maybe isn't as efficient as can be done with special threading functions, because it involves a syscall to read and write, but it plays nicely with the external events you might also be waiting on, like socket data.
It isn't a simple example, but I did this in callback_dispatch of VideoLAN::LibVLC where VLC was handing me filled picture buffers in a second thread, and I needed to get them to perl's main thread. I pass the pointer through a pipe, then pass it up to perl when the user's event library sees the handle is readable. Meanwhile the perl event library can also be reacting to sockets and timers and all that.
Rephrased, the design here is that there are a collection of picture objects, and at any one time they are either owned by the main thread, owned by the video thread, or in transit in a pipe. It's a few fractions of a millisecond slower than thread synchronization functions, but perfectly fine for real-time video playback.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^10: OT: Why does malloc always give me 24?
by karlgoethebier (Abbot) on Aug 25, 2024 at 09:55 UTC |