thread

#include <afxwin.h>
#include <vector>
#include <string>
#include <iostream>
#include <mutex>

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

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

// 存放匹配结果的 …


This content originally appeared on DEV Community and was authored by 海前 王

#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;
}


This content originally appeared on DEV Community and was authored by 海前 王


Print Share Comment Cite Upload Translate Updates
APA

海前 王 | Sciencx (2024-08-27T04:48:29+00:00) thread. Retrieved from https://www.scien.cx/2024/08/27/thread-2/

MLA
" » thread." 海前 王 | Sciencx - Tuesday August 27, 2024, https://www.scien.cx/2024/08/27/thread-2/
HARVARD
海前 王 | Sciencx Tuesday August 27, 2024 » thread., viewed ,<https://www.scien.cx/2024/08/27/thread-2/>
VANCOUVER
海前 王 | Sciencx - » thread. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/08/27/thread-2/
CHICAGO
" » thread." 海前 王 | Sciencx - Accessed . https://www.scien.cx/2024/08/27/thread-2/
IEEE
" » thread." 海前 王 | Sciencx [Online]. Available: https://www.scien.cx/2024/08/27/thread-2/. [Accessed: ]
rf:citation
» thread | 海前 王 | Sciencx | https://www.scien.cx/2024/08/27/thread-2/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.