类型的实参与“LPTHREAD_START_ROUTINE”类型的形参不兼容

在使用利用CreateThread创建线程时

struct A
{
    DWORD  WINAPI MyThreadFunction(LPVOID) {}
    void Run()
    {
        HANDLE hThread = CreateThread(
            NULL,                   // default security attributes
            0,                      // use default stack size  
            MyThreadFunction,       // thread function name
            0,                      // argument to thread function 
            0,                      // use default creation flags 
            NULL);   // returns the thread identifier 
    }
};

visual studio 报了如下错误:
英文环境
E0167 argument of type "DWORD (__stdcall A::*)()" is incompatible with parameter of type "LPTHREAD_START_ROUTINE"

中文环境
E0167 DWORD (__stdcall A::*)类型的实参与“LPTHREAD_START_ROUTINE”类型的形参不兼容

问题解决办法参考文档,将MyThreadFunction函数订单改为DWORD static WINAPI MyThreadFunction()

原因在ThreadProc callback function有描述

Each thread receives a unique copy of the local variables of this function. Any static or global variables are shared by all threads in the process.

参考资料:
  1. ThreadProc callback function
  2. CreateThread function

转载于:https://www.cnblogs.com/lkpp/p/incompatible_parameter_LPTHREAD_START_ROUTINE.html

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果出现了“类型实参与 `lpthread_start_routine` 类型形参不兼容”的错误,通常是因为你在创建线程时传递给 `pthread_create` 函数的第三个参数,也就是线程入口函数的参数类型与 `lpthread_start_routine` 类型的参数类型不匹配。 `lpthread_start_routine` 是一个函数指针类型,它的参数类型是 `void*`,也就是一个指向任意类型数据的指针。因此,当你定义线程入口函数时,它的参数类型也必须是 `void*`。如果你需要在线程中使用其他类型的数据,可以通过类型转换来实现。 下面是一个例子,演示了如何正确使用 `lpthread_start_routine`: ```c #include <stdio.h> #include <stdlib.h> #include <pthread.h> void* thread_func(void* arg) { int* num = (int*)arg; printf("Thread received value: %d\n", *num); *num = *num * 2; return NULL; } int main() { int num = 10; pthread_t thread; int rc = pthread_create(&thread, NULL, thread_func, (void*)&num); if (rc) { printf("ERROR; return code from pthread_create() is %d\n", rc); exit(-1); } rc = pthread_join(thread, NULL); if (rc) { printf("ERROR; return code from pthread_join() is %d\n", rc); exit(-1); } printf("Thread return value: %d\n", num); return 0; } ``` 上面的例子中,我们创建了一个整型变量 `num`,并将其初始化为 10。然后,我们创建了一个线程,将 `num` 作为参数传递给线程入口函数 `thread_func`。在 `thread_func` 函数中,我们将 `arg` 强制转换为整型指针,并获取 `num` 的值。然后,我们将 `num` 的值乘以 2,并将结果存储在 `num` 中。最后,在主线程中,我们输出了 `num` 的值,以验证线程是否正确地修改了 `num` 的值。 总之,当你在使用 `lpthread_start_routine` 时,一定要确保线程入口函数的参数类型与 `void*` 匹配,并通过类型转换来获取真实的参数类型
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值