I developed an external which connects to a streaming TCP/IP socket. In
order to receive the (time-critical) data as fast as possible, the object
has to handle the receiving and parsing of the data in a separate thread,
otherwise the max scheduler locks up. I have instantiated a Posix thread to
receive and parse the data, which writes to a shared data buffer in my
object. At the same time a Max clock is running (in the main scheduler) for
reading the buffer and writing the data to an outlet.
I use a circular buffer to pass the data between the Posix thread and the
clock function. I first used std::vector for it, but it didn't like that at
all (debug assertion failure related to iterators), so now I use a plain
array which seems to be ok. The object works fine for the time being, but
I'm pretty sure what I'm doing it not thread safe.
Can I make use of critical regions or mutexes to protect the data buffer? Is
there a Max native way to spawn a new thread and use critical regions with
it? I've read the section about critical regions, but I'm just not sure if
it applies to my situation.
"So unless you are completely sure of what you are doing, and absolutely
need to make use of multiple critical regions to protect your code, we
suggest you use the global critical region."
So yeah, I'm clearly not completely sure:)
In earlier attempts I tried using the scheduler_set() and family to create a
new scheduler instead of using Posix, but this got me into trouble more than
anything. I find the documentation about this is subject very sparse at
best, and a search in the archives didn't turn up anything either. The text
in the paragraph "creating schedulers" in the writingexternals.pdf seems to
be messed up.