1

复制一些用于工作增强线程的数据,我在下面实现了以下代码。我收到错误 C:\dev\default threads_threads.cpp|18|error: invalid conversion from 'void ( )(void )' to 'unsigned int ( attribute (( stdcall )) )(void )' [-fpermissive]|

但是...评论的行是推荐的,评论解释了我得到的错误。

事实证明,强烈推荐 _beginThreadEx,但在网络上的文档记录很差(如教程中)

#include <iostream>
#include <process.h>

void myThread(void *data)
{
    //C:\dev\default threads\_threads.cpp|6|error: invalid conversion from 'int*' to 'int' [-fpermissive]|
    //int x = static_cast<int*>(data);

    int *x = (int*)data;

    std::cout << "Hellow World! " << x;
}

int main()
{

    int x = 10;
    _beginthreadex(NULL, 0, myThread, &x, 0, NULL);
    while(true);
}
4

1 回答 1

1

无论如何,您必须将 x 声明为指针:

int *x = static_cast<int*>(data);

两者_beginthread_beginthreadex记录在这里:http: //msdn.microsoft.com/en-us/library/kdzttdcb (v=vs.80).aspx

根据 _beginthreadex() 的声明,您的 myThread() 函数应声明如下:

unsigned __stdcall myThread(void *data);
于 2012-10-17T00:45:23.943 回答