proper threadpool using pthreads -


i trying write customized threadpool suited purpose using pthreads, , new pthreads. read these (posix threads programming , linux tutorial posix threads) tutorials online , quite helpful, still have (maybe silly) doubts regarding mutexes , condition variables:

  1. what scope of mutex? global mutex lock global variables 1 thread can access them @ time? if have 2 global mutexes, lock same set of variables? mutex declared inside class or function, happen when lock/unlock it?
  2. if plan read global variable, , not modify @ all, should still use mutex lock?
  3. if correct, condition variable used wake other threads sleeping (or blocked using pthread_cond_wait()) on condition. wake call sleeping threads given pthread_cond_signal() or pthread_cond_broadcast() other thread. how flow of control supposed occur or 1 thread wake(s) work , wait until next work available? particularly interested in scenario 4 threads.
  4. is there way set affinity of thread particular processor core before created (so starts execution on desired core , no shifting of cores occur after creation)?

i sorry if questions silly, said, new this. help, comments, code or pointer resources appreciated. in advance help.

that's lot of questions. few answers.

(1a) scope of mutex whatever program be. in sense no different other kind of variable.

(1b) global mutex protect whatever variables program protect. think other questions might have fundamental misunderstanding here. there nothing magical mutexes. can't declare 1 , "ok, protect these variables", have incorporate mutex in code. if have 2 functions use variable x , 1 mutex lock/unlock around changes variable , other function ignores mutex exists aren't protecting anything. best example can think of advisory file locks - 1 program can use them if doesn't file isn't locked.

(1c) rule, don't have multiple mutexes locking same data. invitation problems. again use of mutexes depends on programmed cooperation. if function protecting data b mutex c while function d protecting data b mutex e data b isn't protected @ all. function can hold lock on mutex c since function d pays no attention it overwrite data b anyway.

(1d) basic scoping rules apply.

(2) no. if variable isn't going change in way make inconsistent among threads don't need lock it.

(3) there number of detailed answers on over go considerable detail on this. search around bit.

(4) not aware.


Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -