Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Memory management 用pthreaad代码替换汇编程序内存屏障_Memory Management_Assembly_Pthreads - Fatal编程技术网

Memory management 用pthreaad代码替换汇编程序内存屏障

Memory management 用pthreaad代码替换汇编程序内存屏障,memory-management,assembly,pthreads,Memory Management,Assembly,Pthreads,我有以下代码使用内联汇编程序实现内存屏障: #define barrier() __asm__ __volatile__("":::"memory") #define SET_PTR_TO_GLOBALS(x) do { \ (*(struct globals**)&ptr_to_globals) = (void*)(x); \ barrier(); \ } while (0) 我想用PTHREADS代码替换它,如下所示: #include <pthread

我有以下代码使用内联汇编程序实现内存屏障:

 #define barrier() __asm__ __volatile__("":::"memory")
#define SET_PTR_TO_GLOBALS(x) do { \
    (*(struct globals**)&ptr_to_globals) = (void*)(x); \
    barrier(); \
} while (0)
我想用PTHREADS代码替换它,如下所示:

  #include <pthread.h>
#include <stdio.h>

void * entry_point(void *arg)
{
#define SET_PTR_TO_GLOBALS(x) do { \
    (*(struct globals**)&ptr_to_globals) = (void*)(x); \
    } while (0)

    return NULL;
}

int main(int argc, char **argv)
{
    pthread_t thr;
    if(pthread_create(&thr, NULL, &entry_point, NULL))
    {
        printf("Could not create thread\n");
        return -1;
    }

    if(pthread_join(thr, NULL))
    {
        printf("Could not join thread\n");
        return -1;
    }
    return 0;
}
#包括
#包括
void*入口点(void*arg)
{
#定义SET_PTR_TO_GLOBALS(x)do{\
(*(struct globals**)和ptr_to_globals)=(void*)(x)\
}而(0)
返回NULL;
}
int main(int argc,字符**argv)
{
pthread_t thr;
if(pthread_create(&thr,NULL,&entry_point,NULL))
{
printf(“无法创建线程\n”);
返回-1;
}
if(pthread_join(thr,NULL))
{
printf(“无法加入线程\n”);
返回-1;
}
返回0;
}

这会取代并保留第一个代码块提供的内存屏障功能吗???

这不是“内存屏障”。这是一个“编译器障碍”。这是否意味着pthread代码将无法编译?它可以编译,但可能是错误的,并且有微妙的、难以复制的错误。您可以尝试通过种族检测器(如tsan或helgrind)运行它,但这不是一种有效的编写代码的方式……您的代码对我来说没有多大意义:您创建并加入一个立即退出的线程(从不使用宏)。我们是不是错过了一些重要的东西?您的两个代码片段之间没有明显的联系。我只是将其作为示例代码发布,显然使用适当的参数调用main()会将宏包含在其中。