John Reis(deleted)
|
Re: RE: Resource manager using thread pool in C++
|
John Reis(deleted)
02/01/2016 9:22 AM
post115643
|
Re: RE: Resource manager using thread pool in C++
Thank you both very much for such a quick reply.
Follow the "complete" code of what I have until the moment (there's still much to do).
About returning a void pointer, I've just did the same as the thread pool example in the Get Programming with QNX
Neutrino guide does.
#define CAN_SERVER_PATH "/net/..."
//****************************************************************************
// Global variables
//****************************************************************************
//****************************************************************************
// Thread pool lifecycle functions
//****************************************************************************
// Context manipulation functions when thread is created and killed
THREAD_POOL_PARAM_T * Comm::ContextAlloc(THREAD_POOL_HANDLE_T *handle)
{
// Stablish connection to CAN server
CommServerStruct *comm_server = new CommServerStruct;
comm_server->can_coid = open(CAN_SERVER_PATH, O_WRONLY);
if(comm_server->can_coid == -1)
{
// Failed to connect to CAN server
// Return error
}
// Initialize values;
comm_server->comm_rcvid = -1;
return ((void*)comm_server);
}
void Comm::ContextFree(THREAD_POOL_PARAM_T *param)
{
CommServerStruct *comm_server = (CommServerStruct *)param;
delete comm_server;
}
// Function to block waiting for a message from client
THREAD_POOL_PARAM_T * Comm::WaitForMessage(THREAD_POOL_PARAM_T *ctp)
{
// Cast pointer
CommServerStruct *comm_server = (CommServerStruct *)ctp;
// Blocks waiting for message
MsgReceive(comm_chid_, comm_server->msg, sizeof(comm_server->msg), NULL);
return ((void*)comm_server);
}
// Message parsing function when unblocks with received message
int Comm::ParseMessage(THREAD_POOL_PARAM_T *ctp)
{
// Cast pointer
CommServerStruct *comm_server = (CommServerStruct *)ctp;
char return_message[16];
// Pass the message over to CAN server
MsgSend(comm_server->can_coid, comm_server->msg, sizeof(comm_server->msg), return_message, sizeof(return_message));
return 1;
}
//****************************************************************************
// Communications set up and start
//****************************************************************************
int Comm::InitComm()
{
if((comm_chid_ = ChannelCreate(0)) == -1)
{
// Could not create Channel, insert error code
return -1;
}
// Set up the thread parameters for the threads that will populate the pool
pthread_attr_t *t_attr;
pthread_attr_init (t_attr);
// Set up the thread pool parameters
thread_pool_attr_t tp_attr;
thread_pool_t *tpp;
memset(&tp_attr, 0, sizeof (tp_attr));
//tp_attr.handle = (void *) 0x12345678; // passed to contextalloc
tp_attr.block_func = WaitForMessage;
tp_attr.unblock_func = UnblockFunc;
tp_attr.context_alloc = ContextAlloc;
tp_attr.handler_func = ParseMessage;
tp_attr.context_free = ContextFree;
tp_attr.lo_water = 2;
tp_attr.hi_water = 6;
tp_attr.increment = 1;
tp_attr.maximum = 10;
tp_attr.attr = t_attr;
// Create a thread pool that returns
tpp = thread_pool_create (&tp_attr, 0);
if (tpp == NULL)
{
// Could not create thread pool
return -1;
}
if (thread_pool_start(tpp) == -1)
{
// Could not start thread pool
ChannelDestroy(comm_chid_);
//sleep (3000);
return -1;
}
// If it gets here, thread pool is up and running
return 1;
}
|
|
|
John Reis(deleted)
|
Re: RE: Resource manager using thread pool in C++
|
John Reis(deleted)
02/01/2016 9:26 AM
post115644
|
Re: RE: Resource manager using thread pool in C++
And yes, I did assume it was more of a language problem.
QNX base it's guides and manuals all in C language.
Could you please give me some directions on how to deal with this C++ Method vs C function callback assignment?
|
|
|