<br><br><div class="gmail_quote"><div dir="ltr">On Tue, Aug 15, 2017, 16:36 jml5qh <<a href="mailto:jml5qh@gmail.com">jml5qh@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi all,<br>
<br>
I have a streaming application where the user can play and stop the stream.<br>
When the user presses play, create a new pthread to setup the pipelines and<br>
call g_main_loop_run:<br>
<br>
void GStreamerStreamImpl::start() {<br>
        pthread_create(&pipeline_setup_thread, NULL, &startInternal, this);<br>
}<br>
<br>
void GStreamerStreamImpl::setupPipelines() {<br>
        context = g_main_context_new();<br>
        g_main_context_push_thread_default(context);<br>
        this->createPipelines(); //Internally call gst_parse_launch on the<br>
pipeline<br>
        main_loop = g_main_loop_new(context, FALSE);<br>
        this->startPlayingPipelines(); //Internally call<br>
gst_element_set_state with GST_STATE_PLAYING<br>
        //Now we run the main loop. This will block until the streaming is<br>
finished<br>
        g_main_loop_run(main_loop);<br>
        this->stopPlayingAndDeallocPipelines();<br>
        g_main_loop_unref (main_loop);<br>
        main_loop = NULL;<br>
<br>
        /* Free resources */<br>
        g_main_context_pop_thread_default(context);<br>
        g_main_context_unref (context);<br>
    }<br>
<br>
When the user presses stop, we just call g_main_loop_quit:<br>
<br>
void GStreamerStreamImpl::stop()<br>
    {<br>
            g_main_loop_quit(main_loop); //This will release the main_loop<br>
and then everything will get cleaned up<br>
            pthread_join(pipeline_setup_thread, NULL);<br>
    }<br>
<br>
However, we are running into a race condition if we call g_main_loop_quit<br>
before the new thread gets to g_main_loop_run. How would you expect to<br>
handle this? We can check g_main_loop_is_running in stop(), but we still<br>
need to make sure everything gets cleaned up once the main_loop starts<br>
running. Ideally, I would just call g_main_loop_quit on the streaming thread<br>
but that's not possible since g_main_loop_run blocks that thread.<br></blockquote></div><div><br></div><div>Use g_main_context_invoke to create an idle source with a callback that calls g_main_loop_quit. It will be called by the mainloop.</div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
</blockquote></div>