[Live-devel] Proper function calls from other threads (was: RE: All client termination)
Erlandsson, Claes P (CERLANDS)
CERLANDS at arinc.com
Tue Sep 11 15:06:02 PDT 2012
The code example below is called from an external thread. Is that ok
No - absolutely not!!! What you're trying to do - call
"TaskScheduler::scheduleDelayedTask()" from an external thread - is
extremely wrong!
Look folks, how many times do I have to say this: A LIVE555 application runs
as a single-thread of control (using an event loop, rather than threads, to
provide concurrency). If you want to communicate with a LIVE555 application
from an external thread (i.e., from a thread other than the one that runs
the LIVE555 event loop), then there are only two proper ways to do this:
1/ By setting a global variable, or
2/ Using an 'event trigger' - i.e., by calling
"TaskScheduler::triggerEvent()". Note that "triggerEvent()" is the
***only*** LIVE555 function that you may call from an external thread.
This is all explained in the FAQ that you were all asked to read before
posting to this mailing list!!!
In your case, you would do something like the following:
1/ Within the LIVE555 thread (e.g., somewhere after you've created
"rtspClient"), call
EventTriggerId myStopStreamEvent =
env.taskScheduler().createEventTrigger(StopStream);
2/ Later, from an external thread, you can call:
env.taskScheduler().triggerEvent(myStopStreamEvent, rtspClient);
and this will cause your "StopStream()" function to be called (with
"rtspClient" as parameter) from the event loop - i.e., as a handled event.
(Note: For this to work, "env", "myStopStreamEvent" and "rtspClient" need to
be global variables, so that they are visible to the external thread. Note,
though, that they are never *modified* by the external thread, only read by
it.)
Thanks a lot for taking the time to (again) clarify things.
One last question on this, as I'm in the middle of reorganizing the code.
Shouldn't it be safe to create the rtspClient object in another thread?
I can't really see a problem with that as it only appears to assign
variables. Since I was so wrong before, I want to verify though ;-)
Besides just being curious and want to find out, it would also simplify
things in the application I'm working on. The application creates clients
dynamically and starts up without any active clients. Having to create the
rtspClient objects using triggers would introduce additional callbacks and
making passing arguments a bit more complex.
This is now the "main startup code":
void InitStreamEngine()
{
TaskScheduler* scheduler = BasicTaskScheduler::createNew();
usageEnvironment = BasicUsageEnvironment::createNew(*scheduler);
myStartStreamEvent =
usageEnvironment->taskScheduler().createEventTrigger((TaskFunc*)StartStreamE
vent);
myStopStreamEvent =
usageEnvironment->taskScheduler().createEventTrigger((TaskFunc*)StopStreamEv
ent);
mySeekAbsoluteEvent =
usageEnvironment->taskScheduler().createEventTrigger((TaskFunc*)SeekAbsolute
Event);
usageEnvironment->taskScheduler().doEventLoop(&eventLoopWatchVariable);
}
/Claes
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.live555.com/pipermail/live-devel/attachments/20120911/841b1d3d/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/x-pkcs7-signature
Size: 5740 bytes
Desc: not available
URL: <http://lists.live555.com/pipermail/live-devel/attachments/20120911/841b1d3d/attachment-0001.bin>
More information about the live-devel
mailing list