thread

海前 王 - Aug 27 - - Dev Community
#include <afxwin.h>
#include <vector>
#include <string>
#include <iostream>
#include <mutex>

// 互斥锁,用于保护共享数据访问
std::mutex mtx;

// 存放匹配结果和序号的结构体
struct MatchResult {
    int index;
    std::string result;
};

// 存放匹配结果的 vector
std::vector<MatchResult> results;

// 字符匹配函数
std::string YourMatchingFunction(const char* str) {
    std::string input(str);  // 将字符指针转换为字符串

    // 执行字符匹配逻辑,这里使用一个简单的示例逻辑
    std::string matchResult;
    if (input.find("match") != std::string::npos) {
        matchResult = "Found";
    }
    else {
        matchResult = "Not Found";
    }

    return matchResult;
}
int numThreads = 4;
// MFC线程函数
UINT MatchThread(LPVOID pParam) {
    // 传入的参数包含了需要的数据
    std::pair<std::vector<char*>, int>* threadData = reinterpret_cast<std::pair<std::vector<char*>, int>*>(pParam);
    std::vector<char*>& data = threadData->first;
    int threadIndex = threadData->second;

    // 计算每个线程需要处理的数据范围
    int dataSize = data.size();
    int chunkSize = (dataSize + numThreads - 1) / numThreads;  // 向上取整
    int startIndex = threadIndex * chunkSize;
    int endIndex = min(startIndex + chunkSize, dataSize);

    // 执行字符匹配
    for (int i = startIndex; i < endIndex; ++i) {
        // 执行字符匹配操作,并得到匹配结果
        std::string result = YourMatchingFunction(data[i]);

        // 创建匹配结果结构体
        MatchResult matchResult;
        matchResult.index = i;  // 序号
        matchResult.result = result;

        // 使用互斥锁保护结果 vector 的访问
        std::lock_guard<std::mutex> lock(mtx);
        results.push_back(matchResult);
    }

    return 0;
}

// 创建并启动 MFC 线程进行字符匹配
void RunMatchingThreads(const std::vector<char*>& data, int numThreads) {
    std::vector<std::pair<std::vector<char*>, int>> threadData;
    threadData.reserve(numThreads);

    for (int i = 0; i < numThreads; ++i) {
        threadData.push_back(std::make_pair(data, i));

        // 创建线程并启动
        CWinThread* pThread = AfxBeginThread(MatchThread, &threadData[i]);
        // 或者使用下面的方式创建线程
        // CMyThread* pThread = (CMyThread*)AfxBeginThread(RUNTIME_CLASS(CMyThread), THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED);
        // pThread->m_pData = &threadData[i];
        // pThread->ResumeThread();
    }

    // 等待所有线程完成
    // 这里使用 WaitForSingleObject() 等待线程完成,也可以使用其他方式等待线程完成,
    // 例如通过线程间的信号量、事件等进行同步
    for (const auto& thread : threadData) {
        WaitForSingleObject(thread.first[0], INFINITE);
    }
}

int main() {
    // 假设你已经有一个 vector<char*> 存放了数据
    std::vector<char*> data;
    data.push_back(const_cast<char*>("This is a test"));
    data.push_back(const_cast<char*>("Another test"));
    data.push_back(const_cast<char*>("Testing matches"));
    // 创建并启动两个 MFC 线程进行字符匹配
    int numThreads = 2;
    RunMatchingThreads(data, numThreads);

    // 打印匹配结果
    for (const auto& result : results) {
        std::cout << "Index: " << result.index << ", Result: " << result.result << std::endl;
    }

    return 0;
}
Enter fullscreen mode Exit fullscreen mode
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player