Hurry and beg for help: thread resource release Problems

Source: Internet
Author: User
Hurry and beg for help: the problem of thread resource release-Linux general technology-Linux programming and kernel information. The following is a detailed description. I have been suffering from a problem that may cause me to lose my job for two weeks ~ 555. in time of crisis, I would like to ask my brother, sister, and younger siblings to help me!

Problem description: a network server program is responsible for real-time monitoring of data sending and receiving from each connected/disconnected network client.
In my socket server program, the client connection needs to be monitored in real time. Every time a new client connection is found, I create a MySockClient Class Object: MySockClient

When a class object is started, a thread pthread_create will be responsible for communicating with the corresponding client (this thread is an endless loop of while (1) and will not exit actively). When the client is disconnected

When the program is started, it will delete the MySockClient class object. Now the entire program has been implemented, but the test has found that when the client is connected/disconnected repeatedly,

Pthread_create consumes a large amount of memory each time it is executed. When deleting the MySockClient class object, it is not released, resulting in the final resource depletion and crash.

Note: The MySockClient class does not have dynamically opened memory resources. The loss is determined to be produced by thread creation.
I will roughly describe the program with a simple framework program as follows:


Class MySockClient
{
Protected:
Pthread_t FHandle;
Bool TGC_CreateThread ();

Public:
MySockClient (bool bCreateThread)
{
....
Int ret;
Ret = pthread_create (& FHandle, NULL, TGC_ThreadFunc, (void *) this );
If (ret! = 0)
Printf ("Create pthread error! \ N ");
Return;
}

....
}
~ MySockClient ()
{
....
Pthread_kill (FHandle, 0 );
....
};
....
};
// The preceding thread processing function TGC_ThreadFunc is a monitoring handler containing while (1). the loop can be exited only when the class is deleted.

In the main process, the server program will constantly monitor the connection status of the customer. When the new client connects, it will create a new MySockClient object (in fact, it also creates a thread to process the transaction ), when the customer is disconnected

Will call delete MySockClient to release class resources, where the main process will not exit


The problem is: Every time I create a new MySockClient object, executing pthread_create () will consume a lot of resources, but when I delete MySockClient, the program

Pthread_kill (FHandle, 0) is called, but the thread resources are not released. What exactly is the problem and how can it be solved?



Thank you!
Related Article

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.