[英]lvalue required as unary '&" operand
I have a problem with my project which is supposed to add up every line using a thread then sum it all, but I'm getting an error that says lvalue required as unary '&" operand 我的项目有一个问题,应该使用一个线程将每一行加起来然后求和,但是我遇到一个错误,说左值要求为一元'&“操作数
pthread_create(&tid, NULL, &sum_line(0), NULL); pthread_create(&tid,NULL,&sum_line(0),NULL);
I've tried some thing but couldn't solve it, any ideas? 我已经尝试过一些东西,但是无法解决,有什么想法吗? Thanks 谢谢
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
static void * sum_line(int nr_line);
int Sum;
int A[4][4];
int main() {
pthread_t tid;
Sum=0;
printf("\nOrig thread tid(%d) Sum=%d", pthread_self(), Sum);
pthread_create(&tid, NULL, &sum_line(0), NULL);
printf("\nChild thread was created tid(%d)", tid);
pthread_join(tid,NULL);
printf("\nOrig thread tid(%d)-->Child thread ended tid(%d) Sum=%d",pthread_self(), tid, Sum);
printf("\n");
}
static void * sum_line(int nr_line) {
int i;
for(i=0;i<4;i++) {
Sum=Sum+A[i];
printf("\nChild thread tid(%d), i=%d, Sum=%d",pthread_self(),i,Sum);
sleep(2);
}
printf("\nChild thread tid(%d)--> ending", pthread_self());
}
pthread_create()
将指针传递给函数pthread_create()
Write just sum_line
, not &sum_line(0)
. 只写sum_line
而不是&sum_line(0)
。
The pthread_create()
function expects a pointer to the thread function — ie the function name — and not the result of calling the function. pthread_create()
函数需要一个指向线程函数的指针(即函数名称),而不是调用该函数的结果。 The pthread_create()
function will arrange for the new thread to call the function, but it needs a function pointer. pthread_create()
函数将安排新线程调用该函数,但它需要一个函数指针。
Also, the signature for a thread function must be: 另外,线程函数的签名必须为:
void *function(void *arg);
The function should also return a value — add return 0;
该函数还应该返回一个值-加return 0;
before the close brace. 在大括号之前。
You pass a null pointer to the function; 您将空指针传递给该函数; you can't expect that to work as int nr_line
. 您不能指望它可以像int nr_line
工作。 You'll need to do some fancy footwork to get a number to the function. 您需要做一些花哨的步法才能为函数添加一个数字。 There are two main options: 有两个主要选项:
int nr_line = 247;
pthread_create(&tid, NULL, sum_line, &nr_line);
The function then looks like: 该函数如下所示:
void *sum_line(void *arg)
{
int nr_line = *(int *)arg;
…
return 0;
}
Just make sure each thread gets a pointer to a different object when you start multiple threads. 只要确保在启动多个线程时每个线程都有一个指向不同对象的指针即可。
uintptr_t nr_line = 247;
pthread_create(&tid, NULL, sum_line, (void *)nr_line);
Or: 要么:
int nr_line = 247;
pthread_create(&tid, NULL, sum_line, (void *)(uintptr_t)nr_line);
and the function then looks like: 然后该函数如下所示:
void *sum_line(void *arg)
{
int nr_line = (uintptr_t)arg;
…
return 0;
}
The double cast avoids compiler warnings about converting an integer of a different size to a pointer. double强制转换避免了编译器有关将不同大小的整数转换为指针的警告。
Note that pthread_create()
will call the function as if it is void *function(void *args)
, so passing it any other type of function pointer, even if cast with (void (*)(void *))
, is cheating and leads to undefined behaviour. 请注意pthread_create()
会像调用void *function(void *args)
一样调用函数,因此,即使使用(void (*)(void *))
强制转换,也会将其他任何类型的函数指针传递给该函数导致不确定的行为。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.