473,789 Members | 2,781 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

using pthread to create threads, and my application hogs on memory

Hello,

I have written a simple relay server. using pthreads. To handle
multiple clients.
Design wise its like this.. (my humble apologies if the post is too
long)

The thread design was borrowed from this article
(http://www.codeproject.com/useritems...avaThreads.asp)

class IRunnable {
public:
virtual void run() = 0;
};

class Thread {
private:
IRunnable *threadObj;
pthread_t a_thread;
void *tresult;

public:

Thread(IRunnabl e *ptr) {
threadObj = ptr;
}
static void *threadProc(voi d* ptr) {
((IRunnable*)pt r)->run();
return NULL;
}
void Thread::start() {
int result = pthread_create ( &a_thread, NULL, threadProc,
threadObj);
printf("Thread START [%s::%d] thread result=%d\n", name, count,
result);
}
void wait() {
pthread_join(a_ thread, 0);
threadObj = NULL;
}
};
now the server class listens on some port and as soon as someone
connects hands it over to this new Thread called ClientHandler which
implements IRunnable.

server () {
/*.. Socket definitions etc... */
while (1) {
SOCKET clientSocket = accept(mySocket ,
(struct sockaddr *)
&clientAddr,(in t *) &clientAddrLen) ;
ClientHandler *ch = new ClientHandler(c lientSocket, log);
Thread *t = new Thread(ch);
t->start();
}
}
class ClientHandler : public IRunnable
{
private:
//some private data
SOCKET s;
sockaddr_in addr;
char *getbuffer;
char *sendbuffer;

char *clientNameCach e;
int oldPort;
int port;
int bufSize;
int result;
bool disconnectFlag, errorFlag, relent, connected, runflag;
DataManager *dm;
PingManager *pm;
time_t tim;

public:
char *myClientId,*hi sClientId, *username, *password;

public:
//public methods
ClientHandler(S OCKET &s, ofstream *log);
~ClientHandler( );
virtual void run();
bool touch();
bool getTime (time_t &tim);
bool stop();
};
Now the issue, is, that the server does not release any memory at the
end, which means there is a memory leak, the threads are not being
cleaned up.

So I wrote another thread.. which recieves a shared list of all the
threads the server creates.. and all of them in a loop are waiting for
the client handlers to exit. Now, after the clienthandler exists, and
I call the destructor, I get an exception..
the destructor is quite simple..

ClientHandler:: ~ClientHandler( ) {
try {
closesocket(s); // stop all comunication
delete[] getbuffer;
delete[] sendbuffer; // I get Error Here.. However sendbuffer is
initialised and used
delete[] myClientId;
delete[] hisClientId;
delete[] clientNameCache ;
delete[] username;
delete[] password;
} catch (...) {
int err = GetLastError();
*log << "Error In Destroying Client Handler ... " << endl;
}
}
I am sure I am doing something horribly wrong. Any pointers ?
thanking you all, for reading this awfully newbie code, and making
sense (if any) of the explanation above.

Cheers.

Feb 3 '07 #1
1 2099
sharekhan wrote:
>
class Thread {
private:
IRunnable *threadObj;
pthread_t a_thread;
void *tresult;

public:

Thread(IRunnabl e *ptr) {
threadObj = ptr;
}
static void *threadProc(voi d* ptr) {
((IRunnable*)pt r)->run();
return NULL;
}
Why not pass the Tread object in here, so it can be deleted after run()
returns, assuming Threads are always created on the heap?
void Thread::start() {
int result = pthread_create ( &a_thread, NULL, threadProc,
threadObj);
This should at least give a warning as threadProc isn't an extern "C"
linkage function.

--
Ian Collins.
Feb 3 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2500
by: Abbas | last post by:
I am running a t-sql script which makes a bunch of calls to stored procedures, updates few tables and inserts records. I do this in Begin Tran and commit. The problem I am having is that SQL Server hogs 100% of cpu. This degrades the performance of other applications obviously. The memory it uses remains flat at about less than half of total memory. Is there any optimization required? Or if there is any script to be run on SQL Server...
6
2229
by: Evan David Light | last post by:
After agonizing over this problem for a few days, I've decided to seek help. No, not the variety that involes a jacket that zips up the back but this august body of intrepid individuals. I've discovered the following: - My compile line should resemble: CC -D_PTHREADS -D_POSIX_C_SOURCE=199506L -LANG:std -n32 -mips3 -c <src> -o <obj> - My link line should resemble: CC -LANG:std -n32 -mips3 <objs> -o <executable> -lpthread
7
4376
by: lokb | last post by:
Hi, I am creating a detach thread as shown below and caling pthread_create in a while loop where the file names in the directory are fetched and is passed as a parmater to pthread create. The apilcation is running fine except for the sleep cycle. My sleep cylce is sleep(5) which is halting all the threads created from execution. What i need here is the main thread creating the pthread should only sleep but not all the threads created. Is...
2
3614
by: fran | last post by:
Server: IBM XSERIES 225 (Intel Xeon CPU 2.40GHz 1GB RAM) Operating System: Linux RedHat ES 2.1 kernel 2.4.9 Languaje: C++ Compiler: gcc 2.96 Libs: pthread We are in need of your help in order to solve the folowing problem: We´re working on a server side application that implements threads (pthread under linux Red Hat ES 2.1 kernel 2.4.9). The problem seems
6
3206
by: m | last post by:
Hello, I have an application that processes thousands of files each day. The filenames and various related file information is retrieved, related filenames are associate and placed in a linked list within a single object, which is then placed on a stack(This cuts down thread creation and deletions roughly by a factor of 4). I create up to 12 threads, which then process a single object off of the stack. I use a loop with a boolean...
35
4044
by: Carl J. Van Arsdall | last post by:
Alright, based a on discussion on this mailing list, I've started to wonder, why use threads vs processes. So, If I have a system that has a large area of shared memory, which would be better? I've been leaning towards threads, I'm going to say why. Processes seem fairly expensive from my research so far. Each fork copies the entire contents of memory into the new process. There's also a more expensive context switch between...
0
2555
by: tonno01 | last post by:
Hi... i am developing some multi-threaded application on linux (fedora) Intel SMP hardware. i was wondering if it is possible to force a pthread to use always the same processor, in other words, it is possible to prevent the linux scheduler to switch the running threads between the processors? I checked the thread attributes, but nothing worked there. Hope somebody can help me! Best regards! manuel
19
3400
by: =?ISO-8859-1?Q?Nordl=F6w?= | last post by:
I am currently designing a synchronized queue used to communicate between threads. Is the code given below a good solution? Am I using mutex lock/unlock more than needed? Are there any resources out there on the Internet on how to design *thread-safe* *efficient* data- structures? /Nordlöw
5
7302
by: AmeL | last post by:
Hi all. I have a socket. I want to create two threads, one is to read from socket, and another is to write to socket. However, I want these two threads to be able to share the same data, so I created a mutex to lock the resource. I don't know what happens with my two threads, but it always quits after it did some tasks. #include "camel.h" #include "irc.h" pthread_t gtid; pthread_mutex_t gMutex;
0
9666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9511
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10410
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9984
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9020
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7529
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5418
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.