Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to send my callbacks in different threads is it possible ?

What I have tried:

iam continuously sending callbacks to other application
C
typedef struct
	{
		char *Domain;
		char *Uri;
		UINT32 IpAddr;
		UINT32 Port;
		int  Direction;	//0:-InBound,1:-Outbound
	}PACKETINFO, *PPACKETINFO;

	typedef int(CALLBACK *ProgressCallback)(PACKETINFO *info);	


Dowork(ProgressCallback funcaddr)
{
while(true)
{
         	if(funcaddr !=0 )
		{	
                	//callback send here		
			res = funcaddr(pinfo);			
		}
}
}
Posted
Updated 14-May-19 5:52am
v2
Comments
Rick York 14-May-19 11:06am    
What does "send my callbacks" mean?

The code you posted passes a pointer to a function into a function. That is possible.

It isnt a good idea, because you need to transfer memory and control to another thread which may pause or exit some time.

So I would schedule the data with some transfer function (in thread 1) into some storage which appends the data (in the memory of thread 2). Than the thread 2 can fetch the data and work on it.

Handle the scenario when thread 2 is terminated and so set the function pointer in thread 1 to NULL.
 
Share this answer
 
Comments
Member 14087451 15-May-19 3:35am    
sample code plz
Yes, you can do it, of course.

I would write the Dowork function this way
C
Dowork(ProgressCallback funcaddr)
{
  if ( funcaddr != 0)
  {
    while(true)
    {	
      res = funcaddr(pinfo);
    }
  }
}
Anyway:
  • Where is res defined?
  • As well, where is pinfo defined?
  • Why are you iterating on a neverending loop, simply discarding the callback return value?
 
Share this answer
 
v2
Comments
Member 14087451 15-May-19 1:04am    
actually this is a code of User mode dll which is getting information from driver, which it is sending it to UI and getting resonse from there, res is a response
pinfo is PACKETINFO which it is sending to UI
Member 14087451 15-May-19 1:09am    
iam iterating on a neverending loop because this user mode dll is reading the packets send by driver application and analysis the packet and dropping the packets and re injecting according the response getting from UI application
Member 14087451 15-May-19 1:12am    
currently iam facing the problem that user mode dll is continuously sending information to UI application which is making UI application crash, i want if we send response in different thread UI application will get more time to response to user mode dll
Member 14087451 15-May-19 1:24am    
complete code is like

typedef struct
{
char *Domain;
char *Uri;
UINT32 IpAddr;
UINT32 Port;
int Direction; //0:-InBound,1:-Outbound
}PACKETINFO, *PPACKETINFO;

typedef int(CALLBACK *ProgressCallback)(PACKETINFO *info);

PPACKETINFO pinfo = { 0 };
const int ALLOW = 1;
const int BLOCK = 0;
Dowork(ProgressCallback funcaddr)
{



while(true)
{


if (!PacketsRecv(handle, packet, sizeof(packet), &packet_len, &addr))
{
fprintf(stderr, "warning: failed to read packet (%d)\n",
GetLastError());
continue;
}
//printf("Receiving packet....\n");
ParsePacket(packet, packet_len, &ip_header, NULL,
NULL, NULL, NULL, &tcp_header, NULL, &payload, &payload_len,
NULL, NULL);

if (ip_header == NULL || tcp_header == NULL || payload == NULL)
{
// Packet does not match the blacklist; simply reinject it.
if (!PacketSend(handle, packet, packet_len, NULL, &addr))
{
fprintf(stderr, "warning: failed to reinject packet (%d)\n",
GetLastError());
}
continue;
}

int response = 0;

if(funcaddr !=0 )
{
//callback send here
response = funcaddr(pinfo);

}

free(pinfo);
pinfo = NULL;

if (response == ALLOW )
{
// Packet does not match the blacklist; simply reinject it.
if (!PacketSend(handle, packet, packet_len, NULL, &addr))
{
fprintf(stderr, "warning: failed to reinject packet (%d)\n",
GetLastError());
}
continue;
}

else we block it by hijacking the
// connection.
}


}
Member 14087451 15-May-19 1:28am    
how can i send response in multiple thread so that i can make my application more responsive

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



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