CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 19

Thread: Casting

  1. #1
    Join Date
    Nov 2003
    Posts
    42

    Casting

    I am writing a thread to control a timer. I need to pass "theEvent" and "theLock" to this thread. where "theEvent" and "theLock" are
    theEvent = new CEvent(FALSE,FALSE, "Something happened");
    theLock = new CSingleLock(theEvent);
    How can i cast these together and pass them to the thread
    UINT StartClock(LPVOID pParam)??
    Is it possible??

  2. #2
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Yes...by using a structure...

  3. #3
    Join Date
    Nov 2003
    Posts
    42

    Casting

    Can anyone advise my on how I may use a structure to achieve this??

  4. #4
    Join Date
    Aug 2001
    Location
    Stockholm, Sweden
    Posts
    1,664
    Do as Andreas told you, wrap them in a structure and pass the pointer to the structure, to your thread func. Like this:
    Code:
    typedef struct {
        HANDLE theEvent;
        HANDLE theLock;
    } ThreadData;
    
    .
    .
    .
    
    td = malloc(sizeof(ThreadData));
    td->theEvent = ....
    td->theLock = ...
    _beginthread(bla bla bla bla... , td);
    Free the data in your thread func when not used anylonger.

  5. #5
    Join Date
    Jun 2003
    Location
    Armenia, Yerevan
    Posts
    720
    Code:
    struct ThreadData
    {
     CEvent *pEvent;
     CSingleLock *pSngLock;
    };
     .
     .
    ThreadData data;
     .
     .
    UINT StartClock(LPVOID param)
    {
     ThreadData *pData = reinterpret_cast<ThreadData*>(param);
     .
     .
    }
     .
     .
     data.pEvent = theEvent = new CEvent(..);
     data.pSngLock = theLock = new CSingleLock(..);
    
     AfxBeginThread(StartClock, &data);
    Last edited by AvDav; November 6th, 2003 at 02:47 PM.

  6. #6
    Join Date
    Nov 2003
    Posts
    42

    Catsing

    Thanx a lot for that it has solved the problem. Now that i can begin the thread I need it to draw a digital clock to the view every 5 seconds. I 'm gussing i need to use a timer to do this, Can u give me a guide on how to set it up?? I can succesfull get the sytem time and print it to the view but once I start usingmyn thread to try and achieve the udated it all goes wrong. Sorry if this seems simple, I have only started to work with MFC and C++
    Thanx

  7. #7
    Join Date
    Nov 2003
    Posts
    42
    So far i have come up with the following code:

    UINT StartClock(LPVOID pParam)
    {
    ThreadData *pData = reinterpret_cast<ThreadData*>(pParam);
    LARGE_INTEGER theTime;
    theTime.QuadPart = -100000000;
    void CALLBACK wakeUp(LPVOID lpArgToCompletionRoutine, DWORD dwTimerLowValue, DWORD dwTimerHighValue);
    HANDLE timerhand = CreateWaitableTimer(NULL, false,"Clock Timer");
    BOOL status = SetWaitableTimer (timerhand, &theTime, 5000,wakeUp ,pParam , 0);
    return 1;
    }
    I do not know what to put in the CALLBACK wakeUp section. Im guessing that i need to call the Event that will draw to the screen. How can I do this??

  8. #8
    Join Date
    Dec 2002
    Posts
    126
    I thought it was a Bad Thing in MFC/Windows-in-general to have more than one thread attempt to draw to the screen? If that's true, then your thread can't draw the clock.

    Why are you doing the clock as a seperate thread, anyway? Wouldn't it work better to have that in the main thread? If the main thread is being held off due to other processing, it seems like you have it backwards. The slow work is done in the secondary thread, while the main thread keeps the UI running (including the clock).

  9. #9
    Join Date
    Nov 2003
    Posts
    42
    It might not be a good idea to do the clock in a new thread but its an exercise that we have been asked to do for my degree. I'm guessing that we have been asked to do this to learn how to build threads, how to create an event etc. Any help would be most appreciated.
    thanx

  10. #10
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Originally posted by AvDav
    Code:
    ThreadData data;
     .
     .
    UINT StartClock(LPVOID param)
    {
     ThreadData *pData = reinterpret_cast<ThreadData*>(param);
     .
     .
    }
    Two things here...
    • There is no need for the unsafe 'reinterpret_cast'...use 'static_cast' instead.
    • You would need to make sure that the passed structure 'data' lives at least as long as the thread.

  11. #11
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    And to add a small improvement to avoid having memory leaks...

    Usually you allocate the structure you wish to pass on the heap (thus dynamically). Then before the thread terminates, you would release the previous allocated memory. However, this might resolve in memory leaks if the thread function terminates unexpected (like a function throws an exception etc.) since your 'delete' statement will not be processed in this case. To avoid this, you can use an auto pointer instead...
    Code:
    #include <memory>
    
    struct MyStruct
    {
      int m_Int;
    
      MyStruct(int i) { m_Int = i; }
    };
    
    // Create thread
    MyStruct *pData = new MyStruct(10);
    
    AfxBeginThread(MyThreadFunc, pData);
    
    UINT MyThreadFunc(void *pvParam)
    {
      std::auto_ptr<MyStruct> Struct(static_cast<MyStruct*>(pvParam));
      {
        .....
      }
    
      return 0;
    }
    The above code will not leaking memory disregarding how the thread function terminates...

  12. #12
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Originally posted by Daviesrt
    So far i have come up with the following code:

    UINT StartClock(LPVOID pParam)
    {
    ThreadData *pData = reinterpret_cast<ThreadData*>(pParam);
    LARGE_INTEGER theTime;
    theTime.QuadPart = -100000000;
    void CALLBACK wakeUp(LPVOID lpArgToCompletionRoutine, DWORD dwTimerLowValue, DWORD dwTimerHighValue);
    HANDLE timerhand = CreateWaitableTimer(NULL, false,"Clock Timer");
    BOOL status = SetWaitableTimer (timerhand, &theTime, 5000,wakeUp ,pParam , 0);
    return 1;
    }
    I do not know what to put in the CALLBACK wakeUp section. Im guessing that i need to call the Event that will draw to the screen. How can I do this??
    Well...why is there a function declaration of a callback function within the thread?

    This should not even compile...furthermore I honestly do not understand what you are trying to achieve. A clarification might be helpful...

  13. #13
    Join Date
    Nov 2003
    Posts
    42

    Casting

    Thankyou your answers have been very helpfull. Truthly I am not sure what I am doing, which may be why I am asking alot of silly questions. I have been given a task of designing an MFC application that will display a digital clock in the window. The clock should update itself every 5 seconds. I have to use a thread to do this. Hope this explains my problem better. Feel free to ask any further questions. I'm hopping that you could help me out with this problem.
    Thanx

  14. #14
    Join Date
    Dec 2002
    Posts
    126

    Re: Casting

    Originally posted by Daviesrt
    Thankyou your answers have been very helpfull. Truthly I am not sure what I am doing, which may be why I am asking alot of silly questions. I have been given a task of designing an MFC application that will display a digital clock in the window. The clock should update itself every 5 seconds. I have to use a thread to do this. Hope this explains my problem better. Feel free to ask any further questions. I'm hopping that you could help me out with this problem.
    Thanx
    Well... the single thread solution would be pretty easy. Assuming you have a clock control, and a way to trigger it to update the time:

    - Start a timer. On timer expiry, update the clock.

    If you are required to use a thread it depends on if the clock is threadsafe or not. If it is, the code is pretty much the same, except your secondary thread is updating the clock. If not, then perhaps something like:

    - Start a thread. In the thread, start a timer. When the timer expires, send a message to the main thread. When the main thread receives that message, updates the clock.

    I don't know why you'd use two threads for this, unless the point is to not use the timer functions. In that case, you could have the secondary thread poll the time from the OS. Doing so will still eat up tons of CPU time, but since it is in a secondary thread it won't lock up the whole program.

  15. #15
    Join Date
    Nov 2003
    Posts
    42

    Casting

    Thanks for the reply.
    I think the method i am trying to use is the one shown below:
    Start a thread. In the thread, start a timer. When the timer expires, send a message to the main thread. When the main thread receives that message, updates the clock.

    I am also required to create an event and a singlelock, i expect this is where the timer gets udated. Should the thread only contain the timing mechanism, and everything else be contained in the main thread??
    What should be contained in the Event and lock??
    Thanx
    Last edited by Daviesrt; November 6th, 2003 at 07:56 PM.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured