Hadi
Hadi

Reputation: 307

Use Multithreads in MFC C++

I amnew to using multithreads on CPU, MFC C++. I am trying to initialize two of the CWinThreads and run them simultaneously as worker threads, here it is in my header file

    CWinThread      *m_thread;  
    CWinThread      *m_thread1; 

In my cpp file i am trying to call them like this

    CdecoderD3D9App *decoder_object_1 = new CdecoderD3D9App(460, 1);
    m_thread = AfxBeginThread(MainTread, decoder_object_1);

    CdecoderD3D9App *decoder_object_2 = new CdecoderD3D9App(460, 2);
    m_thread1 = AfxBeginThread(MainTread1, decoder_object_2);

CdecoderD3D9App is my app class that i am passing to the threads and running its functions my threads functions are like this

UINT Ctesting_projectDlg::MainTread(LPVOID pParam)

{
    clock_t t1, t2;
    t1 = clock(); 
    CdecoderD3D9App *decoder_object_1 = (CdecoderD3D9App *)pParam;
    char *video_source = "my_movie.mp4";



    decoder_object_1->InitInstance();

    decoder_object_1->run_program(video_source);


    t2 = clock(); 
    float diff = (((float)t2 - (float)t1) / 1000000.0F ) * 10;


    return 0;
}


UINT Ctesting_projectDlg::MainTread1(LPVOID pParam)

{
    clock_t t1, t2;
    t1 = clock(); 
    CdecoderD3D9App *decoder_object_3 = (CdecoderD3D9App *)pParam;

    char *video_source = "my_movie.m2v";



    decoder_object_3->InitInstance();




    decoder_object_3->run_program(video_source);
    t2 = clock(); 
    float diff = (((float)t2 - (float)t1) / 1000000.0F ) * 10;

    return 0;
}

When I am calling the thread functions only the second thread is running, the first thread start to run but as soon as the second thread is call upon the first thread stops. Is there anyway i can run them both simultaneously? I have to run basically four threads like these simultaneously in the program. Thank you.

Upvotes: 2

Views: 3822

Answers (2)

ANjaNA
ANjaNA

Reputation: 1426

Try this,

CWinThread *pThread;
pThread = AfxBeginThread (TestStartThread
                             ,param,THREAD_PRIORITY_NORMAL,0,0,NULL);

here, TestStartThread is the function and param is a pointer to the TestStartThread s' input arguments.

define TestStartThread(...) like this,

static UINT TestStartThread (LPVOID param);

call what ever your function inside TestStartThread static function as desired.

If you are new refer http://www.codeproject.com/Articles/14746/Multithreading-Tutorial

Upvotes: 3

SmacL
SmacL

Reputation: 22932

It looks like your worker threads are actually GUI threads, based on the InitInstance and run_program(video_source). While you can have technically have multiple gui threads under MFC, you might be better off with separate processes and some inter-process communications.

Upvotes: 0

Related Questions