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
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Problem with Win32 Threads

Problem with Win32 Threads

Scheduled Pinned Locked Moved C / C++ / MFC
data-structuresc++securityhelpquestion
7 Posts 4 Posters 0 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.
  • K Offline
    K Offline
    kikoso
    wrote on last edited by
    #1

    Hello all, I´m trying to multithread an application by using CreateThread function. So far I´ve done the following:

    //Here I initialize the Thread
    LPDWORD iID;
    HANDLE hThread; // array of thread handles
    hThread = CreateThread (
    0, // Security attributes
    0, // Stack size
    (LPTHREAD_START_ROUTINE)OpenCVWin32::Form1::Threadi,
    NULL,
    CREATE_SUSPENDED,
    iID);
    }

    //Called function
    static DWORD WINAPI Threadi()
    {
    System::Windows::Forms::MessageBox::Show("Hi there!");
    return 0;
    }

    When I launch the application, the thread is not created, as I cannot go inside the Threadi function. I´m right now using VC++ 2008. Does anyone has a suggestion on why this could be happening? Thanks

    H U R 3 Replies Last reply
    0
    • K kikoso

      Hello all, I´m trying to multithread an application by using CreateThread function. So far I´ve done the following:

      //Here I initialize the Thread
      LPDWORD iID;
      HANDLE hThread; // array of thread handles
      hThread = CreateThread (
      0, // Security attributes
      0, // Stack size
      (LPTHREAD_START_ROUTINE)OpenCVWin32::Form1::Threadi,
      NULL,
      CREATE_SUSPENDED,
      iID);
      }

      //Called function
      static DWORD WINAPI Threadi()
      {
      System::Windows::Forms::MessageBox::Show("Hi there!");
      return 0;
      }

      When I launch the application, the thread is not created, as I cannot go inside the Threadi function. I´m right now using VC++ 2008. Does anyone has a suggestion on why this could be happening? Thanks

      H Offline
      H Offline
      HimanshuJoshi
      wrote on last edited by
      #2

      kikoso wrote:

      CREATE_SUSPENDED,

      This is the problem. If you use this attribute then the thread is created in suspended state. More Info Here[^] If you want the thread to start as soon as you launch the application, don't create a thread in suspended state. If you want to create in suspended state, then you can use ResumeThread() function.

      K 1 Reply Last reply
      0
      • K kikoso

        Hello all, I´m trying to multithread an application by using CreateThread function. So far I´ve done the following:

        //Here I initialize the Thread
        LPDWORD iID;
        HANDLE hThread; // array of thread handles
        hThread = CreateThread (
        0, // Security attributes
        0, // Stack size
        (LPTHREAD_START_ROUTINE)OpenCVWin32::Form1::Threadi,
        NULL,
        CREATE_SUSPENDED,
        iID);
        }

        //Called function
        static DWORD WINAPI Threadi()
        {
        System::Windows::Forms::MessageBox::Show("Hi there!");
        return 0;
        }

        When I launch the application, the thread is not created, as I cannot go inside the Threadi function. I´m right now using VC++ 2008. Does anyone has a suggestion on why this could be happening? Thanks

        U Offline
        U Offline
        User 3919723
        wrote on last edited by
        #3

        LPTHREAD_START_ROUTINE is defined as: typedef DWORD (WINAPI *PTHREAD_START_ROUTINE)( LPVOID lpThreadParameter ); typedef PTHREAD_START_ROUTINE LPTHREAD_START_ROUTINE; so, your thread function is to be: static DWORD WINAPI Threadi(LPVOID lpThreadParameter) { System::Windows::Forms::MessageBox::Show("Hi there!"); return 0; }

        K 1 Reply Last reply
        0
        • U User 3919723

          LPTHREAD_START_ROUTINE is defined as: typedef DWORD (WINAPI *PTHREAD_START_ROUTINE)( LPVOID lpThreadParameter ); typedef PTHREAD_START_ROUTINE LPTHREAD_START_ROUTINE; so, your thread function is to be: static DWORD WINAPI Threadi(LPVOID lpThreadParameter) { System::Windows::Forms::MessageBox::Show("Hi there!"); return 0; }

          K Offline
          K Offline
          kikoso
          wrote on last edited by
          #4

          Hello! Thank you! I didn´t though that passing the parameters will be compulsory. But nevermind, I put it on my code.

          1 Reply Last reply
          0
          • H HimanshuJoshi

            kikoso wrote:

            CREATE_SUSPENDED,

            This is the problem. If you use this attribute then the thread is created in suspended state. More Info Here[^] If you want the thread to start as soon as you launch the application, don't create a thread in suspended state. If you want to create in suspended state, then you can use ResumeThread() function.

            K Offline
            K Offline
            kikoso
            wrote on last edited by
            #5

            Thank you HimanshuJoshi. I was reading in zig-zag the doc, so I didn´t realize about it. I still have some problems. After changing it, I have a problem accessing the memory. This is the error I got (after using 0 instead of CREATE_SUSPENDED,: Unhandled exception at 0x003628c2 in OpenCV Win32.exe: 0xC0000005: Access violation reading at position 0x0000000c. I´ve been thinking about sending the 3 parameter as (LPTHREAD_START_ROUTINE)&OpenCVWin32::Form1::Threadi, But I got a different error: error C3374: Address of "OpenCVWin32:: Form1:: Threadi" can only be adopted if a delegate instance is created. I will appreciate any suggestions. Thank you :)

            1 Reply Last reply
            0
            • K kikoso

              Hello all, I´m trying to multithread an application by using CreateThread function. So far I´ve done the following:

              //Here I initialize the Thread
              LPDWORD iID;
              HANDLE hThread; // array of thread handles
              hThread = CreateThread (
              0, // Security attributes
              0, // Stack size
              (LPTHREAD_START_ROUTINE)OpenCVWin32::Form1::Threadi,
              NULL,
              CREATE_SUSPENDED,
              iID);
              }

              //Called function
              static DWORD WINAPI Threadi()
              {
              System::Windows::Forms::MessageBox::Show("Hi there!");
              return 0;
              }

              When I launch the application, the thread is not created, as I cannot go inside the Threadi function. I´m right now using VC++ 2008. Does anyone has a suggestion on why this could be happening? Thanks

              R Offline
              R Offline
              Rajesh R Subramanian
              wrote on last edited by
              #6

              If you're using MFC, you *must* use AfxBeginThread to begin a worker thread. If you are using Win32, you should be using _beginthreadex to create a thread. CreateThread is an API and that will even create a thread if you're linking to the single threaded version of the CRT (which will inevitably lead to disaster). If you're writing managed code, take a look at Thread.Start method in the documentation. Basically, I want to emphasise that there's no sane reason or excuse to use CreateThread from your code. There are safer and sane alternatives.

              “Follow your bliss.” – Joseph Campbell

              K 1 Reply Last reply
              0
              • R Rajesh R Subramanian

                If you're using MFC, you *must* use AfxBeginThread to begin a worker thread. If you are using Win32, you should be using _beginthreadex to create a thread. CreateThread is an API and that will even create a thread if you're linking to the single threaded version of the CRT (which will inevitably lead to disaster). If you're writing managed code, take a look at Thread.Start method in the documentation. Basically, I want to emphasise that there's no sane reason or excuse to use CreateThread from your code. There are safer and sane alternatives.

                “Follow your bliss.” – Joseph Campbell

                K Offline
                K Offline
                kikoso
                wrote on last edited by
                #7

                Hello, I finally found the problem. The code for the Thread Function was inside the ref class, so that made it managed code. I finally put outside it, and now is working fine :) Thank you.

                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