Advertisement
Guest User

Untitled

a guest
Nov 3rd, 2022
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.93 KB | None | 0 0
  1. [main.cpp]
  2. #include "my_application.h"
  3.  
  4. #include <thread>
  5.  
  6. int main( int argc, char* argv[] )
  7. {
  8.   printf( "QT_VERSION_STR: %s\n", QT_VERSION_STR );
  9.  
  10.   My_application app( argc, argv );
  11.  
  12.   QObject::connect( &app, &My_application::on_command, [&]( const char* buffer_ ) {
  13.     printf("My_application::on_command: %s\n", buffer_);
  14.   } );
  15.  
  16. #if 0
  17.   auto my_thread_proc = [&app](){
  18.     for(int i = 0; i < 100; ++i)
  19.     {
  20.       app.post_command("some_command");
  21.     }
  22.   };
  23.   auto my_thread1 = std::thread(my_thread_proc);
  24. #endif    
  25.  
  26.   QWidget window;
  27.   window.resize( 320, 240 );
  28.   window.show();
  29.  
  30.   int result = app.exec();
  31.  
  32.   printf("---> after app.exec()\n");
  33.  
  34.   return result;
  35. }
  36.  
  37. [my_application.h]
  38. #pragma once
  39.  
  40. #include <QtWidgets>
  41.  
  42. class My_application : public QApplication
  43. {
  44.     Q_OBJECT
  45. public:
  46.     explicit My_application( int& argc, char** argv );
  47.     void post_command( const char* buffer_ );
  48.     Q_SIGNAL void on_command( const char* buffer_ );
  49. };
  50.  
  51. [my_application.cpp]
  52. #include "my_application.h"
  53.  
  54. My_application::My_application( int& argc, char** argv ) : QApplication( argc, argv ){}
  55.  
  56. void My_application::post_command( const char* buffer_ )
  57. {
  58.   QMetaObject::invokeMethod(
  59.     this, [this, buffer_] { Q_EMIT on_command( buffer_ ); }, Qt::QueuedConnection );
  60. }
  61.  
  62. [CMakeLists.txt]
  63. cmake_minimum_required (VERSION 3.14)
  64.  
  65. project(qt_tsan_tests)
  66.  
  67. set(CMAKE_CXX_STANDARD 14)
  68. set(CMAKE_CXX_STANDARD_REQUIRED ON)
  69.  
  70. set(CMAKE_AUTOMOC ON)
  71. set(CMAKE_AUTORCC ON)
  72.  
  73. add_executable (qt_tsan_tests my_application.h my_application.cpp main.cpp)
  74.  
  75. find_package(Qt5 CONFIG COMPONENTS Widgets REQUIRED) # using Fedora36 default Qt 5.15.6
  76.  
  77. SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -fno-omit-frame-pointer -Wall -Wextra -fsanitize=thread" )
  78. SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fno-omit-frame-pointer -fsanitize=thread")
  79.  
  80. target_link_libraries(qt_tsan_tests PRIVATE Qt5::Widgets)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement