c++ - Using asynchronous method vs thread wait -
i have 2 versions of function available in c++ library same task. 1 synchronous function, , of asynchronous type allows callback function registered.
which of below strategies preferable giving better memory , performance optimization?
- call synchronous function in worker thread, , use mutex synchronization wait until result
- do not create thread, call asynchronous version , result in callback
i aware worker thread creation in option 1 cause more overhead. wanting know issues related overhead caused thread synchronization objects, , how compares overhead caused asynchronous call. asynchronous version of function internally spin off thread , use synchronization object, or uses other technique directly talk kernel?
"profile, don't speculate." (djb)
the answer question depends on many things, , there no general answer. role of developer able make these decisions. if don't know, try options , measure. in many cases, difference won't matter , non-performance concerns dominate.
"premature optimisation root of evil, 97% of time" (dek)
update in response question edit:
c++ libraries, in general, don't use magic avoid synchronisation primitives. asynchronous vs. synchronous interfaces wrappers around things anyway. processing must happen in context, , if completion signalled context, synchronisation primitive necessary that.
of course, there might other considerations. if c++ library talking piece of hardware can processing, things might different. haven't told that.
the answer question depends on context haven't given us, including information library interface , structure of code.
Comments
Post a Comment