Skip to content

C++ Gurus

The forum for all discussions in C++ land.
1.3k Topics 8.6k Posts
  • This topic is deleted!

    Unsolved
    1
    0 Votes
    1 Posts
    927 Views
    No one has replied
  • Mutex, please explain what's wrong

    Unsolved
    9
    0 Votes
    9 Posts
    1k Views
    Q
    @JonB said in Mutex, please explain what's wrong: If this is your precise objective I think you cannot achieve this with a single mutex. It seems to me you have 3 alternatives: Use semaphores. I see this is the "accepted" solution at How Thanx, Semaphors are on the list of my styding. Also I have fully resolved the problem by numerating threads and blocking "last processed thread" to work twice: #include <QCoreApplication> #include <QDebug> #include <QMutex> #include <QThread> #include <QObject> #include <QDebug> #include <QMutexLocker> QMutex mutex; int count = 0; int last_thread_num = 0; //================= class thread1_tst : public QThread { Q_OBJECT public: int _thread_num; int& _last_thread_num; explicit thread1_tst(int thead_num, int& last_thread_num, QObject *parent = nullptr); void run(); void my_work(); }; //------------------------------ thread1_tst::thread1_tst(int thread_num, int& last_thread_num, QObject *parent) : QThread{parent}, _thread_num(thread_num), _last_thread_num(last_thread_num) {} //------------------------------ void thread1_tst::run() { while(1) { if(_thread_num != _last_thread_num) my_work(); else msleep(1); } } //------------------------------ void thread1_tst::my_work() { { QMutexLocker locker(&mutex); for(int i=0; i < 5; i++) qDebug() << "thread 1 out: count=" << count++ << " (" << i << ")"; qDebug() << "----end 1 -----"; } msleep(1000); last_thread_num = _thread_num; } //================= class thread2_tst : public QThread { Q_OBJECT public: int _thread_num; int& _last_thread_num; explicit thread2_tst(int thead_num, int& busy_fl, QObject *parent = nullptr); void run(); void my_work(); }; thread2_tst::thread2_tst(int thread_num, int& last_thread_num, QObject *parent) : QThread{parent}, _thread_num(thread_num), _last_thread_num(last_thread_num) {} void thread2_tst::run() { while(1) { if(_thread_num != _last_thread_num) my_work(); else msleep(1); } } //------------------------------ void thread2_tst::my_work() { { QMutexLocker locker(&mutex); for(int i=0; i < 5; i++) qDebug() << "thread 2 out: count=" << count++ << " (" << i << ")"; qDebug() << "----end 2 -----"; } msleep(1000); last_thread_num = _thread_num; } //============================= thread1_tst thread1(0, last_thread_num); //make threads enumerated thread2_tst thread2(1, last_thread_num); int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); thread1.start(); thread2.start(); return a.exec(); } #include "waitconditions.moc"
  • Nested Control Structures in C++: Finding Prime Numbers in a Range

    Unsolved
    3
    0 Votes
    3 Posts
    908 Views
    JonBJ
    @J-Hilk One difference to be aware of is that Eratosthenes or similar uses storage (primes[]) for its computation where the OP's does not. This might become costly if upper bound is large? I refused to use this approach on my computations of primes into the 100s of billions for this reason! I picked a compromise where I store all the primes as I calculate them for future re-use, but not maintain an array holding true/false for every number in the range. @Sachin-Bhatt for (int i = 5; i * i <= n; i += 6) { if (n % i == 0 || n % (i + 2) == 0) { return false; } } I do not recall coming across this approach/algorithm/equation. Can you provide a reference link, please? I wonder how it compares against my way of storing all the previous primes and doing a MOD using just them? You visit every number in steps of 6 and do 2 MODs. That is still a lot of numbers to visit if n is into the trillions?
  • QMessage box in console app slot

    Solved
    3
    0 Votes
    3 Posts
    437 Views
    Q
    @J-Hilk said in QMessage box in console app slot: @qAlexKo set the quitOnLastWindowClosed property to false It works, thank you!
  • How to get detailed fail messages when using ifstream.fail() in C++

    Moved Unsolved c++ ifstearm messages fail
    4
    0 Votes
    4 Posts
    10k Views
    J
    In C++, you can use the std::ifstream::fail() function to check if there was a failure during file operations. However, to get more detailed error messages, you can use the std::ifstream::rdstate() function in combination with std::strerror() or std::perror(). Here's an updated version of your code with more detailed error messages: #include <iostream> #include <fstream> #include <cstring> void open_file(std::ifstream& ifstream, const std::string& file_name) { ifstream.open(file_name); if (ifstream.fail()) { // Get the error code std::ios_base::iostate state = ifstream.rdstate(); // Check for specific error bits if (state & std::ios_base::eofbit) { std::cout << "End of file reached." << std::endl; } if (state & std::ios_base::failbit) { std::cout << "Non-fatal I/O error occurred." << std::endl; } if (state & std::ios_base::badbit) { std::cout << "Fatal I/O error occurred." << std::endl; } // Print system error message std::perror("Error: "); return; } std::cout << "File was opened successfully!" << std::endl; } In this code, we use std::ios_base::iostate to retrieve the error state of the ifstream object. Then, we check specific error bits like std::ios_base::eofbit, std::ios_base::failbit, and std::ios_base::badbit to provide more detailed information about the failure. The std::perror() function is used to print the system error message corresponding to the error code. This will give you additional information about what went wrong. Remember to include the necessary headers for this code to work: #include <iostream> #include <fstream> #include <cstring>
  • This topic is deleted!

    1
    0 Votes
    1 Posts
    3 Views
    No one has replied
  • This topic is deleted!

    Solved
    2
    0 Votes
    2 Posts
    4 Views
  • error LNK2005:... already defined in xxy.lib(xxz.obj)

    Unsolved
    8
    0 Votes
    8 Posts
    2k Views
    A
    @Alcor I remove the following line add_compile_definitions(QT_STATIC) in the CMakelists.txt (in the library files folder, not the client folder), and the link error goes away. Yet to figure out if my .exe works as expected.
  • Read MQTT topic at app start

    Unsolved
    4
    0 Votes
    4 Posts
    600 Views
    jsulmJ
    @qAlexKo said in Read MQTT topic at app start: must be more clear and be in the app contructor I disagree. If you use communication like MQTT you should not do everything in a constructor of a class. Assynchronous approach is the right one, especially when using assynchronous frameworks like Qt.
  • Assessment of the difficulty in porting CPU architecture for QT5

    Unsolved
    1
    0 Votes
    1 Posts
    167 Views
    No one has replied
  • Need a function to extract Windows Name & Version in C/C++

    Unsolved
    3
    0 Votes
    3 Posts
    2k Views
    Chris KawaC
    GetVersionEx is not a reliable way to get OS version. Rather what your OS tells your app what it is, and that can vary in certain compatibility conditions. Starting with Windows 8.1 you need a version manifest in your app. Without a manifest GetVersionEx will return 6.2 for any Windows 8 and above. Even if you use a manifest you get 10.0 for Windows 10 and above. To detect Windows 11 you need to look at build number. Anything above 22000 is Windows 11. There are of course a lot of different versions of 10 and 11 and things will most likely change again with 12. Of course in compatibility mode scenarios the OS will lie to you and give you different versions, ignoring the manifest if needed. That's basically why detecting OS version is generally not a good idea. Your app shouldn't care. It should care about availability of required features, and that you do by first targeting a defined minimum supported OS version by using appropriate SDK version and then dynamically loading libraries with features from above that version and checking if a query for the specific functions resolve or not.
  • This topic is deleted!

    Unsolved
    1
    0 Votes
    1 Posts
    78 Views
    No one has replied
  • This topic is deleted!

    Unsolved
    1
    0 Votes
    1 Posts
    19 Views
    No one has replied
  • Can anybody explain below statment ?

    Solved
    3
    0 Votes
    3 Posts
    497 Views
    Chris KawaC
    All symbols in C++ that start with __ are implementation specified. In other words they are not portable. __attribute__((ATTRIBUTE)) is a GNU extension of C++ that lets you put non-standard attributes on types. One of those attributes used here is alignment. Other vendors have other means of doing that. For example MSVC has #pragma pack(32) for the same thing. Starting from C++11 we also have a standard way of specifying alignment with alignas(32), so these vendor specific extensions are no longer needed, but a lot of older code still uses them. As for alignment itself that just means that the variable will be placed at an address aligned to the value specified. In other words if you print the address of the variable Can1MessageRAM it's going to be a multiple of 32. If you place two class members with alignment attribute the compiler might pad the space between them so that their address is the multiple of specified value. For example if you had a struct like this: struct A { uint8_t Can1MessageRAM1[712U]; uint8_t Can1MessageRAM2[712U]; }; and printed the offsets of its fields: qDebug() << offsetof(A, Can1MessageRAM1) << offsetof(A, Can1MessageRAM2); a possible output would be 0 712 i.e. the second member starts right after the first. If you align them like this: struct A { alignas(32) uint8_t Can1MessageRAM1[712U]; alignas(32) uint8_t Can1MessageRAM2[712U]; }; the output may change to be like this: 0 736, so compiler inserts some space between the two members, so that the address of the second is aligned to 32. Alignment has many uses. Some techniques, like using SSE requires aligned variables to work. On some hardware unaligned access is less performant because there's no dedicated silicon in the CPU, so it has to compute the unaligned address or split operation to aligned parts. Another usage is when you have a platform with fixed memory addresses mapping to specific hardware functions. Yet another use of aligned data is when you iterate over large dataset. Aligned elements might fit better into a cache line improving access performance. Many other uses exist.
  • cannot convert argument 1 from 'QWidget *' to 'QObject *'

    Solved
    4
    0 Votes
    4 Posts
    1k Views
    A
    @J-Hilk said in cannot convert argument 1 from 'QWidget *' to 'QObject *': QObject *parent Thank You. It worked.
  • if using friend function and class we can access private data then how to avoid it ?

    Solved
    4
    0 Votes
    4 Posts
    476 Views
    JonBJ
    @Qt-embedded-developer If you want to access private data you use friend. If you do not use friend you cannot access private data. What is the question? No, you cannot access private data and yet not use friend, else what else would it be for? If there was a different keyword which did this we would say so...
  • when to use below type of macro ?

    Solved
    5
    0 Votes
    5 Posts
    627 Views
    M
    @Qt-embedded-developer said in when to use below type of macro ?: #define NULL_PTR ((void *) 0L) In C, the macro NULL may have the type void*, but that is not allowed in C++. see: https://en.cppreference.com/w/cpp/types/NULL
  • Qt1.0 source code

    Solved
    13
    0 Votes
    13 Posts
    2k Views
    C
    @SimonSchroeder Thank you very much for your valuable and useful advice, I will try it.
  • Waiting for qt signal in c++ function

    Solved
    9
    0 Votes
    9 Posts
    961 Views
    N
    @Chris-Kawa said in Waiting for qt signal in c++ function: @NewbieQTUser You can also connect a function: void fnx() { client->doSomeWork(); QObject::connect(client, &ClientClass::done, &doStuff); } void doStuff() { // following operations } Also thx, looks like a good solution
  • jump to another condition from for loop in qt creator

    Unsolved
    8
    0 Votes
    8 Posts
    849 Views
    MijazM
    @jsulm I tried to address the points.