将结构数组作为参数输入 pthread_create 的问题
Issue with feeding arrays of structs as arguments into pthread_create
我正在编写一个小型步进电机控制程序,我需要一个单独的线程来检查是否有任何电机需要更新。
我一直卡在将我的数据结构传递给 pthread_create() 并修改 test_motor2 的状态值。以下代码应该可以让您了解我要完成的工作:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
typedef struct motor{
int motor_status;
} motor;
typedef struct argstruct{
int *vect;
struct motor *allmotors;
} args;
void *test(void *arg){
struct argstruct *arguments = arg;
int *a_array = arguments->vect;
a_array[0] = 80;
// HERE I GET STUCK
struct motor *motors = arguments->allmotors;
// set test_motor2 status to 1
}
int main(){
pthread_t sidethread;
struct motor test_motor;
struct motor test_motor2;
test_motor.motor_status = 0;
test_motor2.motor_status = 0;
int a[3];
a[0] = 8; a[1] = 3; a[2] = 2;
struct motor *all_motors[2];
all_motors[0] = &test_motor;
all_motors[1] = &test_motor2;
struct argstruct motors_and_a;
motors_and_a.allmotors = all_motors;
motors_and_a.vect = a;
if (pthread_create(&sidethread, NULL, test, (void *)&motors_and_a)){
printf("Thread could not be started\n");
}
pthread_join(sidethread, NULL);
// Check that a[0] has been set to 80
printf("a[0]: %d\n", a[0]);
// Check that test_motor2 status is now 1
printf("Status of test_motor2: %d\n", test_motor2.motor_status);
}
该示例适用于阵列 a,但我无法使其适用于电机。
你能帮我找到解决办法吗?
谢谢!
最大
警告作业
motors_and_a.allmotors = all_motors;
无效,因为 all_motors
是 motor* [2]
而不是 motors_and_a.allmotors
预期的 motor*
,因此之后的使用具有未定义的行为。
你只需要给出一个motor*而不是一个motor*数组,或者改变[=40]的定义=]argstruct 有 struct motor **allmotors;
当然还有它的用途
因为
// HERE I GET STUCK
struct motor *motors = arguments->allmotors;
// set test_motor2 status to 1
我想你想要:
typedef struct argstruct{
int *vect;
struct motor ** allmotors; /* MODIFIED */
} args;
void *test(void *arg){
struct argstruct *arguments = arg;
int *a_array = arguments->vect;
a_array[0] = 80;
struct motor ** motors = arguments->allmotors; /* MODIFIED */
motors[1]->motor_status = 1; /* MODIFIED */
return 0;
}
其余不变
编译与执行:
pi@raspberrypi:~ $ gcc -g -pedantic -Wextra -Wall m.c -lpthread
pi@raspberrypi:~ $ ./a.out
a[0]: 80
Status of test_motor2: 1
在valgrind下执行:
pi@raspberrypi:~ $ valgrind ./a.out
==4083== Memcheck, a memory error detector
==4083== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==4083== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==4083== Command: ./a.out
==4083==
a[0]: 80
Status of test_motor2: 1
==4083==
==4083== HEAP SUMMARY:
==4083== in use at exit: 0 bytes in 0 blocks
==4083== total heap usage: 2 allocs, 2 frees, 1,160 bytes allocated
==4083==
==4083== All heap blocks were freed -- no leaks are possible
==4083==
==4083== For counts of detected and suppressed errors, rerun with: -v
==4083== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 3)
test
中也缺少 return
我正在编写一个小型步进电机控制程序,我需要一个单独的线程来检查是否有任何电机需要更新。
我一直卡在将我的数据结构传递给 pthread_create() 并修改 test_motor2 的状态值。以下代码应该可以让您了解我要完成的工作:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
typedef struct motor{
int motor_status;
} motor;
typedef struct argstruct{
int *vect;
struct motor *allmotors;
} args;
void *test(void *arg){
struct argstruct *arguments = arg;
int *a_array = arguments->vect;
a_array[0] = 80;
// HERE I GET STUCK
struct motor *motors = arguments->allmotors;
// set test_motor2 status to 1
}
int main(){
pthread_t sidethread;
struct motor test_motor;
struct motor test_motor2;
test_motor.motor_status = 0;
test_motor2.motor_status = 0;
int a[3];
a[0] = 8; a[1] = 3; a[2] = 2;
struct motor *all_motors[2];
all_motors[0] = &test_motor;
all_motors[1] = &test_motor2;
struct argstruct motors_and_a;
motors_and_a.allmotors = all_motors;
motors_and_a.vect = a;
if (pthread_create(&sidethread, NULL, test, (void *)&motors_and_a)){
printf("Thread could not be started\n");
}
pthread_join(sidethread, NULL);
// Check that a[0] has been set to 80
printf("a[0]: %d\n", a[0]);
// Check that test_motor2 status is now 1
printf("Status of test_motor2: %d\n", test_motor2.motor_status);
}
该示例适用于阵列 a,但我无法使其适用于电机。
你能帮我找到解决办法吗?
谢谢!
最大
警告作业
motors_and_a.allmotors = all_motors;
无效,因为 all_motors
是 motor* [2]
而不是 motors_and_a.allmotors
预期的 motor*
,因此之后的使用具有未定义的行为。
你只需要给出一个motor*而不是一个motor*数组,或者改变[=40]的定义=]argstruct 有 struct motor **allmotors;
当然还有它的用途
因为
// HERE I GET STUCK struct motor *motors = arguments->allmotors; // set test_motor2 status to 1
我想你想要:
typedef struct argstruct{
int *vect;
struct motor ** allmotors; /* MODIFIED */
} args;
void *test(void *arg){
struct argstruct *arguments = arg;
int *a_array = arguments->vect;
a_array[0] = 80;
struct motor ** motors = arguments->allmotors; /* MODIFIED */
motors[1]->motor_status = 1; /* MODIFIED */
return 0;
}
其余不变
编译与执行:
pi@raspberrypi:~ $ gcc -g -pedantic -Wextra -Wall m.c -lpthread
pi@raspberrypi:~ $ ./a.out
a[0]: 80
Status of test_motor2: 1
在valgrind下执行:
pi@raspberrypi:~ $ valgrind ./a.out
==4083== Memcheck, a memory error detector
==4083== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==4083== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==4083== Command: ./a.out
==4083==
a[0]: 80
Status of test_motor2: 1
==4083==
==4083== HEAP SUMMARY:
==4083== in use at exit: 0 bytes in 0 blocks
==4083== total heap usage: 2 allocs, 2 frees, 1,160 bytes allocated
==4083==
==4083== All heap blocks were freed -- no leaks are possible
==4083==
==4083== For counts of detected and suppressed errors, rerun with: -v
==4083== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 3)
test
中也缺少 return