How to copy data in buffer to somewhere in memory (char *)
int str_len = read(m_events[i].data.fd, buf, BUF开发者_如何学C_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 Work
class: 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):
char data[1024];
char *pdata = data; // now data as well as pdata point to the first element
// data[0] is the same as using *(pdata + 0)
// data[1] is the same as using *(pdata + 1)
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 of char *variable
.
I think the "incompatible types" should only be a warning. If it is an error then try casting the buffer to char* when you give it to the function. But this is a quick and dirty solution Maybe you should think about a more generic solution that does not produce this error.
The parsing of the data should be the workers job. If you let the master parse it would slow him down.
Can you just avoid the copying? If you read the data into a buffer class, you could just queue the buffer instance and immediately create another for the next lot of data. No copying and the pool threads will always be working on different data than the server thread that is reading from the socket. Sure, you will have to free the buffer objects, (or repool them), after the pool threads have processed the data, but this is a small price to pay for avoiding copying, read/write clashes, buffer pointer manipulation synchronized across threads and all that nasty stuff that's very hard to get right.
Rgds, Martin
精彩评论