What does get_message do?What does get_message do? Based on the limited information it looks like you are exiting the thread after reading a single message. To answer your question accurately I would need to see more code - for instance the context for signaling the event.
Code:
static int sock_receive(char* message, size_t size) {
int len;
len = recv(chatls, message, size, 0);
if (len == -1) {
if (errno != EAGAIN && errno != EWOULDBLOCK) {
return -1;
}
}
return len;
}
void get_message() {
char message[500] = "";
int n = sock_receive(message, 500);
if(n > 0) {
add_message(message, 1, white, background_message);
}
else {
exitReceive = true;
}
}
That is why i would need to use a thread.
sock_receive() keeps running until it receives an answer.
it looks like you are exiting the thread after reading a single message
The threads only exits when i shut the server down.
it keeps running, i've tested this.
the context for signaling the event.
At the beginning of the script i create the sockets and i signal the event to start listening.
If i would use get_message() in the beginning in the loop, i get the exact same result, so the thread does literally the same as the function call.
What i want it to do
I want my code to keep checking on new messages, but since recv only stops running when it actually receives a message, I need to do it simultaneously with my main thread.
So basically what I need is that the function sock_recv() keeps running on the background, while my main loop keeps running as well.
EDIT: found this in a c forum "If it's asyncronous, it will. If it's synchronous, it usually blocks until it receives something.".
So I'm gonna try to make it asynchronous?
EDIT: Gonna play a little with select() and poll() to see if there is a message that i can recv()
But the threads are useless for me in this case.
Last edited by Tjessx,