c - write pointers to pipe, are there any strict aliasing or pun type issues? -
i need create many fifo queues in program, used communication between threads in same process.
i think can use pipe() purpose, because in way, can use select or poll on thread fetch nodes queue.
int* fd_pipe = (int*)malloc(2*sizeof(int)); pipe(fd_pipe);
now problem how put pointer queue since each node strucutre, want put pointer queue, like
typedef{ struct packet *pkt; struct info *info; int seq; }node;
on threads put node queue: node* node = (node*)malloc(sizeof(node)); node->info = ...; node->seq = ...; node->pkt = ...; write(fd_pipe[1], node, sizeof(node)); on threads read node queue: char buf[1000]; read(fd_pipe[0], buf, sizeof(node)) node* mynode = (node*)buf;
then mynode want.
is there wrong in program? strict aliasing or pun type problems? thanks!
you not have aliasing issues. here issue see read(fd_pipe[0], buf, sizeof(node))
, should read(fd_pipe[0], buf, sizeof(node *))
.
i not sure why use char buffer here, prefer
node *node; if (read(fd_pipe[0], &node, sizeof(node)) <= 0) { // error, deal }
it's simpler , clearer.
now, code work if use blocking i/o default. also, technically, should handle short read/writes writing/reading on pipe atomic if read/written size less pipe_buf (which larger pointer)
you have bit of grey area in code memory synchronization before write since system call, work.
a pipe unusual way communicate between threads. usually, people use in-memory queues communicate between threads. 1 example http://www.boost.org/doc/libs/1_53_0/doc/html/boost/lockfree/queue.html
Comments
Post a Comment