Thread: Starting/Ending a Process

  1. #1
    Registered User
    Join Date
    Mar 2006
    Location
    USA::Colorado
    Posts
    155

    Starting/Ending a Thread

    Hey everyone,

    I'm working on a program in Visual C++ 2k8 and I've got a button that is responsible for creating and ending a thread. It's controlled by a switch statement that changes a boolean from true to false and depending on the value of the boolean, its going to start or a thread.

    I'm using the function AfxBeginThread to start my thread, I'm just confused how to end it (how can I scan for the button being pressed again, or how do I stop the thread. I've been warned not to use AfxEndThread because it's somehow dangerous).

    Any help will be greatly appreciated.

    Thanks,

    Matt
    Last edited by guitarist809; 03-15-2008 at 11:28 PM.
    ~guitarist809~

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981

  3. #3
    Registered User
    Join Date
    Mar 2006
    Location
    USA::Colorado
    Posts
    155
    Thanks for the help codeplug,

    MSDN is telling me to use AfxEndThread, however, it looks like there is no way to terminate an individual thread (I've got more than one running)...

    Maybe I'm doing this all wrong...

    I'll just say what I'm trying to do.

    I'm making a program called AntiLeet, which monitors your keystrokes and scans them for phrases such as "lol" (these are defined in a file).

    I've got one thread that constantly monitors the keys pressed, and puts them into a global vector called keys (25 last pressed keys).

    The button activates and disables the scanner (which scans the pressed keys). I need to be able to disable/enable it because there will be variable changes.


    Is there a way for a thread to access a variable when its running? (I'm new at this, sorry). Say, the thread has been running for a few seconds, but someone clicked a checkbox on a dialog box, is there a way to tell the thread this (or have it look it up)?

    Thanks,

    Matt
    ~guitarist809~

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Don't confuse AfxEndThread with TerminateThread. TerminateThread is the dangerous one. Calling AfxEndThread is the same as just returning from the thread function.

    One option is to use a CEvent to signal a thread to exit.

    gg

  5. #5
    Registered User
    Join Date
    Mar 2006
    Location
    USA::Colorado
    Posts
    155
    When I call:
    Code:
    AfxEndThread(0, true);
    The program terminates, and a debug window comes up.

    Why?
    ~guitarist809~

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I think that is difficult to say. A million things can go wrong.
    Perhaps you try to be a little more specific as to what happen?
    What kind of error do you get, for example?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Are you calling it inside one of the threads you created? Or are you calling it in the "main" thread of the application?

    Calling AfxEndThread will end the current thread of execution.

    gg

  8. #8
    Registered User
    Join Date
    Mar 2006
    Location
    USA::Colorado
    Posts
    155
    Quote Originally Posted by Codeplug View Post
    Are you calling it inside one of the threads you created? Or are you calling it in the "main" thread of the application?

    Calling AfxEndThread will end the current thread of execution.

    gg
    That's it, I was calling it in the main program, which killed everything. Explains a lot...

    For some reason, yesterday, nothing was making sence, if I just use a global and have the thread access that, everything works. Dont know why I didn't see that yesterday =\

    Thanks for the help everyone
    ~guitarist809~

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by guitarist809 View Post
    For some reason, yesterday, nothing was making sence, if I just use a global and have the thread access that, everything works. Dont know why I didn't see that yesterday =\
    Wait. Are you saying two threads are using the same variable?
    Have you used some kind of synchronization?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Mar 2006
    Location
    USA::Colorado
    Posts
    155
    Quote Originally Posted by Elysia View Post
    Wait. Are you saying two threads are using the same variable?
    Have you used some kind of synchronization?
    I'm too noobish for synchronization. I looked at some of the MSDN example source code and said forget it.

    using Afx threads is extremely easy, and the program works
    ~guitarist809~

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Afx threads are threads. Threads are threads.
    Are two threads accessing the same variable? If they do, then you need synchronization.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    If they do, then you need synchronization.
    It is not so explicit... one can write (with atomic write) and second can read (with atomic read) - in this case - no locking is needed...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    But that is one sort of synchronization. In a way, at least.
    So an atomic operation, a critical section, a mutex, or something like that is needed to avoid race conditions.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    atomic operation
    by atimic operation I mean here one assembler command... no lock is needed

    if I have in one thread
    flag = 1

    and in the another
    while(flag == 0)

    I definitely do not want to add locking here
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  15. #15
    Registered User
    Join Date
    Nov 2002
    Posts
    319
    button states is the answer , look for the messages for buttons
    switch(wParam)
    {
    case YOUR_BUTTON_ID:
    }
    why dont you use CreateThread as well ? , then you can have SuspendThread or TerminateThread
    Last edited by Anddos; 03-20-2008 at 05:51 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. init adopts zombie process?
    By password636 in forum Linux Programming
    Replies: 4
    Last Post: 07-01-2009, 10:05 AM
  2. create a child process that creates a child process
    By cus in forum Linux Programming
    Replies: 9
    Last Post: 01-13-2009, 02:14 PM
  3. Replies: 3
    Last Post: 10-15-2008, 09:24 AM
  4. Problem with forking a process
    By Unitedroad in forum C Programming
    Replies: 10
    Last Post: 10-04-2007, 01:43 AM
  5. process programming
    By St0rM-MaN in forum Linux Programming
    Replies: 2
    Last Post: 09-15-2007, 07:53 AM