Does using callbacks in C++ increase coupling?

前端 未结 8 1739
予麋鹿
予麋鹿 2021-01-03 11:04

Q1. Why are callback functions used?

Q2. Are callbacks evil? Fun for those who know, for others a nightmare.

Q3. Any alternative to

8条回答
  •  囚心锁ツ
    2021-01-03 11:27

    In rare scenarios with Win32-style-callbacks watch out for "obscure reentrancy" issues (the following is irrelevant if by callback you just mean very basic passing of a function as arg to another function where concurrency issues are unimaginable ).
    The excellent index to Joe Duffy's "Concurrent Programming on Windows" lists 5 topics under "reentrancy", which generally are specializations of the concept of "Around Robin-Hood's Barn RECURSION on the SAME THREAD" -- in other words from the top-rated-answer "the invoked party is passed some pointer and it has no idea what's behind it.", "no idea" can sometimes lead around-Robin-Hood's-barn.
    Nothing I just said is specific to callbacks, but if the invoked-party stumbles across one of Duffy's scenarios then "obscure reentrancy" can happen. To put it a different way, implicit in the concept of "callback" is that you're going to seem to be called back in-the-same-thread, how does this happen, how does it get synchronized.
    If you Google on Duffy's book-title and tack the word Callback onto your search then you get more than 10 pages of hits.

提交回复
热议问题