Some people explain the differences between threads and processes as follows:
2. Thread: minimum unit of Resource Competition
2. Process: minimum unit of Resource Allocation
From the perspective of resource management, threads and processes are well separated. Here, we will not go into the details about how each specific platform implements a thread or process. Here we only need to understand that the crash of a process generally does not affect other processes running on the same system as it, the death of a thread is related to other threads in the same process as the thread.
Generally, the system provides APIs in the C format of the registration thread as follows (Java and other built-in tasks are not mentioned here ):
// Function: registers a thread.
// Para: char * threadname thread name
// (Void *) Fun (void *) thread callback function
// Void * para callback function entry Parameter
Void reg_thread (char * threadname, (void *) Fun (void *), void * para );
// Function: deregister a thread
// Para: char * threadname thread name
Void unreg_thread (char * threadname );
These two interfaces are simple and clear. I can write my lower thread:
# Define free (s) do {/
Free (s );/
S = NULL ;/
} While (0)
Void fun (void * para)
{
......
While (true)
{
Dosomething ()
}
}
Void * para = malloc (1 (Para );
If (null = pthis)
{
Return;
}
Pthis-> prethread ();
While (pthis-> m_blflag)
{
Pthis-> onthread ();
}
Pthis-> postthread ();
}
};
We encapsulate it into a class shelf and start and stop a thread in an object-oriented way. The specific implementation in it is reserved for readers who are interested to fill it out by themselves! The task you think of is derived from you. You can adapt to the blocking function as needed. At least in this section, I really don't want to say it has any potential risks, because I have to take a break. ^_^
Conclusion: I'm trying to convince a CProgramSwitch to C ++ ^_^