Advertisement
Guest User

helloworld_pthread_example.cpp

a guest
Mar 25th, 2020
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.76 KB | None | 0 0
  1. /*
  2.  *
  3.  * Licensed to the Apache Software Foundation (ASF) under one
  4.  * or more contributor license agreements.  See the NOTICE file
  5.  * distributed with this work for additional information
  6.  * regarding copyright ownership.  The ASF licenses this file
  7.  * to you under the Apache License, Version 2.0 (the
  8.  * "License"); you may not use this file except in compliance
  9.  * with the License.  You may obtain a copy of the License at
  10.  *
  11.  *   http://www.apache.org/licenses/LICENSE-2.0
  12.  *
  13.  * Unless required by applicable law or agreed to in writing,
  14.  * software distributed under the License is distributed on an
  15.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16.  * KIND, either express or implied.  See the License for the
  17.  * specific language governing permissions and limitations
  18.  * under the License.
  19.  *
  20.  */
  21.  
  22. #include <proton/connection.hpp>
  23. #include <proton/container.hpp>
  24. #include <proton/delivery.hpp>
  25. #include <proton/message.hpp>
  26. #include <proton/messaging_handler.hpp>
  27. #include <proton/tracker.hpp>
  28.  
  29. #include <iostream>
  30.  
  31. #include "fake_cpp11.hpp"
  32.  
  33. #include <pthread.h>
  34.  
  35. const std::string conn_url_glob="//127.0.0.1:5672";
  36. const std::string addr_glob="examples";
  37.  
  38. class hello_world : public proton::messaging_handler {
  39.     std::string conn_url_;
  40.     std::string addr_;
  41.  
  42.   public:
  43.     hello_world(const std::string& u, const std::string& a) :
  44.         conn_url_(u), addr_(a) {}
  45.  
  46.     void on_container_start(proton::container& c) OVERRIDE {
  47.         std::cout<<"on_container_start"<<std::endl;
  48.         c.connect(conn_url_);
  49.     }
  50.  
  51.     void on_connection_open(proton::connection& c) OVERRIDE {
  52.         c.open_receiver(addr_);
  53.         c.open_sender(addr_);
  54.     }
  55.  
  56.     void on_sendable(proton::sender &s) OVERRIDE {
  57.         std::cout<<"on_sendable"<<std::endl;
  58.  
  59.         proton::message m("Hello World!");
  60.         s.send(m);
  61.         s.close();
  62.     }
  63.  
  64.     void on_message(proton::delivery &d, proton::message &m) OVERRIDE {
  65.         std::cout << m.body() << std::endl;
  66.         d.connection().close();
  67.     }
  68. };
  69.  
  70. void *pthread_callback(void *a) {
  71.     std::cout<<"c1 "<<"- URL: "<<conn_url_glob<<" - QUEUE NAME: "<<addr_glob<<std::endl;
  72.  
  73.     try {
  74.         hello_world hw(conn_url_glob,addr_glob);
  75.         proton::container(hw).run();
  76.     } catch (const std::exception& e) {
  77.         std::cerr << e.what() << std::endl;
  78.     }
  79.  
  80.     pthread_exit(NULL);
  81. }
  82.  
  83. int main(int argc, char **argv) {
  84.     pthread_t tid1;
  85.  
  86.     try {
  87.         // This does work (no additional thread is created)
  88.         //pthread_callback(NULL);
  89.  
  90.         // This does not work!
  91.         pthread_create(&tid1,NULL,pthread_callback,NULL);
  92.  
  93.         return 0;
  94.     } catch (const std::exception& e) {
  95.         std::cerr << e.what() << std::endl;
  96.     }
  97.  
  98.     pthread_join(tid1,NULL);
  99.  
  100.     return 1;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement