【问题标题】:P thread Create Pointer issueP线程创建指针问题
【发布时间】:2021-07-23 18:11:51
【问题描述】:
   #include <stdio.h>
   #include <stdlib.h>
   // Thread Declarations
  #include <pthread.h>
 pthread_t monitor_thread;
 void *Get_Monitor_Data(void *BM_params);


// this is my struct ///

typedef struct
{
HCORE BM_cores[0];
int total_cores;
} BM_PARAMS;

BM_PARAMS BM_Dat;

int Start_monitor(void)
{
int RETVAL=0;
 RETVAL = pthread_create(&monitor_thread, NULL, Get_Monitor_Data,(void*)BM_Dat);
 if(RETVAL !=0)
{
 printf("Error Starting Thread \n");
}

return 0;
}
void *Get_Monitor_Data (void *BM_Dat) // Bus Monitor Thread
{
BM_PARAMS*monitor_params;
int no_of_cores=0;
monitor_params = (BM_PARAMS *) BM_Dat;

BTICard_CardReset(*monitor_params->BM_cores);// reset card if required

return 0;
}

pthread 创建部分给出“无法转换为指针类型”的错误 我做错了什么吗? 我需要将一个结构作为参数传递给 P 线程函数,我该怎么做?

【问题讨论】:

  • 什么是monitor_thread?您还声称您已经“在我的主要内容之前声明了 Get_Monitor_Data”,这在帖子中没有显示。请提供完整准确的代码minimal reproducible example 以及准确完整的错误消息。
  • 它必须是&amp;BM_Dat 作为pthread_create 的最后一个参数
  • 当时我尝试更改为 &BM_Dat Eclipse 无法识别包含文件中的函数
  • G:\Projects\Eclipse Resources\BM Ballard\BM_BALLARD\Debug/..\BallardBM/BM_BALLARD.c:46:未定义对BTICard_CardOpen' G:\Projects\Eclipse Resources\BM Ballard\BM_BALLARD\Debug/..\BallardBM/BM_BALLARD.c:60: undefined reference to BTICard_CoreOpen' G:\Projects\Eclipse Resources\ 的引用BM Ballard\BM_BALLARD\Debug/..\BallardBM/BM_BALLARD.c:64:未定义对“BTI1553_ChIs1553”的引用
  • 这是一个完全不同的问题。发布一个新问题。

标签: c multithreading pointers struct pthreads


【解决方案1】:

您正在尝试将具有结构类型的BM_Dat 转换为void *struct 不能转换为指针。但是你可以传递BM_Dat的地址:

RETVAL = pthread_create(&monitor_thread, NULL, Get_Monitor_Data, &BM_Dat);

BM_PARAMS * 可以转换为void *(实际上是隐含的),实际上你的线程函数会将参数转换为这种类型。

【讨论】:

  • 我已经在我的电源之前声明了 Get_Monitor_Data void *Get_Bus_Monitor_Data(void *BM_Dat);
  • 我在 Pthread 创建“(void*)BM_Dat”时出错,它说无法转换为指针类型
  • 给出一个完整的最小且可编译的例子来显示你的错误。
  • 代码未编译我得到错误无法在创建 P 线程时转换为指针类型
  • 这是控制台报告 ---
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-17
  • 1970-01-01
相关资源
最近更新 更多