multithreading - QT -> how to kill thread if thread is consuming all resources and doesnt allow slot to execute -


i working on qt 5.5 , have created seperate thread uses 3rd party library apis. api function when executes consumes resources , not allow other function execute in thread.

this new thread starts click of button "start" , not know how stop or kill thread when press stop button. below sample example

threadworking = new qthread();  workheavy    = new workinghard;           workheavy->movetothread( threadworking );          connect( threadworking, signal( started() ),        workheavy,     slot( slotstartstream() ) );         connect( workheavy,     signal( sigstopstream() ),  threadworking, slot( quit() ) );         connect( workheavy,     signal( sigstopstream() ),  workheavy,     slot(deletelater() ) );         connect( threadworking, signal( finished() ),       threadworking, slot(deletelater() ) );          connect( workheavy,     signal( sigstartstream() ), this, slot( slottruestreamrun()  ) );         connect( workheavy,     signal( sigstopstream() ),  this, slot( slotfalsestreamrun() ) );          connect( this,            signal( sigmopscamstopcmd() ), workheavy, slot(slotstopstream() ) );         threadworking->start();  also// void workinghard::slotstartstream()  {     g_main_loop_run( gloop ); // consumes resources. } void workinghard::slotstopstream()  {   // clean mess g_main_loop_quit( gloop );     gst_element_set_state (pipeline, gst_state_null);    // g_main_loop_quit( gloop );     releasememory(); } 

please advice me kill thread based on id or else. 1 thing clear cant go inside thread when function running.

as folks have suggested use terminate. if use terminate() still need free memory pointed in fiunction slotstopstream??

please advice me kill thread based on id or else.

you not want such advice. terminating running thread leaks resources , may leave process in corrupt state - e.g. heap manager's data structures may corrupt, or other global state may corrupt. do not forcibly terminate threads - else undefined behavior.

i doubt g_main_loop_run "consuming" resources. well, busy doing something, or waiting things happen. it's event loop, after all. it's documented :) calling g_main_loop_run not different calling exec() on qeventloop.

you can call g_main_loop_quit quit event loop. if qeventloop, call quit on thread. i'm not sure if g_main_loop_quit thread-safe, documentation mum it. alas, don't have worry that: can invoke worker thread itself.

we'll call g_main_loop_quit within loop's context. lambda's body run in worker thread , cause loop exit:

class workinghard : public qobject {    q_object    gmainloop * gloop;    .... };  /// method thread-safe void workinghard::quitloop() {   auto context = g_main_loop_get_context(gloop);   g_main_context_invoke(context, +[](gpointer ptr) -> gboolean {     g_main_loop_quit((gmainloop*)ptr);     return false;   }), (gpointer)gloop); } 

the + operator applied lambda converts function pointer. saves defining stand-alone function.

to end loop, call quitloop thread.

note if you're on linux, qeventloop based on glib event loop , can use qeventloop directly, instead of using glib apis.


Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -