c++ - how to lock for a variety of threads pthread_mutex_lock -


how create lock lock different threads entering function? thread 1: thread 2: c thread 3: c thread 4: b thread 5: c thread 6: b

"a", "b" ,"c" argument passed in thread when created.

so when lock 1 lock x, threads of have "a" , "b" passed in cannot continue , thread of type "c" can enter critical section. or have make 6 different locks example?

thank you!

your requirement's vague, assuming want concurrent readers same "allowed letter", you'd better off using rwlock. (if want "control" thread setting of a, b or c allowed work, rather having a, b , c take on whenever no other "letter" running through controlled code, different (simpler) solution needed....)

as first cut (so can "evolve" solution in easier-to-understand way), each thread:

1) acquires reader lock

2) if "allowed letter" variable differs letter, release reader lock , try writer lock

    2a) when getting writer lock, modify allowed letter own, release lock , restart @ 1) (see below)

3) work

4) release reader lock

this uses "writer" mode affect change in allowed letter. if need arbitrate "allowed letter" threads having write access shared data, want change 3) take either reader or writer lock on distinct rwlock, released before 4).

2a) creates race condition, in thread gets writer lock sets allowed letter, when releases lock b thread may writer lock before can reader lock actual work. b thread may release writer lock , go reader lock find has it, neither thread progresses, nor deadlocked per se. whether resolves depends on thread queuing implementation rwlock, on race conditions, interrupts, pre-emption due system load etc.. tackled in couple ways, it's easy , adequate use mutex disallow other threads write locks modifying allowed letter: use trylock on this, , if fails release write lock , restart @ 1).

so, end 1 or 2 rwlocks , supporting mutex....


Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -