Results 1 to 5 of 5

Thread: Architecture to eliminate "children and parent that is in a different thread"

  1. #1
    Join Date
    Jun 2020
    Posts
    9
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Question Architecture to eliminate "children and parent that is in a different thread"

    I need call QSslSocket into grpc method (https://www.grpc.io/docs/languages/cpp/):
    Qt Code:
    1. class GreeterServiceImpl final : public Greeter::Service {
    2. Status SayHello(ServerContext* context, const HelloRequest* request, HelloReply* reply) override {
    3. mySslClient.setHostName(/*...*/);
    4. // ...
    5. return Status::OK;
    6. }
    7.  
    8. MySslClient mySslClient;
    9. };
    To copy to clipboard, switch view to plain text mode 

    MySslClient looks like that:
    Qt Code:
    1. MySslClient::MySslClient(QObject *parent) :
    2. QObject(parent),
    3. mSocket(new QSslSocket(this))
    4. {
    5. QSslConfiguration config = mSocket->sslConfiguration();
    6. //...
    To copy to clipboard, switch view to plain text mode 

    As a result, I get an error
    Qt Code:
    1. QObject: Cannot create children for a parent that is in a different thread.
    2. (Parent is QSslSocket(0x55f14d7adf60), parent's thread is QThread(0x55f14d7ad700), current thread is QThread(0x7fc3fc3faf60)
    To copy to clipboard, switch view to plain text mode 

    Can this problem be resolved?

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Architecture to eliminate "children and parent that is in a different thread"

    Can this problem be resolved?
    Instead of creating your QSslSocket instance in the MySslClient constructor, create it in the same thread where you create the MySslClient instance and pass the pointer in as an additional argument to the MySslClient constructor. Basically, any QObjects that have a parent-child relationship all must be created in the same thread. So any children of MySslClient have to be created in the same thread where the MySslClient instance (and its parent QObject) are created.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. The following user says thank you to d_stranz for this useful post:

    CPPProger (18th June 2020)

  4. #3
    Join Date
    Jun 2020
    Posts
    9
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Architecture to eliminate "children and parent that is in a different thread"

    Quote Originally Posted by d_stranz View Post
    Instead of creating your QSslSocket instance in the MySslClient constructor, create it in the same thread where you create the MySslClient instance and pass the pointer in as an additional argument to the MySslClient constructor. Basically, any QObjects that have a parent-child relationship all must be created in the same thread. So any children of MySslClient have to be created in the same thread where the MySslClient instance (and its parent QObject) are created.
    Unfortunately, it didn’t work out. I get gRPC helloworld example https://github.com/plasticbox/grpc-w...helloworld/src

    Qt Code:
    1. class GreeterServiceImpl final : public Greeter::Service {
    2.  
    3. public:
    4.  
    5. // begin attempt 1
    6. QSslSocket* mSocket;
    7. void setSslSocket(QSslSocket *socket = nullptr) {
    8. mSocket = socket;
    9. }
    10. // end attemt 1
    11.  
    12. Status SayHello(ServerContext* context, const HelloRequest* request, HelloReply* reply) override {
    13.  
    14. // begin attemt 1
    15. QSslConfiguration config = mSocket->sslConfiguration();
    16. config.setPeerVerifyMode(QSslSocket::VerifyNone);
    17. config.setProtocol(QSsl::SecureProtocols);
    18. mSocket->setSslConfiguration(config);
    19. mSocket->connectToHostEncrypted("google.com", 8080);
    20. // end attemt 1
    21.  
    22. std::string prefix("Hello ");
    23. reply->set_message(prefix + request->name());
    24. return Status::OK;
    25. }
    26.  
    27.  
    28. };
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. void RunServer() {
    2. std::string server_address("0.0.0.0:50051");
    3. GreeterServiceImpl service;
    4.  
    5. // begin attemt 1
    6. QSslSocket* sok = new QSslSocket;
    7. service.setSslSocket(sok);
    8. // end attemt 1
    9. ...
    10. server->Wait();
    11. }
    12.  
    13. int main(int argc, char** argv) {
    14. QCoreApplication a(argc, argv);
    15. RunServer();
    16. return a.exec();
    17. }
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. $ ./server
    2. Server listening on 0.0.0.0:50051
    3. QObject: Cannot create children for a parent that is in a different thread.
    4. (Parent is QSslSocket(0x56117c127520), parent's thread is QThread(0x56117c120800), current thread is QThread(0x7fa828004510)
    5. ^C
    To copy to clipboard, switch view to plain text mode 
    Attached Files Attached Files
    Last edited by CPPProger; 22nd June 2020 at 19:26.

  5. #4
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    554
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Architecture to eliminate "children and parent that is in a different thread"

    You still don't create the socket in the run thread but in the main thread...

  6. #5
    Join Date
    Jun 2020
    Posts
    9
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Architecture to eliminate "children and parent that is in a different thread"

    Quote Originally Posted by ChristianEhrlicher View Post
    You still don't create the socket in the run thread but in the main thread...
    Directly inside the gRPC method, the socket also does not want to work.

    Qt Code:
    1. class GreeterServiceImpl final : public Greeter::Service {
    2. public:
    3. Status SayHello(ServerContext* context, const HelloRequest* request, HelloReply* reply) override {
    4. // begin attempt 2
    5. QSslSocket mSocket;
    6. QSslConfiguration config = mSocket.sslConfiguration();
    7. config.setPeerVerifyMode(QSslSocket::VerifyNone);
    8. config.setProtocol(QSsl::SecureProtocols);
    9. mSocket.setSslConfiguration(config);
    10. mSocket.connectToHostEncrypted("google.com", 8080);
    11. // end attemt 2
    12. std::string prefix("Hello ");
    13. reply->set_message(prefix + request->name());
    14. return Status::OK;
    15. }
    16. };
    17.  
    18. void RunServer() {
    19. std::string server_address("0.0.0.0:50051");
    20. GreeterServiceImpl service;
    21. grpc::EnableDefaultHealthCheckService(true);
    22. grpc::reflection::InitProtoReflectionServerBuilderPlugin();
    23. ServerBuilder builder;
    24. builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
    25. builder.RegisterService(&service);
    26. std::unique_ptr<Server> server(builder.BuildAndStart());
    27. std::cout << "Server listening on " << server_address << std::endl;
    28. server->Wait();
    29. }
    30.  
    31. int main(int argc, char** argv) {
    32. QCoreApplication a(argc, argv);
    33. RunServer();
    34. return a.exec();
    35. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. QObject: Cannot create children for a parent that is in a different thread.
    2. (Parent is QSslSocket(0x556688f2a520), parent's thread is QThread(0x556688f23800), current thread is QThread(0x7fe7a8004510)
    To copy to clipboard, switch view to plain text mode 


    Added after 21 minutes:


    I also tried to connect Qt and gRPC with the inclusion of a signal in the server. The latter would help connect the two frameworks.
    Qt Code:
    1. class GreeterServiceImpl final : public Greeter::Service, public QObject {
    2. Q_OBJECT
    3. public:
    4. Status SayHello(ServerContext* context, const HelloRequest* request, HelloReply* reply) override {
    5. std::string prefix("Hello ");
    6. reply->set_message(prefix + request->name());
    7. emit message();
    8. return Status::OK;
    9. }
    10. signals:
    11. void message();
    12. };
    To copy to clipboard, switch view to plain text mode 
    But it failed
    Qt Code:
    1. In function `GreeterServiceImpl::GreeterServiceImpl()':
    2. error: undefined reference to `vtable for GreeterServiceImpl'
    3. In function `GreeterServiceImpl::~GreeterServiceImpl()':
    4. error: undefined reference to `vtable for GreeterServiceImpl'
    5. error: collect2: error: ld returned 1 exit status
    To copy to clipboard, switch view to plain text mode 
    Last edited by CPPProger; 24th June 2020 at 10:07.

Similar Threads

  1. Replies: 0
    Last Post: 16th June 2011, 22:48
  2. "new" + "delete" a QWidget and its children
    By vkincaid in forum Qt Programming
    Replies: 2
    Last Post: 19th January 2010, 22:51
  3. Replies: 2
    Last Post: 7th September 2009, 22:13
  4. Replies: 6
    Last Post: 8th July 2009, 14:24
  5. Replies: 5
    Last Post: 19th April 2009, 14:24

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.