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. Create a blocking function for a thread

Create a blocking function for a thread

Scheduled Pinned Locked Moved C / C++ / MFC
question
6 Posts 5 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.
  • W Offline
    W Offline
    Willem B
    wrote on last edited by
    #1

    i have a thread that is going to keep attention to a variable, once something is changed in the variable it has to respond to it. So this is how my code looks now: BOOL SomeVariable; UINT WatchThread(LPVOID param) { while (TRUE) { if (SomeVariable) { break; } } } ..... AfxBeginThread(WatchThread, (LPVOID *)this, THREAD_PRIORITY_NORMAL); ..... if i run this my program will need 100% processor usage. how can i prevent this? Make a Blocking function of "SomeVariable"? and how? []D [] []D []

    H O V 3 Replies Last reply
    0
    • W Willem B

      i have a thread that is going to keep attention to a variable, once something is changed in the variable it has to respond to it. So this is how my code looks now: BOOL SomeVariable; UINT WatchThread(LPVOID param) { while (TRUE) { if (SomeVariable) { break; } } } ..... AfxBeginThread(WatchThread, (LPVOID *)this, THREAD_PRIORITY_NORMAL); ..... if i run this my program will need 100% processor usage. how can i prevent this? Make a Blocking function of "SomeVariable"? and how? []D [] []D []

      H Offline
      H Offline
      HENDRIK R
      wrote on last edited by
      #2

      I think you should perhaps insert a Sleep command inside your thread for not to consume so much processor power. Beside that, I don't know whether your thread-solution is the most practible one when waiting for a variable to change. Perhaps you could work with events and one of the wait functions (like WaitForSingleObject), setting the event when your computations have finished (take care: in that case your computation should be placed inside the thread because the wait function is blocking)

      1 Reply Last reply
      0
      • W Willem B

        i have a thread that is going to keep attention to a variable, once something is changed in the variable it has to respond to it. So this is how my code looks now: BOOL SomeVariable; UINT WatchThread(LPVOID param) { while (TRUE) { if (SomeVariable) { break; } } } ..... AfxBeginThread(WatchThread, (LPVOID *)this, THREAD_PRIORITY_NORMAL); ..... if i run this my program will need 100% processor usage. how can i prevent this? Make a Blocking function of "SomeVariable"? and how? []D [] []D []

        O Offline
        O Offline
        OBRon
        wrote on last edited by
        #3

        Spinning in a tight loop waiting for a variable to change is not the proper way to notify a thread to begin doing something. Read up on thread synchronization techniques; events and semaphores and the method WaitForSingleObject. If you must do it the way you've described, put a tiny sleep (Sleep(10)) in your while loop. Ron Ward

        W 1 Reply Last reply
        0
        • O OBRon

          Spinning in a tight loop waiting for a variable to change is not the proper way to notify a thread to begin doing something. Read up on thread synchronization techniques; events and semaphores and the method WaitForSingleObject. If you must do it the way you've described, put a tiny sleep (Sleep(10)) in your while loop. Ron Ward

          W Offline
          W Offline
          Willem B
          wrote on last edited by
          #4

          the reason why i want it to be a blocking process is because i want to recreate the windows messaging device, so make my own messagelist. this list will be read by a procedure for the control of a database. i wanted to make the function that reads the messagelist like the function recv(for network communication), the function recv lockes up the while loop until it has received information. i hope that something like this is possible... []D [] []D []

          P 1 Reply Last reply
          0
          • W Willem B

            the reason why i want it to be a blocking process is because i want to recreate the windows messaging device, so make my own messagelist. this list will be read by a procedure for the control of a database. i wanted to make the function that reads the messagelist like the function recv(for network communication), the function recv lockes up the while loop until it has received information. i hope that something like this is possible... []D [] []D []

            P Offline
            P Offline
            palbano
            wrote on last edited by
            #5

            i don't really get what ur doing but maybe this is sort of what u are looking for? UINT WatchThread(LPVOID param) { while(1){ myclass* pthis = (myclass*)param; int nmsg = pthis->getMsg(); // do something here } } int cmyclass::getMsg(){ ::WaitForSingleObject( _event, INFINITE); return _msgNumber; } void myclass::setMsg( int nmsg) { // lock a critical section or use whatever synchronization needed _msgNumber = nmsg; SetEvent( _event); // unlock the critical section or whatever }

            "No matter where you go, there your are..." - Buckaoo Banzi

            -pete

            1 Reply Last reply
            0
            • W Willem B

              i have a thread that is going to keep attention to a variable, once something is changed in the variable it has to respond to it. So this is how my code looks now: BOOL SomeVariable; UINT WatchThread(LPVOID param) { while (TRUE) { if (SomeVariable) { break; } } } ..... AfxBeginThread(WatchThread, (LPVOID *)this, THREAD_PRIORITY_NORMAL); ..... if i run this my program will need 100% processor usage. how can i prevent this? Make a Blocking function of "SomeVariable"? and how? []D [] []D []

              V Offline
              V Offline
              valikac
              wrote on last edited by
              #6

              There are several solutions. One solution is an event kernel object and WaitForSingleObject(). Use ::SetEvent() and ::ResetEvent() to signal the object. Kuphryn

              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