Solved Pthread and Gui wont work together
-
Hello!
After a night with just looking at the program I can't find the what the problem is. When I start my program I get a nice looking view but I can't recvieve anything from my server, only when I'm closing my program I get an output from the server. I don't get any message that I put to see if the function is even running, I get them as I said when I'm closing down this application. Can please anyone help me?
#include "mainwindow.h" #include <QApplication> #include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <netdb.h> #include <cstring> #include <stdlib.h> #include <vector> #include <pthread.h> #include "msg.h" #define NUM_THREADS 1 using namespace std; void sendInitMessage(int socket){ int results, seq, playerID, x, y; seq = 0; JoinMsg join; join.head.id = 0; join.head.seq_no = seq; join.head.type = Join; join.head.length = sizeof(join); join.desc = Human; join.form = Cube; join.name[0] = 'D'; seq = seq + 1; char sendBuffer[sizeof(join)]; memcpy((void*)sendBuffer, (void*)&join, sizeof(join)); results = send(socket, sendBuffer, sizeof(sendBuffer), 0); } void sendMessage(int socket){ } void *recvieveMsg(void *sock){ long socket; socket = (long)sock; int results, seq, playerID, x, y; char recBuffer[1024]; printf("Comming a bit.."); while(1){ results = recv(socket, recBuffer, sizeof(recBuffer), 0); printf("Something recieved"); if(results > 0){ printf("%lo ", sizeof(recBuffer)); MsgHead* msghead; msghead = (MsgHead*)recBuffer; if(msghead->type == Join){ playerID = msghead->id; printf("%d", playerID); }else if(msghead->type == Leave){ printf("LEAVE!!\n"); }else if(msghead->type == Change){ ChangeMsg* changemsg; changemsg = (ChangeMsg*)recBuffer; if(changemsg->type == NewPlayer){ NewPlayerMsg* newPlayer; newPlayer = (NewPlayerMsg*)recBuffer; if(msghead->id != playerID){ printf("%d\n", newPlayer->desc); printf("%d\n", newPlayer->form); printf("%s\n", newPlayer->name); } }else if(changemsg->type == PlayerLeave){ printf("Player Leave"); }else if(changemsg->type == NewPlayerPosition){ printf("Update player pos"); }else{ printf("Change msg went wrong"); } }else if(msghead->type == Event){ printf("Event!!\n"); }else if(msghead->type == TextMessage){ printf("TextMessage!!\n"); }else{ printf("What was that?"); } }else if(results == 0){ printf("Connection closed!!!"); break; }else{ printf("Something went wrong"); break; } } } void connection(){ int socket, results, seq, playerID, x, y; vector<int> vecID; vector<int> vecx; vector<int> vecy; seq = 0; struct addrinfo *serverInfo, hints; memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; results = getaddrinfo("localhost", "49152", &hints, &serverInfo); if(results != 0){ printf("getaddrinfo went wrong"); exit(1); } socket = ::socket(serverInfo->ai_family, serverInfo->ai_socktype, serverInfo->ai_protocol); results = connect(socket, serverInfo->ai_addr, serverInfo->ai_addrlen); if(results == -1){ printf("Could not connect"); exit(1); } freeaddrinfo(serverInfo); pthread_t threads[NUM_THREADS]; sendInitMessage(socket); pthread_create(&threads[0], NULL, recvieveMsg, (void *)socket); } int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); connection(); return a.exec(); }
-
Hi
I have never mixed pthreads and Qt so just asking.Does the Mainwindow work ? You can click in it and it responds?
-
Yes, the Mainwindow works just find, can click it and all, so that one responds well.
-
@degens
And that is also after you call void connection() ? -
That's correct, the gui parts works just fine. Can click my buttons and they change values and so on.
-
@degens
Ok so we can be sure the main event loop is running.
So issue must be with thread.is the
while(1){
results = recv(xxx)being run?
Also, just be be 100% sure
pthread_t threads[NUM_THREADS]; << local variable
xx
pthread_create(&threads[0], NULL, recvieveMsg, (void *)socket);so when connection() ends, the threads[NUM_THREADS] will be deleted.
Is that intentional ?Just asking that it should only live as long as inside connection() and not longer.
-
No that is not intentional, but when the conneciton() is done, shouldn't the thread still be running until I "kill" it? I mean, how would you otherwise use threads?
-
@degens
Yes, it should be running, but we give it pointer to this structure
http://man7.org/linux/man-pages/man3/pthread_create.3.html
and its silently deleted a moment after.
So Im not sure if it will upset the thread or not. Just asking. :)Also did you check the return code from pthread_create?
-
Alright this is really weird. I did a printf on my connection() function, and it didn't print out anything, only when I closed my program. So it seems like the functions is not even executed when running main, only when I close...
Thanks for the help btw! :)
-
@degens
Ok that sounds a bit strange. :)
Have you tried the debugger ? single stepping and see what is actually being run.
Or set a break point and start.Update: Wait, missin gprintf can be other issue
http://stackoverflow.com/questions/1716296/why-does-printf-not-flush-after-the-call-unless-a-newline-is-in-the-format-strintry with
printf("test!");
fflush(stdout); -
Ha! The problem was lying right in front of me... Now I get the output I wanted, thank you very much for your help! Will definitely ask here again if something else comes up, even though I'm a Java Developer. ;)
-
@degens
Super :)
I think Qt + application sets up buffer so thats why it stopped printing once it became
part of Qt project.
You are always welcome to come ask here. Its a user orientated forum and many other also use
Java so its also good place to ask. "In java, i can write this, how can i do same in Qt". Kind of questions.