Jim Diamond
Jim Diamond

Reputation: 1274

Linux, pthreads -- send stop condition

I'm writing an application using phtreads and C under Linux. The main function starts a bunch of threads (up to 20). Then, under some criterion I need to stop all threads at once. Each thread is running under some conditional loop, like:

while (state) {....}

So I need to change state to false for each thread (I think at the moment it would be enough to have one state for each thread, but maybe in the future I'll have to stop each thread separately)

So, what's the best way to do it? I could use some state as a global variable and use mutex for it. Each time I will have to lock, read, unlock it to read it's value. And I think it's kinda of time consuming procedure. Do you have any other ideas how to implement it?

Upvotes: 1

Views: 558

Answers (1)

Brijesh Valera
Brijesh Valera

Reputation: 1117

# man pthread_cancel

allows you to send cancellation request to the thread.

# man pthread_cleanup_push

allows you to set cancellation routine.

Upvotes: 1

Related Questions