multithreading - Linux multi-thread, pausing one thread while continue running the other threads within the same process -
i cannot find proper solution problem.
if have more 1 thread in 1 process. , want make 1 thread sleep while running other threads within same process, there predefined syntax or have own implementation (sleep) ?
ideally want send indication thread thread when time sleep.
edited (2015-08-24) have 2 main threads, 1 sending data on network, other receives data network. beside jitter, receiving thread validation , verification , file management in time lead drag behind. add micro sleep sender receiver catch up. sched_yield() not in case because hw has multi core cpu more 40 cores.
from description in comments, looks you're trying synchronize 2 threads 1 of them doesn't fall behind far other.
if that's case, you're going wrong way. seldom idea synchronization sleeping, because scheduler may incur unpredictable , long delays cause other (slow) thread remain stopped in run queue without being scheduled. if works of time, it's still race condition, , it's ugly hack.
given use case , constraints, think you'd better off using barriers (see pthread_barrier_init(3)
). pthread barriers allow create rendezvous point in code threads can catch up.
you call pthread_barrier_init(3)
part of initialization code, specifying number of threads synchronized using barrier. in case, it's 2.
then, threads synchronize others calling pthread_barrier_wait(3)
. call blocks until number of threads specified in pthread_barrier_init(3)
call pthread_barrier_wait(3)
, @ point every thread blocked in pthread_barrier_wait(3)
becomes runnable , cycle begins again. essentially, barriers create synchronization point no 1 can move forward until arrives. think you're looking for.
here's example simulates fast sender thread , slow receiver thread. both synchronize barriers ensure sender not work while receiver still processing other requests. threads synchronize @ end of work unit, of course, can choose each thread calls pthread_barrier_wait(3)
, thereby controlling when (and where) threads synchronize.
#include <stdio.h> #include <pthread.h> #include <stdlib.h> #include <unistd.h> #include <string.h> pthread_barrier_t barrier; void *sender_thr(void *arg) { printf("entered sender thread\n"); int i; (i = 0; < 10; i++) { /* simulate work (500 ms) */ if (usleep(500000) < 0) { perror("usleep(3) error"); } printf("sender thread synchronizing.\n"); /* wait receiver catch */ int barrier_res = pthread_barrier_wait(&barrier); if (barrier_res == pthread_barrier_serial_thread) printf("sender thread last.\n"); else if (barrier_res == 0) printf("sender thread first.\n"); else fprintf(stderr, "pthread_barrier_wait(3) error on sender: %s\n", strerror(barrier_res)); } return null; } void *receiver_thr(void *arg) { printf("entered receiver thread\n"); int i; (i = 0; < 10; i++) { /* simulate lot of work */ if (usleep(2000000) < 0) { perror("usleep(3) error"); } printf("receiver thread synchronizing.\n"); /* catch sender */ int barrier_res = pthread_barrier_wait(&barrier); if (barrier_res == pthread_barrier_serial_thread) printf("receiver thread last.\n"); else if (barrier_res == 0) printf("receiver thread first.\n"); else fprintf(stderr, "pthread_barrier_wait(3) error on receiver: %s\n", strerror(barrier_res)); } return null; } int main(void) { int barrier_res; if ((barrier_res = pthread_barrier_init(&barrier, null, 2)) != 0) { fprintf(stderr, "pthread_barrier_init(3) error: %s\n", strerror(barrier_res)); exit(exit_failure); } pthread_t threads[2]; int thread_res; if ((thread_res = pthread_create(&threads[0], null, sender_thr, null)) != 0) { fprintf(stderr, "pthread_create(3) error on sender thread: %s\n", strerror(thread_res)); exit(exit_failure); } if ((thread_res = pthread_create(&threads[1], null, receiver_thr, null)) != 0) { fprintf(stderr, "pthread_create(3) error on receiver thread: %s\n", strerror(thread_res)); exit(exit_failure); } /* work... */ if ((thread_res = pthread_join(threads[0], null)) != 0) { fprintf(stderr, "pthread_join(3) error on sender thread: %s\n", strerror(thread_res)); exit(exit_failure); } if ((thread_res = pthread_join(threads[1], null)) != 0) { fprintf(stderr, "pthread_join(3) error on receiver thread: %s\n", strerror(thread_res)); exit(exit_failure); } if ((barrier_res = pthread_barrier_destroy(&barrier)) != 0) { fprintf(stderr, "pthread_barrier_destroy(3) error: %s\n", strerror(barrier_res)); exit(exit_failure); } return 0; }
note that, specified in manpage pthread_barrier_wait(3)
, once desired number of threads call pthread_barrier_wait(3)
, barrier state reset original state in use after last call pthread_barrier_init(3)
, means barrier atomically unlocks , resets state, ready next synchronization point, wonderful.
once you're done barrier, don't forget free associated resources pthread_barrier_destroy(3)
.
Comments
Post a Comment