Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is mine start of the program

C++
#include"Derived.h"
int main()
{
	for(int i=0;i<=2;i++)
	{
		Derived *pDerived=new Derived();
		pDerived->fun();
		Sleep(30000);
	}
	return 0;
}



This is the interface i mean base class

C++
#include <stdlib.h>
#include <stdio.h>
#include <string.h> 
#include <process.h> 
#include<atlstr.h>
#include<Windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
using namespace std; 
class Interface
{
public:
	struct addrinfo    addr_;   
	unsigned short          port_;      
	CString                  hostname_;    
	       
	HANDLE                  threadHandle_;    
	HANDLE                  threadHandle1_;  
	bool                    connected_;     
	SOCKET          sock_; 
	SOCKET ThreadSocket;
	virtual void fun()=0;
	virtual DWORD WINAPI MachineConnectThread(Interface *my)=0;
	virtual int SendRequest(SOCKET sock)=0;      //This will send the request to the machine.
	virtual char* ReceiveResponse(SOCKET sock)=0;  //This will receive the response from the machine that is binary data.
	HANDLE                  threadHandle_;    

};


This is mine derived class
C++
#include"CBase.h"
class Derived:public Interface
{

public:
	Derived();
	void fun();
	int SendRequest(SOCKET sock);
	char* ReceiveResponse(SOCKET sock );
	 DWORD WINAPI MachineConnectThread(Interface *my);
	static UINT ThreadFunc(LPVOID param) ;
	CRITICAL_SECTION	VarCS;
};




C++
#include"Derived.h"

Derived::Derived()
{

	sock_=INVALID_SOCKET;
	ThreadSocket=INVALID_SOCKET;

		InitializeCriticalSection(&VarCS);

}
void Derived::fun()
{
	printf("in fun function\n");

	//Here socket will be created and will be passed to sebd request and receive response function
	try{
	DWORD dwThreadId;

	threadHandle_=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ThreadFunc,this,0,&dwThreadId);
}catch(...)
{
	if(threadHandle_ == NULL)
	{

		printf("Error while creating thread.\n");

		return 0;
	}
}
}
int Derived::SendRequest(SOCKET sock)
{
	printf("in this function request will be sent to server\n");

}
char* Derived::ReceiveResponse(SOCKET sock)
{
	printf("here response wil be received from the server\n");
}
UINT Derived::ThreadFunc(LPVOID param)
{
	Interface* obj = (Interface *)param; 

		obj->MachineConnectThread(obj); // call the member function                       
		// to do the work in our new thread
		return 0;
}
DWORD WINAPI Derived::MachineConnectThread(Interface *my)
{
	ThreadSocket=(SOCKET)my->sock_;
	while(1)
	{
		EnterCriticalSection(&VarCS);//WILL THIS CRITICAL SECTION WORK HERE
		char *buf=NULL;
		char* preBuf=NULL;
		int i=0;
		
		SendRequest(ThreadSocket);
		buf=ReceiveResponse(ThreadSocket);
		Sleep(20000);
		LeaveCriticalSection(&VarCS);
	}
		
		
		
		
			
	
		
		
		
		
				
}
Posted

yes, as long as its inside one application it will work,
the only thing you can add is to use DeleteCriticalSection[^] when you'll no longer need it.
 
Share this answer
 
Hi, you shoud initialize your critical section only once in your main thread. And also you have to free resources it holds by calling DeleteCriticalSection(...);

C++
#include"Derived.h"
CRITICAL_SECTION VarCS;
int main()
{
    InitializeCriticalSection(&VarCS);
    for(int i=0;i<=2;i++)
    {
        Derived *pDerived=new Derived();
        pDerived->fun();
        Sleep(30000);
    }
    DeleteCriticalSection(&VarCS);
    return 0;
}

Also try to consider TryEnterCriticalSection call instead of EnterCriticalSection, which is blocking your thread.
The critical section is the best choice if you plan to create single process solution. Mutexes and semaphores not as fast as critical section. Please see Jeffrey Richter's "Windows via C/C++", or please refer to MSDN (http://msdn.microsoft.com/en-us/library/windows/desktop/ms682530(v=vs.85).aspx[^])
 
Share this answer
 
You have a problem here:
C++
Sleep(20000);
LeaveCriticalSection(&VarCS);

You want to leave the critical section before sleeping. Only stay in the CS while it's critical that your thread's execution is not interrupted. See this StackOverflow discussion.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900