Solved Pthread and Gui wont work together
-
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.