c - Mutex lock and a number of threads -
i have api (written in c
) permits number of incoming (concurrent) connections. each connection handled independet pthread
created whenever client connects api. each of these connections can (but not have to) change properties of server such requests should not processed @ same time.
my code follows structure:
pthread_mutex_t lock; void request_handler(char * request) { pthread_mutex_lock(&lock); process_request(request); pthread_mutex_unlock(&lock); }
assume 1 request being processed takes long time (e.g. ten seconds). in time, 5 other requests come in, 5 additional pthreads
reach @ pthread_mutex_lock
function.
- do wait there , continue intended (one after served)?
the reason why i'm asking this i'd expect haven't found example more 1 concurrent thread in official documentation.
- is there guarantee requests processed in same order in have been received or any single 1 of 5 waiting threads allowed start after long request finished?
strict in-order execution not needed code, i'd know beforehand expect in order design code properly.
i read recursive mutex
, i'd avoid them due number of reasons. furthermore, code not try lock multiple times 1 single pthread
construction.
the mutex ensures 1 thread enters critical section of code, in case, call process_request()
, @ time. once thread t
obtains lock, subsequent thread must wait until t
releases it.
if multiple such threads arrive, gets chance go first depends on scheduling of threads operating system, not deterministic , can different each time run program. however, guarantee 1 thread can pass, each time.
Comments
Post a Comment