I need to read data from a serial device and put it into a buffer to be consumed by another thread. Basically, I want to achieve this:
while(!exit){
// read from fd and push into the vector<char> buffer
}
And do it the right way in C++. I know how to get this done in C, and I'd really appreciate it if someone could point me in the right direction.
From what I've found so far, people have been suggesting:
read(fd, &vector[0], vector.size());
But, I'm not convinced. Especially since modifying the &vector[0] directly doesn't update size() (or does it?) and seems like an indirect way to modify the underlying array. I'd like to avoid using open() and read() as well, if I could help, as they aren't really C++. Some form of istream would be awesome here!
Also, I couldn't find any examples of how to neatly "pop" the data from this vector when the data needs to be consumed from the other thread. I believe, and I'm certainly not 100% sure about this, that if there's only one writer thread and one reader thread for this vector, I wouldn't need any special code for thread safety. Please correct me if I'm wrong.
If it matters at all, the data in the vector is binary.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire