Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Pthread and Gui wont work together

Pthread and Gui wont work together

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 2 Posters 3.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    degens
    wrote on last edited by
    #1

    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();
    }
    
    
    
    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      I have never mixed pthreads and Qt so just asking.

      Does the Mainwindow work ? You can click in it and it responds?

      1 Reply Last reply
      0
      • D Offline
        D Offline
        degens
        wrote on last edited by
        #3

        Yes, the Mainwindow works just find, can click it and all, so that one responds well.

        mrjjM 1 Reply Last reply
        0
        • D degens

          Yes, the Mainwindow works just find, can click it and all, so that one responds well.

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @degens
          And that is also after you call void connection() ?

          1 Reply Last reply
          0
          • D Offline
            D Offline
            degens
            wrote on last edited by
            #5

            That's correct, the gui parts works just fine. Can click my buttons and they change values and so on.

            mrjjM 1 Reply Last reply
            0
            • D degens

              That's correct, the gui parts works just fine. Can click my buttons and they change values and so on.

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by mrjj
              #6

              @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.

              1 Reply Last reply
              1
              • D Offline
                D Offline
                degens
                wrote on last edited by
                #7

                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?

                mrjjM 1 Reply Last reply
                0
                • D degens

                  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?

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by mrjj
                  #8

                  @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?

                  1 Reply Last reply
                  0
                  • D Offline
                    D Offline
                    degens
                    wrote on last edited by
                    #9

                    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! :)

                    mrjjM 1 Reply Last reply
                    0
                    • D degens

                      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! :)

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by mrjj
                      #10

                      @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-strin

                      try with
                      printf("test!");
                      fflush(stdout);

                      1 Reply Last reply
                      2
                      • D Offline
                        D Offline
                        degens
                        wrote on last edited by
                        #11

                        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. ;)

                        mrjjM 1 Reply Last reply
                        1
                        • D degens

                          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. ;)

                          mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          @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.

                          1 Reply Last reply
                          1

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved