ldcsaa / HP-Socket

High Performance TCP/UDP/HTTP Communication Component
https://www.oschina.net/p/hp-socket
Other
5.73k stars 1.76k forks source link

向线程池提交任务失败,提示错误码1,Operation not permitted,是什么原因? #296

Closed XTU-Liu-LAB closed 1 day ago

XTU-Liu-LAB commented 2 days ago
VOID TaskProc(PVOID pvArg)
{
    printf("Task is processing with argument: %p\n", pvArg);
}

int main()
{
    // 创建一个监听对象
    HP_TcpPullClientListener listener = Create_HP_TcpPullClientListener();
    // 创建客户端
    HP_PullClient tcpclient = Create_HP_TcpPullClient(listener);
    // 创建一个线程池对象,监听器设置为 NULL
    thread_pool = Create_HP_ThreadPool(NULL);

    if (!HP_ThreadPool_HasStarted(thread_pool))
    {
        printf("Starting thread pool.\n");

        /* 启动线程池 */
        if (HP_ThreadPool_Start(thread_pool, 2, 2, TRP_CALL_FAIL, 1024)) // 线程堆栈空间大小(dwStackSize)不能设置为默认?
        {
            /* 线程池启动失败 */
            DWORD err_code = SYS_GetLastError();
            LPCSTR err_str = SYS_GetLastErrorStr();
            printf("Failed to start thread pool: (%d)%s.\n", err_code, err_str);
            return err_code;
        }
    }

    /* 为监听器添加回调函数 */
    HP_Set_FN_Client_OnConnect(listener, onConnect);
    HP_Set_FN_Client_OnHandShake(listener, onHandShake);
    // HP_Set_FN_Client_OnPullReceive(listener, onPullReceive);
    HP_Set_FN_Client_OnClose(listener, onClose);

    /* 连接到服务器 */
    if (!HP_Client_Start(tcpclient, "192.168.17.129", 65535, FALSE))
    {
        /* 连接失败,输出失败原因并退出 */
        printf("connect to server failed: %s\n", HP_Client_GetLastErrorDesc(tcpclient));
        return -1;
    }

    // 定义任务函数指针并指向 TaskProc
    HP_Fn_TaskProc fnTask = TaskProc;

    // 模拟提交任务到线程池
    void *taskArg = (void *)1234; // 示例参数

    if (!HP_ThreadPool_Submit(thread_pool, TaskProc, taskArg, INFINITE))
    {
        DWORD error = SYS_GetLastError();
        printf("Failed to submit task. Error code: %u,%s\n", error, SYS_GetLastErrorStr());
    }
    else
    {
        printf("Task submitted successfully.\n");
    }

    getchar(); // 不让程序退出

    HP_Client_Stop(tcpclient);                 // 停止客户端通信组件
    HP_ThreadPool_Stop(thread_pool, INFINITE); // 停止线程池组件
    /* 销毁相关组件 */
    Destroy_HP_TcpPullClientListener(listener);
    Destroy_HP_TcpPullClient(tcpclient);
    Destroy_HP_ThreadPool(thread_pool);
    return 0;
}
FunlyDay commented 2 days ago

这是来自樊  邮箱的自动回复邮件。 您好,您发的邮件我已收到,我会及时查看并予以回复。

ldcsaa commented 2 days ago

image

栈内存分配1K(1024),太小了。导致创建线程池分配失败。线程池分配失败当然无法提交任务了。

你关键的错误是,这个if判断取反了: if (HP_ThreadPool_Start(thread_pool, 2, 2, TRP_CALL_FAIL, 1024))

XTU-Liu-LAB commented 1 day ago

谢谢,确实是我代码的逻辑错误,修改后没有问题了。