Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

Multitasking in VC++ 6(threads by AfxBeginThread())

Last post 04-08-2008 1:46 PM by chong. 0 replies.
Page 1 of 1 (1 items)
Sort Posts: Previous Next
  • 04-08-2008 1:46 PM

    • chong
    • Not Ranked
    • Joined on 01-22-2008
    • United Kingdom
    • Junior Member
    • Points 110

    Multitasking in VC++ 6(threads by AfxBeginThread())

     Hello guys

    I am learning VC++ 6.0 which is not an easy language at all.  I have just learnt how to create threads using AfxBeginThread( ).  I have listed here my own example program (multitask.cpp) to show the use of threads.  Hopefully the program is simple and self explannary to eveybody.  I would like to share what I have learnt!!  I would be grateful if anybody can comment on my program and may suggest a way to improve it.

    All the best

    Chong

    P.C.

    If anybody wants another version of this program which uses _beginthread() instead of AfxBeginThread(), let me know.

    +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 

    // multitask.cpp

    // Note : Before building for an executable file, do the following (after compiling):
    //   (1) Choose the [Project] menu
    //   (2) Choose [Settings]
    //   (3) Choose [General] and select [Use MFC in a Static Library]
    //       for [Microsoft Foundation Classes].

    // Note that, when the main application exits/terminates, it kills all its
    // threads.

    // the test.txt file contains "abcdefghi" 

    #include <stdio.h>
    #include <stdlib.h>
    #include <fcntl.h>
    #include <io.h>

    #include <Afxwin.h> // for CWinThread

    #include <windows.h> // for Sleep()
    #include <winbase.h> // for Sleep()


    UINT ThreadFunc(LPVOID pParam);


    class MsgFromFile {
    private:
     int fp; char buf[100];
    public:
     BOOL OpenDataFile() {
      if ((fp=open("test.txt",O_RDONLY))<0) return FALSE;
       else return TRUE;
     }
     BOOL ReadDataFile(){
      int nbytes, k =0;
      while (TRUE){
       if ((nbytes=read(fp,buf,3-k))<0) return FALSE;
       else if (nbytes == 0) {buf[k]=NULL;return TRUE;}
       else if ((k=k+nbytes)<3) continue;
       break;
      }
      buf[k]=NULL;
      return TRUE;
     }
     void WriteMsg(){ printf("Thread:%s\n",buf);}
    };


    char gMsg[100]; // global to the threads too!


    main()
    {
     MsgFromFile a;
     int iPriority = 0;
     CWinThread* ThreadID[2];

     a.OpenDataFile();
     a.ReadDataFile();
     printf("The main application: ");
     a.WriteMsg();

     strcpy(gMsg,"This works");

     //Start the independent threads
     ThreadID[0] = AfxBeginThread(ThreadFunc,(LPVOID)&a,iPriority);
     ThreadID[1] = AfxBeginThread(ThreadFunc,(LPVOID)&a,iPriority);
     
     //ThreadID[0]->SuspendThread(); //This suspends the thread ThreadID[0].It does not kill
             //the thread.
     //ThreadID[0]->ResumeThread(); //This resumes the thread ThreadID[0].

     //Wait until the threads have died.
     HANDLE hthread;
     hthread= ThreadID[0]->m_hThread;
     WaitForSingleObject(hthread,INFINITE);
     hthread = ThreadID[1]->m_hThread;
     WaitForSingleObject(hthread,INFINITE);

     printf("The main application: "); a.WriteMsg();
     printf("The main application(parent) exits\n");
     Sleep(10000); //Sleep for 10 seconds

    } // main


    UINT ThreadFunc(LPVOID pParam)
    {

     MsgFromFile* myfile = (MsgFromFile*)pParam;

     if (!(myfile->ReadDataFile())) {
      printf("Thread: failed to read the data file\n");
      //exit(-1); //This kills the main application too!
      return 0;
     } // if

     myfile->WriteMsg();
     printf("Thread: %s \n", gMsg);

     Sleep(5000);
     printf("Thread: exits\n");

     return 0;
    } // ThreadFunc
    +++++++++++++++++++++++++++++++++++++++++++++++++++

    • Post Points: 5
Page 1 of 1 (1 items)