Sergio6Rey
Sergio6Rey

Reputation: 403

How to cancel GTK3 thread from another thread?

I have a graphic application which I am developing using GLib and the g_thread functions for creating and manipulating threads. When I press an "ok" button, I create a thread that does some heavy tasks. However I want to have another button named "cancel" that allows me to cancel the created thread at any moment.

I was doing some research but I cannot find any way of doing this: terminating the thread from another place that is not the created thread. The function g_thread_exit only can be called from the function that the thread is executing. So my main aim is to cancel the created thread from another thread originated when I click the "cancel" button.

Maybe I have to use the pthread library, which has a function called pthread_cancel that may fit my expectations.

Thank you so much for any kind of information.

Upvotes: 0

Views: 265

Answers (1)

ptomato
ptomato

Reputation: 57920

You can share a GCancellable between the two threads, and have the heavy task thread call g_thread_exit() when it is cancelled.

Using GLib threads there is indeed no way to directly abort a thread from another thread.

Upvotes: 2

Related Questions