【问题标题】:C++ Worker threads with Builder (Borland Libraries)带有 Builder 的 C++ 工作线程(Borland 库)
【发布时间】:2012-07-18 07:38:51
【问题描述】:

我有一个应用程序,它基本上有一个 GUI 和一个负责运行我的主程序和创建对象的函数。

目前我的 GUI 会崩溃,因为后台工作完成后后台工作太多,它将再次恢复活力。

我需要创建一个工作线程(我们不需要接口线程,因为我的 GUI 只显示正在发生的事情的进度。),工作线程将运行我的函数,另一个工作线程将运行我的 GUI。

我正在努力寻找这方面的任何信息。谷歌似乎没有帮助,谁能给我一些有用的信息?目前我有这样的事情:

void __fastcall TfrmRunning::FormCreate(TObject *Sender)
{
   //Does most of my background stuff when this form is created
}

【问题讨论】:

  • 我相信在 C++ builder 中使用 FormCreate() 是不鼓励的,据我所知,它与 Delphi 兼容仍然存在一些问题。您应该在 Form 的构造函数或 FormShow 事件中创建线程。

标签: c++ multithreading c++builder


【解决方案1】:

Threadz 使用 C++ Builder 很容易,尤其是。如果您不需要任何 GUI 通讯。我不得不就 GUI 通信的问题向 SO 征求意见 - 需要一个宏来处理消息,并且我提供了错误的表单类 - 我得到了堆栈溢出:)

'classes'中有一个TThread类,类似于Delphi。覆盖“执行”以执行您的线程代码,例如:

class TpoolThread : public TThread{
    CBthreadPool *FmyPool;
protected:
     virtual void __fastcall Execute(void);
public:
    TpoolThread(CBthreadPool *myPool):TThread(true){
        FmyPool=myPool;
        Resume();
    };
};

如果您没有 TThread 类(我有 C++ Builder 2009 - 之前不确定),您可以按照@inkooboo 的建议使用 API 调用。 WINAPI CreateThread() 只会调用简单的 C 风格函数或静态方法,因此您通常需要显式传递一个实例(通常是“this”)作为 CreateThread 参数,以便从线程代码中调用实例方法。使用 CreateThread API 的 ThreadPool 示例,(尽管我确信如果你有 STL,你也会有 TThread):

#ifndef cthreadpoolH
#define cthreadpoolH

#include <Classes.hpp>
#include <deque.h>

class ThreadPool;

class PoolTask {
friend class ThreadPool;
    TNotifyEvent FonComplete;
protected:
    ThreadPool *myPool;
    int param;
public:
    PoolTask(int inParam, TNotifyEvent OnDone):param(inParam),FonComplete(OnDone){};
    virtual void run()=0;
};

template <typename T> class PCSqueue{
    CRITICAL_SECTION access;
    deque<T> *objectQueue;
    HANDLE queueSema;
public:
    PCSqueue(){
        objectQueue=new deque<T>;
        InitializeCriticalSection(&access);
        queueSema=CreateSemaphore(NULL,0,MAXINT,NULL);
    };
    void push(T ref){
        EnterCriticalSection(&access);
        objectQueue->push_front(ref);
        LeaveCriticalSection(&access);
        ReleaseSemaphore(queueSema,1,NULL);
    };
    bool pop(T *ref,DWORD timeout){
        if (WAIT_OBJECT_0==WaitForSingleObject(queueSema,timeout)) {
            EnterCriticalSection(&access);
            *ref=objectQueue->back();
            objectQueue->pop_back();
            LeaveCriticalSection(&access);
            return(true);
        }
        else
            return(false);
    };
};

class ThreadPool {
    int FthreadCount;
    PCSqueue<PoolTask*> queue;
public:
    ThreadPool(int initThreads){
        for(FthreadCount=0;FthreadCount!=initThreads;FthreadCount++){
            CreateThread(NULL,0,staticThreadRun,this,0,0);
        };
    }
    void setThreadCount(int newCount){
        while(FthreadCount<newCount){
            CreateThread(NULL,0,staticThreadRun,this,0,0);
            FthreadCount++;
        };
        while(FthreadCount>newCount){
            queue.push((PoolTask*)NULL);
            FthreadCount--;
        };
    }
    static DWORD _stdcall staticThreadRun(void *param){
        ThreadPool *myPool=(ThreadPool*)param;
        PoolTask *thisTask;
        SetThreadPriority(GetCurrentThread(),THREAD_PRIORITY_BELOW_NORMAL);
        while (myPool->queue.pop(&thisTask,INFINITE)){
            if(thisTask==NULL){return(0);};
            thisTask->run();
            if (thisTask->FonComplete!=NULL) {
                thisTask->FonComplete((TObject*)thisTask);
            }
        }
    }
    void submit(PoolTask *aTask){
        aTask->myPool=this;
        queue.push(aTask);
    };
};

#endif

【讨论】:

    【解决方案2】:

    您可以使用WinAPI 线程工具或任何线程库,例如:boost/C++11 threading librarypthread 等。

    欢迎使用 C++ 中的多线程!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-25
      • 2010-09-25
      • 2014-12-10
      • 2012-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多