带参数的 C++ 简单线程(无 .net)

标签 c++ windows linux multithreading winapi

我已经在互联网上搜索了一段时间并找到了不同的解决方案,但所有解决方案都不起作用或者对我的使用来说太复杂了。 我直到两年前才使用 C++,所以它可能有点生疏:D

我目前正在编写一个将数据发布到 URL 的程序。它只发布数据,没有其他内容。 为了发布数据,我使用curl,但它会阻塞主线程,并且当第一篇文章仍在运行时,将会开始第二篇文章。 最终大约有 5-6 个后期操作同时运行。

现在我想将带有curl 的帖子推送到另一个线程中。每个帖子一个线程。 线程应该获取一个字符串参数,其中包含要推送的内容。

我目前被困在这个问题上。尝试了 Windows 的 WINAPI,但在读取参数时崩溃了。 (在我的示例中,当主线程结束时,第二个线程仍在运行(等待系统(“暂停”))。

如果有一个多平台解决方案就好了,因为它可以在 Windows 和 Linux 下运行!

这是我当前的代码:

#define CURL_STATICLIB
#include <curl/curl.h>
#include <curl/easy.h>
#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#if defined(WIN32)
    #include <windows.h>
#else
    //#include <pthread.h>
#endif

using namespace std;

void post(string post) { // Function to post it to url
    CURL *curl; // curl object
    CURLcode res; // CURLcode object
    curl = curl_easy_init(); // init curl
    if(curl) { // is curl init
        curl_easy_setopt(curl, CURLOPT_URL, "http://10.8.27.101/api.aspx"); // set url
        string data = "api=" + post; // concat post data strings
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str()); // post data
        res = curl_easy_perform(curl); // execute
        curl_easy_cleanup(curl); // cleanup
    } else {
        cerr << "Failed to create curl handle!\n";
    }
}

#if defined(WIN32)
    DWORD WINAPI thread(LPVOID data) { // WINAPI Thread
        string pData = *((string*)data); // convert LPVOID to string [THIS FAILES]
        post(pData); // post it with curl
    }
#else
    // Linux version
#endif

void startThread(string data) { // FUnction to start the thread
    string pData = data; // some Test
    #if defined(WIN32)
        CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)thread, &pData, 0, NULL); // Start a Windows thread with winapi
    #else
        // Linux version
    #endif
}

int main(int argc, char *argv[]) {
    // The post data to send
    string postData = "test1234567890";
    startThread(postData); // Start the thread
    system("PAUSE"); // Dont close the console window
    return EXIT_SUCCESS;
}

有人有建议吗?

感谢您的帮助!

最佳答案

考虑使用Boost.Thread或新的C++11 threading facilities (如 std::thread 等)。

对最初问题代码的一些评论:

  • 如果远离 std::thread 或 boost::thread,请使用 _beginthreadex(..)而不是CreateThread(..)因为后一种如果与 C 运行时的某些函数一起使用可能会导致资源泄漏。

  • 使用 CreateThread(..) 时,如果传递函数的签名正确,则不需要强制转换为 LPTHREAD_START_ROUTINE。所以转换它是完全错误的。

  • 已经有一些关于堆栈分配变量的生命周期的评论,以及如果将这些变量的地址传递给线程函数会发生什么。

  • 不要使用system("PAUSE")以保持代码的可移植性。请改用以下代码片段:

    void wait_for_key_press()
    {
        std::cin.clear();
        std::cin.ignore(std::cin.rdbuf()->in_avail());
        std::cin.get();
    }

关于带参数的 C++ 简单线程(无 .net),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12231595/

相关文章:

c - 如何拦截 SSH stdin 和 stdout? (不是密码)

linux - 在 Linux 的文本文件中用逗号替换空格

C++ 代码风格 : SHA2 algorithm

c++ - 按整数对字符串 vector 进行排序

c++ - 快速生成随机集,蒙特卡洛模拟

加载 dll : OSError 0x7e 时 Python CFFI 模块失败

c++ - 我如何解决不允许模板化虚拟功能的问题

windows - 在 Windows 上读取原始 USB 输入

windows - 是否可以使用批处理文件将文件发送到打印机?

linux - 从 Windows 到 Linux 的 SVN 预提交 Hook