Monday 15 April 2013

multithreading - C++ threading vs. visibility issues - what's the common engineering practice? -



multithreading - C++ threading vs. visibility issues - what's the common engineering practice? -

from studies know concepts of starvation, deadlock, fairness , other concurrency issues. however, theory differs practice, extent, , real engineering science tasks involve greater detail academic blah blah...

as c++ developer i've been concerned threading issues while...

suppose have shared variable x refers larger portion of program's memory. variable shared between 2 threads a , b.

now, if consider read/write operations on x both a , b threads, perchance @ same time, there need synchronize operations, right? access x needs form of synchronization can achieved illustration using mutexes.

now lets consider scenario x written thread a, passed thread b (somehow) , thread reads x. thread b produces response x called y , passes thread a (again, somehow). question is: synchronization primitives should utilize create scenario thread-safe. i've read atomics and, more importantly, memory fences - these tools should rely on?

this not typical scenario in there "critical section". instead info passed between threads no possibility of concurrent writes in same memory location. so, after beingness written, info should first "flushed" somehow, other threads see in valid , consistent state before reading. how called in literature, "visibility"?

what pthread_once , boost/std counterpart i.e. call_once. help if both x , y passed between threads through sort of "message queue" accessed means of "once" functionality. afaik serves sort of memory fence couldn't find confirmation this.

what cpu caches , coherency? should know engineering science point of view? such knowledge help in scenario mentioned above, or other scenario commonly encountered in c++ development?

i know might mixing lot of topics i'd improve understand mutual engineering science practice reuse known patterns.

this question related situation in c++03 daily environment @ work. since project involves linux may utilize pthreads , boost, including boost.atomic. i'm interested if concerning such matters has changed advent of c++11.

i know question abstract , not precise input useful.

you have shared variable x

that's you've gone wrong. threading much easier if hand off ownership of work items using sort of threadsafe consumer-producer queue, , perspective of rest of program, including business logic, nil shared.

message passing helps prevent cache collisions (because there no true sharing -- except of producer-consumer queue itself, , has trivial effect on performance if unit of work big -- , organizing info messages help cut down false sharing).

parallelism scales best when separate problem subproblems. little subproblems much easier reason about.

you seem thinking along these lines, no, threading primitives atomics, mutexes, , fences not applications using message passing. find real queue implementation (queue, circular ring, disruptor, go under different names meet same need). primitives used within queue implementation, never application code.

c++ multithreading c++11 boost memory-fences

No comments:

Post a Comment