V
V
Vitaly Azovsky2017-03-27 12:11:20
linux
Vitaly Azovsky, 2017-03-27 12:11:20

How to organize an eternal loop in a separate thread in C++11?

Good day.
There is a task to constantly listen to sockets, well, to do something with it all, the class is named Transport.
Because socket types are different + in the final class it is not necessary to show the insides at all - I did it using pimpl.
Now further, in the main program, an eternal loop is spinning, and at the beginning of each iteration, my class is called, epoll_wait is called there. In order for the epol not to block the execution of the eternal loop, the timeout parameter was set to 0.
I want to do the following so that in the main program I don’t constantly call the Transport function, move the latter into a separate thread with its own eternal loop. And add 2 queues, one for receiving, the second for sending. And they should both be visible from the main program, i.e. at the beginning of the loop iteration, the program looks at what is in the receive queue, reads (takes out) messages, then in the loop writes information somewhere in the send queue. Transport, in turn, polls the epol in an eternal loop, if it has subtracted something, writes it to the receive queue and constantly scans the send queue, if it is not empty, we send it. The question is the following, I didn’t work with c++11 threads, I only have experience with Qt threads, so I ask for your help. Is it better to shove the whole class into the thread, if only its eternal cycle? If only the cycle how will the access from the stream to the queues look like? If the whole class, then the method is put in a queue, and get out of the queue wrapped in mutexes?
Well, as I understand it, when creating a thread, you must immediately detach?
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2017-03-27
@TrueBers

Why do you need epoll then? How will it, in your case, differ from a regular blocking socket in a separate thread?
Its meaning is precisely in asynchrony and non-blocking, and you nullify these goodies, introduce braking mutexes, queues.
Since you are writing asynchronously on the pluses, then take the library for this, the same Asio.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question