Create a secure thread

Source: Internet
Author: User
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 ++ ^_^

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.