I haven't written about this series for a long time. I picked it up myself and got a little confused, because I haven't performed it for more than two years.Code.
The last time we solved how to pass parameters like a thread, but did not solve the problem of getting changed parameters in the main thread. There are two reasons for this problem:
1. the startup and termination of threads in the thread pool are not designed by us.ProgramTo control;
2. There is no return value after the thread execution in the thread pool is completed. (This is actually related to the thread scheduling and memory allocation policies of the operating system)
Therefore, to obtain the return value, you need to solve these two problems.
For the first problem, we can easily think of a mechanism provided by the operating system, "signal lights" (also called "semaphores" and "signals ), the mechanism provided by the operating system allows communication between processes. In this way, the sub-thread can communicate with the main thread.
For the second problem, since the thread does not return a value, we can set a field in the parameter passed to the thread to store the return value, you can also obtain the attributes of the class where the waitcallback method is located (of course, this attribute is changed in the thread ).
Now, let's briefly talk about the "Traffic Signal" mechanism and make a metaphor related to the restroom (not very elegant). On the train, when a goes to the toilet, he locks the toilet door and marks a "someone" as an external identifier. When he finishes solving the problem, he opens the door, the "someone" logo will become an "unmanned" logo, so that B can go in. If a does not change this logo, B will not be able to get in even if it throws. (Of course, the brute force mode is excluded ). C # providedAutoreseteventIt is such a "signal light" that defines the variables of this class and passes them to the sub-thread. When the sub-thread ends, set the "signal light" to true ("no one ") status, so that the main thread can enter (start execution ).
Similarly, we still have the experience of going to the public toilet. There are many toilet pools in the public toilet, and of course there is a toilet administrator. At this time, A1, A2, A3 ...... You can get a plaque from the toilet administrator and solve the "problem" at the same time. If the toilet administrator is not very BT, when A1, but there are also BT, so we have to wait until A1, A2, A3 ...... Solve the problem, so that B1, B2 ,...... Enter, this isWAithandle. waitall () and waithandle. waitany (), waithandle. waitone ()The difference is that it can be usedManualresetevent's "signal light"
Of course, there are also some threads competing for resources. To achieve synchronization, you can use the "signal lights" of monitor and mutex ".
In short, the traffic signal mechanism provides a mechanism for communication between the subthread and the main thread, and between the subthread and the subthread, the common feature of these communication mechanisms is that a thread releases resources (giving a signal) and other threads (main threads) start to execute.
Next, I will use a port scan program to describe it in detail.