int str_len = read(m_events[i].data.fd, buf, BUF_SIZE);
I have this and I read data into the buf declared like
char buf[BUF_SIZE];
What I am trying to do is that I am trying to get the data and hand it to the WorkHanndler which I defined and nothing more than just a thread pool.
And the function is
void ServerManager::addWork(int sender, char *data){
Work* work = new Work(sender, data);
m_workHandler->addWork(work);
}
So I need a char pointer which points to the data I just read. Since the buffer is defined as an array I won’t be able to pass that into the function.
Also, do you guys think this is a good idea in terms of server design? I am reading data from the buffer and don’t parse it and just hand to the raw data to the thread pool (insert into queue). The thread pool has a queue and five separate thread competes to get the job from the queue with mutex lock and condition variable. After finish the job then the separate thread will write the result to the output buffer. Please let me know if it has drawbacks and you guys have a better idea. Thanks in advance.
Only possible flaw I see right now could be inside the
Workclass: Ensure it creates its own copy of the buffer contents immediately in the constructor as it will be overwritten once new data arrives. Other than that it should work as expected (hard to judge without specific work to be done, etc.).Also there shouldn’t be an issue using the array when a pointer to the array’s elements’ type is required as the array is essentially just a pointer (code wise):
If there’s some compiler warning or error, post the exact message. The only real difference between both variants to access arrays is that the compiler will know he’ll get a poiner to a whole array instead of a single instance when using
char variable[]instead ofchar *variable.