Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. ATL / WTL / STL
  4. backgroud worker thread crashes but fine when foreground

backgroud worker thread crashes but fine when foreground

Scheduled Pinned Locked Moved ATL / WTL / STL
c++question
11 Posts 4 Posters 1 Views 1 Watching
  • 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.
  • V VeganFanatic

    I have this benchmark program that started off as a console program and I am now well into making work with a window instead. It's all Win32 for the most part with a smattering of C++ STL for some containers. I launch the background via a keyboard command ctrl-r

    			case (0x52): // R key
    				if(GetKeyState(VK\_LCONTROL) || GetKeyState(VK\_RCONTROL)) {
    					// crtl-r
    					// showtime(); //?
    					\_beginthread(showtime, NULL, NULL);
    				}
    				break;
    

    So what am I doing wrong, the background thread runs for while but crashes, used to work fine.

    http://www.contract-developer.tk

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #2

    What is it the background thread doing? Do you get an exception or any other information when it crashes?

    The best things in life are not things.

    V 1 Reply Last reply
    0
    • L Lost User

      What is it the background thread doing? Do you get an exception or any other information when it crashes?

      The best things in life are not things.

      V Offline
      V Offline
      VeganFanatic
      wrote on last edited by
      #3

      void showtime(void *dummy) {
      computing = true;
      output.clear();
      // Display the copyright
      output = _TEXT("Fibonacci 3.0.0 for Windows");
      output += ToString("/n/l/n/l");
      // f << name << ": " << time << " seconds" << std::endl << std::endl;
      output += _TEXT("(c) 2010-1 583727 BC Ltd, All Rights Reserved.");
      // f << name << ": " << time << " seconds" << std::endl << std::endl;
      output += _TEXT("/n/l");
      output += _TEXT("Free for peronsal use only");
      output += _TEXT("/n/l/n/l");
      output += _TEXT("Commercial and government use requires a license");
      output += _TEXT("/n/l/n/l");
      output += _TEXT("To acheieve consistant results, run this program in the clean boot mode");
      output += _TEXT("/n/l");
      output += _TEXT("Click on start, in the seach box enter 'msconfig' and choose the startup tab, disable everyting and reboot");
      output += _TEXT("/n/l/n/l");
      output += _TEXT("RAM Performance ");
      output += ToString(benchmark(fib2));
      output += _TEXT(" seconds");
      output += _TEXT("/n/l");
      output += _TEXT("Scalar Performance ");
      output += ToString(benchmark(fib3));
      output += _TEXT(" seconds");
      output += _TEXT("/n/l");
      output += _TEXT("CPU Large Multiplication Performance ");
      output += ToString(benchmark(fib4));
      output += _TEXT(" seconds");
      output += _TEXT("/n/l");
      v.resize(0); t.resize(0); // free up some RAM
      output += _TEXT("Recursive Factorial Performance ");
      output += ToString(benchmark(RunFact));
      output += _TEXT(" seconds");
      output += _TEXT("/n/l");
      output += _TEXT("Bubble Sort Performance ");
      output += ToString(benchmark(RunBubbleSort));
      output += _TEXT(" seconds");
      output += _TEXT("/n/l");
      output += _TEXT("Insertion Sort Performance ");
      output += ToString(benchmark(RunInsertionSort));
      output += _TEXT(" seconds");
      output += _TEXT("/n/l");
      output += _TEXT("Shell Sort Performance ");
      output += ToString(benchmark(RunShellSort));
      output += _TEXT(" seconds");
      output += _TEXT("/n/l");
      output += _TEXT("Quick Sort Performance ");
      output += ToString(benchmark(RunQuickSort));
      output += _TEXT(" seconds");
      output += _TEXT("/n/l");
      output += _TEXT("Merge Sort Performance ");
      output += ToString(benchmark(RunMergeSort));
      output += _TEXT(" seconds");
      output += _TEXT("/n/l");
      output += _TEXT("Heap Sort Performance ");
      output += ToString(benchmark(RunHeapSort));
      output += _TEXT(" seconds");
      output += _TEXT("/n/l");
      output += _TEXT("Parallel Bitwise Sieve

      L 1 Reply Last reply
      0
      • V VeganFanatic

        void showtime(void *dummy) {
        computing = true;
        output.clear();
        // Display the copyright
        output = _TEXT("Fibonacci 3.0.0 for Windows");
        output += ToString("/n/l/n/l");
        // f << name << ": " << time << " seconds" << std::endl << std::endl;
        output += _TEXT("(c) 2010-1 583727 BC Ltd, All Rights Reserved.");
        // f << name << ": " << time << " seconds" << std::endl << std::endl;
        output += _TEXT("/n/l");
        output += _TEXT("Free for peronsal use only");
        output += _TEXT("/n/l/n/l");
        output += _TEXT("Commercial and government use requires a license");
        output += _TEXT("/n/l/n/l");
        output += _TEXT("To acheieve consistant results, run this program in the clean boot mode");
        output += _TEXT("/n/l");
        output += _TEXT("Click on start, in the seach box enter 'msconfig' and choose the startup tab, disable everyting and reboot");
        output += _TEXT("/n/l/n/l");
        output += _TEXT("RAM Performance ");
        output += ToString(benchmark(fib2));
        output += _TEXT(" seconds");
        output += _TEXT("/n/l");
        output += _TEXT("Scalar Performance ");
        output += ToString(benchmark(fib3));
        output += _TEXT(" seconds");
        output += _TEXT("/n/l");
        output += _TEXT("CPU Large Multiplication Performance ");
        output += ToString(benchmark(fib4));
        output += _TEXT(" seconds");
        output += _TEXT("/n/l");
        v.resize(0); t.resize(0); // free up some RAM
        output += _TEXT("Recursive Factorial Performance ");
        output += ToString(benchmark(RunFact));
        output += _TEXT(" seconds");
        output += _TEXT("/n/l");
        output += _TEXT("Bubble Sort Performance ");
        output += ToString(benchmark(RunBubbleSort));
        output += _TEXT(" seconds");
        output += _TEXT("/n/l");
        output += _TEXT("Insertion Sort Performance ");
        output += ToString(benchmark(RunInsertionSort));
        output += _TEXT(" seconds");
        output += _TEXT("/n/l");
        output += _TEXT("Shell Sort Performance ");
        output += ToString(benchmark(RunShellSort));
        output += _TEXT(" seconds");
        output += _TEXT("/n/l");
        output += _TEXT("Quick Sort Performance ");
        output += ToString(benchmark(RunQuickSort));
        output += _TEXT(" seconds");
        output += _TEXT("/n/l");
        output += _TEXT("Merge Sort Performance ");
        output += ToString(benchmark(RunMergeSort));
        output += _TEXT(" seconds");
        output += _TEXT("/n/l");
        output += _TEXT("Heap Sort Performance ");
        output += ToString(benchmark(RunHeapSort));
        output += _TEXT(" seconds");
        output += _TEXT("/n/l");
        output += _TEXT("Parallel Bitwise Sieve

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #4

        What is output and why are you putting so much text into it for no apparent reason?

        The best things in life are not things.

        V 1 Reply Last reply
        0
        • L Lost User

          What is output and why are you putting so much text into it for no apparent reason?

          The best things in life are not things.

          V Offline
          V Offline
          VeganFanatic
          wrote on last edited by
          #5

          typedef std::basic_string<wchar_t> tstring; tstring output; The output will eventually be placed on the clipboard as a text

          http://www.contract-developer.tk

          L 1 Reply Last reply
          0
          • V VeganFanatic

            typedef std::basic_string<wchar_t> tstring; tstring output; The output will eventually be placed on the clipboard as a text

            http://www.contract-developer.tk

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #6

            You have still given no information as to where this crashes, and what exceptions or other diagnostic information you see. We cannot guess what happens on your system; have you tried running it through the debugger?

            The best things in life are not things.

            V 1 Reply Last reply
            0
            • L Lost User

              You have still given no information as to where this crashes, and what exceptions or other diagnostic information you see. We cannot guess what happens on your system; have you tried running it through the debugger?

              The best things in life are not things.

              V Offline
              V Offline
              VeganFanatic
              wrote on last edited by
              #7

              I am going to use MessageBox all over the module and see how far it gets. One question, do I need to use the exitthread anywhere?

              http://www.contract-developer.tk

              L 1 Reply Last reply
              0
              • V VeganFanatic

                I am going to use MessageBox all over the module and see how far it gets. One question, do I need to use the exitthread anywhere?

                http://www.contract-developer.tk

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #8

                VeganFanatic wrote:

                do I need to use the exitthread anywhere?

                Impossible to say; it depends what your program is actually trying to do. Are you sure you need multiple threads?

                The best things in life are not things.

                V 1 Reply Last reply
                0
                • L Lost User

                  VeganFanatic wrote:

                  do I need to use the exitthread anywhere?

                  Impossible to say; it depends what your program is actually trying to do. Are you sure you need multiple threads?

                  The best things in life are not things.

                  V Offline
                  V Offline
                  VeganFanatic
                  wrote on last edited by
                  #9

                  Putting that module into a thread was to make the program able to quit at any time. This was to make sure the message handler is able to get messages.

                  http://www.contract-developer.tk

                  1 Reply Last reply
                  0
                  • V VeganFanatic

                    I have this benchmark program that started off as a console program and I am now well into making work with a window instead. It's all Win32 for the most part with a smattering of C++ STL for some containers. I launch the background via a keyboard command ctrl-r

                    			case (0x52): // R key
                    				if(GetKeyState(VK\_LCONTROL) || GetKeyState(VK\_RCONTROL)) {
                    					// crtl-r
                    					// showtime(); //?
                    					\_beginthread(showtime, NULL, NULL);
                    				}
                    				break;
                    

                    So what am I doing wrong, the background thread runs for while but crashes, used to work fine.

                    http://www.contract-developer.tk

                    A Offline
                    A Offline
                    Albert Holguin
                    wrote on last edited by
                    #10

                    From looking at the code you posted, its probably because another thread accesses or destroys output while the thread is still using it. The way you're using the string is also very bad practice since you're forcing it to reallocate memory constantly for the string and this is very inefficient. Why not get all the information you need once, then allocate/format your string? This would allow the thread to run way faster and more efficiently.

                    1 Reply Last reply
                    0
                    • V VeganFanatic

                      I have this benchmark program that started off as a console program and I am now well into making work with a window instead. It's all Win32 for the most part with a smattering of C++ STL for some containers. I launch the background via a keyboard command ctrl-r

                      			case (0x52): // R key
                      				if(GetKeyState(VK\_LCONTROL) || GetKeyState(VK\_RCONTROL)) {
                      					// crtl-r
                      					// showtime(); //?
                      					\_beginthread(showtime, NULL, NULL);
                      				}
                      				break;
                      

                      So what am I doing wrong, the background thread runs for while but crashes, used to work fine.

                      http://www.contract-developer.tk

                      P Offline
                      P Offline
                      Paul M Watt
                      wrote on last edited by
                      #11

                      When you have this run on one thread, does it complete properly? I would do 3 things to trouble shoot and improve the code: 1) Add another command to your console entry, that allows you to start the "showtime" function, and run it one the same thread. Verify that it functions properly. 2) Enable Exceptions. I assume you are using Visual Studio of some sort, if not you will have to figure out how to do this for your debugger. In Visual Studio you can do this at the Debug | Exceptions Menu. Enable: C++ Exceptions, Native Run-Time Checks and Win32 Exceptions. When you run your application in the debugger and it crashes, you will be right at the point in code the exception is thrown. Then you can look at the stack and all of the other helpful information. 3) Break up all of the work done in "showtime" into a smaller pieces. Probably a common function that prints out the basic text and runs the test you initiate. Then in "showtime" you would make multiple calls to your smaller function. This will help make your code more manageable. From looking at your code, my guess is that one of the test routines is returning a NULL or some value that the string functions dont like, or is failing internally.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups