This is quite tricky. You have two threads trying to send MIDI, and at least in the Windows case, the sysex message is broken up into packets. The entire sequence of packets must be written without any other intervening writes. On the other hand, you don't want to block. In particular, if the timer thread is running at high priority and the main thread holds a lock, some other thread at middle priority can preempt the main thread, run forever, and prevent the high priority timer thread from making progress (priority inversion).
I think the best solution is to have the main thread send to the timer thread through a FIFO (there is a lock-free, single-reader, single-writer queue implemented in PortMidi). Then let the timer thread check both input and the FIFO for work to do, including Pm_Write's. You have the option of sending the actual message or a pointer to the message when sending sysex from the main thread to the timer thread. It's tempting to send a pointer. To be strictly correct in a multiprocessor implementation, be aware that memory changes visible to one processor may not match the order of operations performed by the other. In this case, you will probably write the message to memory and then write a pointer to the message into the FIFO. First, these writes must not be reordered by an optimizing compiler. A simple, portable, compiler-independent way to guarantee this is to put the writes in separate routines, but this adds routine-call overhead. Second, after writing the entire sysex message, you must perform a memory fence operation that ensures the write will be visible before any following writes, including the write into the FIFO. -Roger Jane wrote: > hey there, > > i think i need to synchronize my calls to Pm_Write (called from within > Pt_Start(resolution, &Prodatum_MIDI::Receive_MIDI, 0)) and > Pm_WriteSysEx (called from the main-thread) as both write to the same > PortMidiStream and sometimes portmidi shuts down when i am trying hard > to keep both calls busy. > > now, how could i synchronize this? the solution must work on linux/mac/ > windows and is not allowed to lock the main-thread. :) > > thank you for any hints, > jan _______________________________________________ media_api mailing list media_api@create.ucsb.edu http://lists.create.ucsb.edu/mailman/listinfo/media_api