Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

linux pthread_suspend

Tags:

c

linux

pthreads

Looks like linux doesnt implement pthread_suspend and continue, but I really need em.

I have tried cond_wait, but it is too slow. The work being threaded mostly executes in 50us but occasionally executes upwards of 500ms. The problem with cond_wait is two-fold. The mutex locking is taking comparable times to the micro second executions and I don't need locking. Second, I have many worker threads and I don't really want to make N condition variables when they need to be woken up.

I know exactly which thread is waiting for which work and could just pthread_continue that thread. A thread knows when there is no more work and can easily pthread_suspend itself. This would use no locking, avoid the stampede, and be faster. Problem is....no pthread_suspend or _continue.

Any ideas?

like image 641
johnnycrash Avatar asked Jun 08 '10 01:06

johnnycrash


People also ask

How do I suspend a thread in Linux?

sleep() — Suspend execution of a thread.

Is accept thread safe?

accept() is listed as an “async signal safe” function. Below, it is explained that ALL async signal safe functions are thread safe.

How do pthreads work?

Pthread uses sys_clone() to create new threads, which the kernel sees as a new task that happens to share many data structures with other threads. To do synchronization, pthread relies heavily on futexes in the kernel.

How is pthreads implemented?

Pthreads are implemented as user threads by the runtime library. Most portable implementation since it requires no kernel support. Fast context switches between user threads because it is handled entirely in user space without the overhead of the kernel implementing a process-level context switch.


1 Answers

Make the thread wait for a specific signal.

Use pthread_sigmask and sigwait.

like image 147
jweyrich Avatar answered Nov 15 '22 23:11

jweyrich