无法使用windows.h创建挂起的线程

时间:2014-08-21 17:48:02

标签: c++ multithreading winapi inheritance

我搜索了一个答案但却找不到答案。我在线程上工作。我有一个线程类和它的3个子类。当我调用这3个子类中的一个时,我必须在线程类中创建一个线程并使用它们的main(因为线程main是纯虚拟抽象)但问题是在它调用它调用的Create Thread函数(c'tor of thread)之前那些分管。

thread.h

#ifndef _THREAD_H_
#define _THREAD_H_

#include <string>
#include <Windows.h>
#include <iosfwd>
#include "Mutex.h"
#include "SynchronizedArray.h"
#include "SynchronizedCounter.h"



std::string message = "";


class Thread{ 

private:
    HANDLE hThread;
    int idThread;

protected:

    SynchronizedArray *arr;
    int size;
    SynchronizedCounter *counter;

public:

    Thread(DWORD funct){   //creates a thread by calling subclasses main functions appropriately 
        hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) funct, NULL, 0, (LPDWORD)&idThread);
    }

    virtual DWORD WINAPI main(void*) = 0;  // pure virtual abstract class 

    void suspend(){  //suspent the thread 
        SuspendThread(hThread);
    }
    void resume(){// retume the thread 
        ResumeThread(hThread);
    }

    void terminate(){ // terminates the thread
    TerminateThread(hThread,0);
    }
    void join(){ // joins the thread 
        Mutex mut;
        mut.lock();
    }
    static void sleep(int sec){ //wrapper of sleep   by sec
        Sleep(sec*1000);
    }

};


#endif

作为示例的3个继承的Thread类中的1个(所有这些都是相同的)

PrintThread.h

#ifndef _PRINTINGTHREAD_H_
#define _PRINTINGTHREAD_H_
#include "SynchronizedArray.h"
#include "SynchronizedCounter.h"
#include "Thread.h"
#include <iostream>
#include "SortingThread.h"
#include "CountingThread.h"
#include <string>
#include <Windows.h>

extern CountingThread counterThread1;
extern CountingThread counterThread2;
extern SortingThread sortingThread1;
extern SortingThread sortingThread2;


class PrintingThread:public Thread{
private:
    char temp;
public:
    PrintingThread() :Thread(main(&temp)){
    }


    DWORD WINAPI main(void* param)
    {
        std::cout << "Please enter an operation ('showcounter1','showcounter2','showarray1','showarray2' or 'quit')" << std::endl;
        std::cin >> message;
        while (message != "quit")
        {
            if (message == "showcounter1")
            {
                std::cout << counterThread1<<std::endl;
            }
            else if (message == "showcounter2")
            {
                std::cout << counterThread2 << std::endl;
            }
            else if (message == "showarray1")
            {
                std::cout << sortingThread1 << std::endl;
            }
            else if (message == "showarray2")
            {
                std::cout << sortingThread2 << std::endl;
            }
            else {
                std::cout << "Invalid operation";
            }
            std::cout << "Please enter an operation ('show counter 1','show counter 2','show array 1','show array 2' or 'quit')" << std::endl;
            std::cin >> message;

        }
        return 0;

    }
};




#endif

为什么它在调用线程的c'tor之前调用主线。

2 个答案:

答案 0 :(得分:2)

您的PrintingThread构造函数初始值设定项列表实际上是调用 PrintingThread::main并将结果传递给Thread构造函数。这意味着CreateThread调用正在接收DWORD(在本例中为0)作为其函数参数。

您需要更改类设计以实际传递函数指针和上下文参数,例如:

Thread(DWORD funct){
    hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) funct, NULL, 0, (LPDWORD)&idThread);

应该是:

Thread(LPTHREAD_START_ROUTINE funct, LPVOID arg) {
    hThread = CreateThread(NULL, 0, funct, arg, 0, (LPDWORD)&idThread);

(你必须施放funct的事实应该是一个巨大的红旗。)

同样,子类构造函数将从:

更改
PrintingThread() :Thread(main(&temp)){

为:

PrintingThread(): Thread(main, &temp) {

请注意,您的代码仍会有其他问题,例如线程函数应该是静态的(因此您无法尝试访问成员函数)。

答案 1 :(得分:2)

你需要做更像这样的事情:

thread.h:

#ifndef _THREAD_H_
#define _THREAD_H_

#include <string>
#include <Windows.h>
#include <iosfwd>
#include "Mutex.h"
#include "SynchronizedArray.h"
#include "SynchronizedCounter.h"

class Thread
{ 
private:
    HANDLE hThread;
    DWORD idThread;
    void *pParam;    

    static DWORD WINAPI ThreadProc(LPVOID lpParameter)
    {
        Thread *pThis = (Thread*) lpParameter;
        return pThis->main(pThis->pParam);
    }

protected:
    SynchronizedArray *arr;
    int size;
    SynchronizedCounter *counter;

public:    
    Thread(void *aParam)
    {
        //creates a thread by calling subclasses main functions appropriately 
        pParam = aParam;
        hThread = CreateThread(NULL, 0, &ThreadProc, this, 0, &idThread);
    }

    virtual DWORD main(void*) = 0;  // pure virtual abstract class 

    void suspend()
    {
        //suspent the thread 
        SuspendThread(hThread);
    }

    void resume()
    {
        // resume the thread 
        ResumeThread(hThread);
    }

    void terminate()
    {
        // terminates the thread
        TerminateThread(hThread, 0);
    }

    void join()
    {
        // joins the thread 
        Mutex mut;
        mut.lock();
    }

    static void sleep(int sec)
    {
        //wrapper of sleep   by sec
        Sleep(sec*1000);
    }
};

#endif

PrintThread.h:

include <iostream>
#include "SortingThread.h"
#include "CountingThread.h"
#include <string>
#include <Windows.h>

extern CountingThread counterThread1;
extern CountingThread counterThread2;
extern SortingThread sortingThread1;
extern SortingThread sortingThread2;

class PrintingThread : public Thread
{
private:
    char temp;

public:
    PrintingThread() : Thread(&temp)
    {
    }

    virtual DWORD main(void* param)
    {
        std::string message;

        do
        {
            std::cout << "Please enter an operation ('showcounter1','showcounter2','showarray1','showarray2' or 'quit')" << std::endl;
            std::cin >> message;
            if (message == "quit")
            {
                break;
            }
            if (message == "showcounter1")
            {
                std::cout << counterThread1 << std::endl;
            }
            else if (message == "showcounter2")
            {
                std::cout << counterThread2 << std::endl;
            }
            else if (message == "showarray1")
            {
                std::cout << sortingThread1 << std::endl;
            }
            else if (message == "showarray2")
            {
                std::cout << sortingThread2 << std::endl;
            }
            else
            {
                std::cout << "Invalid operation";
            }
        }
        while (true);

        return 0;    
    }
};

#endif