PaolaJ.
PaolaJ.

Reputation: 11532

How to stop and destroy thread from main thread (from which is created)

I need to create, run, stop thread and then again same process (reloading some new data and need to refresh and cannot use C++11 standard). I have created and run thread like from mine main thread

pthread_t p;
pthread_create(&p, NULL, calculation, some_pointer_to_object);

How to stop and destroy this thread from main thread ? (pthread_exit is from current thread).

Upvotes: 0

Views: 51

Answers (2)

Deduplicator
Deduplicator

Reputation: 45664

The only clean way to do so is this: Set up a flag in the main thread, start the thread, poll the flag in your new thread and finish fast if it's set. Everything else but letting your new thread close itself down cleanly on request opens a boatload of cans of worms, and that's an understatement.

Upvotes: 0

Chnossos
Chnossos

Reputation: 10486

You need to use pthread_cancel().

Upvotes: 1

Related Questions