Click here to Skip to main content
15,905,420 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Help with code Pin
Iain Clarke, Warrior Programmer21-Oct-08 21:59
Iain Clarke, Warrior Programmer21-Oct-08 21:59 
QuestionLinker options Pin
freeheap21-Oct-08 9:08
freeheap21-Oct-08 9:08 
AnswerRe: Linker options Pin
Perspx21-Oct-08 9:51
Perspx21-Oct-08 9:51 
QuestionNeed help with mutex Pin
auralius manurung21-Oct-08 6:42
auralius manurung21-Oct-08 6:42 
AnswerRe: Need help with mutex Pin
Mark Salsbery21-Oct-08 6:56
Mark Salsbery21-Oct-08 6:56 
GeneralRe: Need help with mutex Pin
auralius manurung21-Oct-08 10:52
auralius manurung21-Oct-08 10:52 
GeneralRe: Need help with mutex Pin
Mark Salsbery21-Oct-08 11:06
Mark Salsbery21-Oct-08 11:06 
GeneralRe: Need help with mutex Pin
auralius manurung21-Oct-08 17:02
auralius manurung21-Oct-08 17:02 
class CPROXY
{
public:
			
	CPROXY(void);
	~CPROXY(void);

	int Run(int nPort);

	static DWORD WINAPI MotherThread(LPVOID param);
	static DWORD WINAPI DaughterThread(LPVOID param);
	int MotherThreadWorker();
	int DaughterThreadWorker();

	int GetAddressAndPort(char * cStr, char *cAddress, int * nPort);


private:

	ACE_SOCK_Acceptor	client_acceptor;
	ACE_SOCK_Connector	server_connector;

	CRITICAL_SECTION	guard;
	HANDLE			wait;

	bool				isMotherThreadRunning;
	bool				isDaughterThreadRunning;

	static std::list<ACE_SOCK_Stream> queue;
};


The implementation is like this:

CPROXY::CPROXY(void)
{
	InitializeCriticalSection(&guard);
	wait = CreateEvent(NULL, FALSE, FALSE, NULL);
}

CPROXY::~CPROXY(void)
{
}

std::list<ACE_SOCK_Stream> CPROXY::queue; 

int CPROXY::Run(int nPort)
{
	DWORD thid;
	HANDLE hMotherThread = CreateThread(NULL, 0, MotherThread, this, 0, &thid);
	if (!hMotherThread)
		return -1;

	ACE_SOCK_Stream client_stream;
	ACE_INET_Addr addr;
	addr.set(nPort, addr.get_ip_address());

	int e = client_acceptor.open(addr);
	if (e == INVALID_SOCKET)
		return -1;

	while(true)
	{
		int e = client_acceptor.accept(client_stream);
		if (e == INVALID_SOCKET)
			continue;

		//Store in a buffer.
		EnterCriticalSection(&guard);
		queue.push_back(client_stream);
		LeaveCriticalSection(&guard);
	}
	
	return 0;
}

DWORD WINAPI CPROXY::MotherThread(LPVOID param)
{
	CPROXY *newobj = (CPROXY *)param;

	newobj->MotherThreadWorker();

	return 0;
}

int CPROXY::MotherThreadWorker()
{
	isMotherThreadRunning = true;

	while (isMotherThreadRunning)
	{
		EnterCriticalSection(&guard);
		bool isEmpty = queue.empty();
		LeaveCriticalSection(&guard);

		if (!isEmpty){
			DWORD thid;
			HANDLE hDaughterThread = CreateThread(NULL, 0, DaughterThread, this, 0, &thid);
			if (!hDaughterThread)
				continue;

			printf("\nWAITING!\n");
			WaitForSingleObject(wait, INFINITE);
			printf("\nFINISHED!\n");
		}
	}
	return 0;
}

DWORD WINAPI CPROXY::DaughterThread(LPVOID param)
{
	CPROXY *newobj = (CPROXY *)param;

	newobj->DaughterThreadWorker();
        :
        :
	return 0;
}

int CPROXY::DaughterThreadWorker()
{
	char buf[BUFSIZE];
	char cServerAddress[256];
	int  nServerPort;

	EnterCriticalSection(&guard);
	ACE_SOCK_Stream client_stream = queue.front();
	queue.pop_front();
	LeaveCriticalSection(&guard);

	SetEvent(wait);
        :
        :
        :
	return 0;
}


Here is the main file:

int main(int argc, char *argv[])
{
CPROXY *MyProxy = new CPROXY;
MyProxy->Run(ACCEPTOR_PORT);

while(1){}

return 0;
}

I've modified the code, now i put the list queue as a static. So what do you think? How if I change it into non-static. Does it still need synchronization? I also change the way i declare it.
From:
static std::list<ACE_SOCK_Stream> *queue;
To
static std::list<ACE_SOCK_Stream> queue;

So it is no longer a dynamic allocation. I just wanna do something with reason. If i use synchronization, i must be able to state clearly why i need it.

thank you vey2 much for your help... Smile | :)
GeneralRe: Need help with mutex [modified] Pin
Mark Salsbery21-Oct-08 18:17
Mark Salsbery21-Oct-08 18:17 
GeneralRe: Need help with mutex Pin
auralius manurung21-Oct-08 19:43
auralius manurung21-Oct-08 19:43 
GeneralRe: Need help with mutex Pin
Mark Salsbery21-Oct-08 20:06
Mark Salsbery21-Oct-08 20:06 
QuestionDWM - desktop windows manager Pin
Sarriss21-Oct-08 5:31
Sarriss21-Oct-08 5:31 
AnswerRe: DWM - desktop windows manager Pin
Richard Andrew x6421-Oct-08 5:43
professionalRichard Andrew x6421-Oct-08 5:43 
GeneralRe: DWM - desktop windows manager Pin
Sarriss21-Oct-08 5:52
Sarriss21-Oct-08 5:52 
GeneralRe: DWM - desktop windows manager Pin
Richard Andrew x6421-Oct-08 6:08
professionalRichard Andrew x6421-Oct-08 6:08 
GeneralRe: DWM - desktop windows manager Pin
Sarriss21-Oct-08 7:29
Sarriss21-Oct-08 7:29 
QuestionNeed help in C++ classes Pin
Quecumber25621-Oct-08 4:59
Quecumber25621-Oct-08 4:59 
AnswerRe: Need help in C++ classes Pin
Rajesh R Subramanian21-Oct-08 5:22
professionalRajesh R Subramanian21-Oct-08 5:22 
GeneralRe: Need help in C++ classes Pin
Quecumber25621-Oct-08 6:02
Quecumber25621-Oct-08 6:02 
QuestionRe: Need help in C++ classes Pin
CPallini21-Oct-08 6:45
mveCPallini21-Oct-08 6:45 
AnswerRe: Need help in C++ classes Pin
Quecumber25622-Oct-08 3:42
Quecumber25622-Oct-08 3:42 
GeneralRe: Need help in C++ classes Pin
CPallini22-Oct-08 3:44
mveCPallini22-Oct-08 3:44 
GeneralRe: Need help in C++ classes Pin
Quecumber25622-Oct-08 5:25
Quecumber25622-Oct-08 5:25 
GeneralRe: Need help in C++ classes Pin
CPallini22-Oct-08 8:52
mveCPallini22-Oct-08 8:52 
AnswerRe: Need help in C++ classes Pin
Iain Clarke, Warrior Programmer21-Oct-08 6:18
Iain Clarke, Warrior Programmer21-Oct-08 6:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.