Why are socket async callback methods usually static?

Why are async callback socket methods usually static? (suppose I understand static classes, methods, and data objects). Will there be a fundamental design / logic error if I need to write a class using these instance methods? Is there anything special that should be avoided?

+3
source share
3 answers

There is no specific reason for them to be static. It all depends on your design.

If the callback requires access to class members, it can be declared as a member of the instance. However, you need to make sure that you synchronize access to instance members correctly, because the callback can be called simultaneously from different threads.

I think that the examples you looked at passed the necessary data through the callback through IAsyncResult.AsyncState, so they did not require additional information from the class. If you can do this, it will simplify your code, since you no longer need to worry about thread synchronization, etc.

Update

, , , . , , , , , , , . , .

+3

, ++ this . , . - , , ( this), ( ) .

, #, , , , . , , . , # , .

: , ++ . , ACE, Boost ASIO, POCO, . , , , , , . , , (, wxWidgets Qt) , . , " " (, , , ACE ).

, #, , . , . WCF () , . , , , (- ), , , WCF, .

, , , , . - Winsock - , C, . ( ), , . , "" Winsock.

+1

Source: https://habr.com/ru/post/1764024/


All Articles