C 在计算要通过的正确参数时遇到困难
我正在尝试调用函数start\u thread()。我要传递的两个参数是void函数send和端口0的地址。但我一直在犯错误 应为“struct port\u t*”,但参数的类型为“struct port\u t*”threads.h。为什么我会犯这个错误C 在计算要通过的正确参数时遇到困难,c,struct,C,Struct,我正在尝试调用函数start\u thread()。我要传递的两个参数是void函数send和端口0的地址。但我一直在犯错误 应为“struct port\u t*”,但参数的类型为“struct port\u t*”threads.h。为什么我会犯这个错误 typedef struct port_t { int port[N]; semaphore_t producer, consumer, mutex; int id; } port_t; int main() {
typedef struct port_t {
int port[N];
semaphore_t producer, consumer, mutex;
int id;
} port_t;
int main()
{
port_t id[100];
int x;
for(x =0; x < 100; x++)
{
createPort(id[x], x);
}
start_thread(send, &id[0]);
}
int send(port_t temp)
{
while (1){
printf("Port %d: trying to send\n", temp.id);
P(&temp.consumer);
P(&temp.mutex);
printf("Port %d: sending message\n", temp.id);
V(&temp.mutex);
V(&temp.producer);
return 1;
}
}
void start_thread(void *function, struct port_t *port) //getting the message for this line
{
printf("In main: creating thread\n");
struct stack * stackP = (struct stack*)malloc(8192);
tcb = (struct TCB_t *)malloc(sizeof(struct TCB_t));
init_TCB (tcb, function, stackP, 8192, port);
tcb->val = port->id;
add_to_list( &ptr, &tcb);
}
typedef结构端口{
int端口[N];
信号量的生产者、消费者、互斥体;
int-id;
}港口;;
int main()
{
端口id[100];
int x;
对于(x=0;x<100;x++)
{
createPort(id[x],x);
}
启动线程(发送,&id[0]);
}
int发送(端口温度)
{
而(1){
printf(“端口%d:正在尝试发送”,临时id);
P(临时消费者);
P(临时互斥);
printf(“端口%d:正在发送消息,\n”,临时id);
V(临时互斥);
V(临时生产商);
返回1;
}
}
void start\u thread(void*函数,struct port\u t*port)//获取此行的消息
{
printf(“主:创建线程\n”);
结构堆栈*stackP=(结构堆栈*)malloc(8192);
tcb=(struct tcb_t*)malloc(sizeof(struct tcb_t));
初始化TCB(TCB,函数,堆栈,8192,端口);
tcb->val=端口->id;
将_添加到_列表(&ptr,&tcb);
}
此
void *function
。。。是对象指针,而不是函数指针。若要声明一个函数,该函数接受与send()
函数兼容的函数指针作为参数,请执行以下操作
void start_thread(int (*function)(port_t), struct port_t *port) { // ...
。。。或者这个
void start_thread(int (*function)(struct port_t), struct port_t *port) { // ...
。但是,我确实觉得您的
send()
函数的签名有点可疑。更常见的做法是将指针传递给结构,而不是传递结构本身(按值)。请按如下方式定义结构。别忘了提前宣布功能。只要在使用过结构端口的地方使用端口即可
typedef struct {
int port[N];
semaphore_t producer, consumer, mutex;
int id;
} port_t;
当询问编译器错误时,发布完整的代码集会有所帮助。照目前的情况,代码会生成大量警告和错误。有关更多信息,请参阅。