C++ 错误:从“int*”到“int”[-fppermissive]的转换无效

C++ 错误:从“int*”到“int”[-fppermissive]的转换无效,c++,c,C++,C,获取错误: 错误:从“int*”到“int”[-fppermissive]的转换无效 论g++ 关于以下代码: void* func(void *s) { int i = 0; int self = (int *)s; printf("Thread Entered: %d\n", self); sm.lock(self); // Critical section (Only one thread // can enter here at a t

获取错误:

错误:从“int*”到“int”[-fppermissive]的转换无效 论g++

关于以下代码:

void* func(void *s)
{
    int i = 0;
    int self = (int *)s;
    printf("Thread Entered: %d\n", self);

    sm.lock(self);

    // Critical section (Only one thread
    // can enter here at a time)
    for (i=0; i<MAX; i++)
        ans++;

    sm.unlock(self);
}

您需要更改int self=int*s;到int self=*int*s;或指向int*self=int*s

你需要把它们看作两件不同的事情。一个是指向存储int*值的内存的指针,另一个是实际值int

查看函数声明void*funcvoid*s,s参数的类型为void,如果要转换它,则必须为int

您的数据类型似乎有点混合,这在C/C++中不太好。你能打扫一下吗?如果要将此函数与pthread_create一起使用,请按照此函数的示例文档,尝试

// your pthread_create call..
int SOME_INT = 123;
s = pthread_create(&thread_id, &attr, &thread_start, &SOME_INT);

//...and your function
void* func(void *s)
{
    int self = (int*) s;
指针可能会令人困惑。查看上面的代码是否类似,特别是将pthread_create的最后一个参数作为指针引用传递。然后尝试您的原始代码。可能只是因为它没有作为引用传递

看看会产生什么结果,否则尝试将其存储为指针,然后在使用时进行转换

void* func(void *s)
{
    int *self = s;

    sm.lock(*self);  // but can give a potential race condition.

在函数体的第二行中,我怀疑您试图获取s处的值作为int指针,但实际上您直接将其转换为int。这将产生s中的地址,而不是存储在那里的值。

int self=int*s;做你确实在做错误所说的。你需要显示启动这个线程的代码,倾斜任何相关变量。现在回答这个问题还为时过早。在显示更多信息之前,您无法确定您的任何建议是否正确。我个人怀疑结果会是int self=int;,但是没有很好的理由这样做。签名void*funcvoid*s是必需的,因此您对此无能为力。您可能需要重新考虑如何传递数据。空指针可能意味着任何东西,如果访问不正确,则可能会开始读取其他程序的内存空间,这将导致您的程序在“常规保护故障”Windows或“段故障”Linux下随机崩溃。我猜pthread_create使用了一种不寻常的方法来传递非类型数据,我对此并不熟悉。根据您链接的文档,它创建的线程为pthread_create&tinfo[tnum]。thread_id,&attr,&thread\u start,&tinfo[tnum],其中&tinfo[tnum]您的void*s组件传递一个thread\u info结构,然后使用struct thread\u info*tinfo=arg;通过thread函数访问该结构;。如果要调用pthread_create,请检查它们是否同样匹配。将更新答案以包含示例。