将 int 传递给 pthread
Passing an int to a pthread
无法将 pthread 的最后一个参数作为实际 int 传递。我希望能够访问 pthread_create 的最后一个参数中的 1。任何帮助将不胜感激!
#include <pthread.h>
#include <unistd.h>
#include <malloc.h>
#define MAX 5
int arr[MAX];
int p0 = 0;
int p1 = 0;
int p2 = 0;
int p3 = 0;
int p4 = 0;
void *process(void *arg);
int main(int argc, char *argv[]) {
if(argc != 7) {
printf("must be 6 ints");
return -1;
}
int quantum = atoi(argv[1]);
p0 = atoi(argv[2]);
p1 = atoi(argv[3]);
p2 = atoi(argv[4]);
p3 = atoi(argv[5]);
p4 = atoi(argv[6]);
下面是我指的 pthread。
int *pointer0 = malloc(sizeof(*pointer0));
*pointer0 = p0;
/* intialize thread 1 */
pthread_t tid0;
pthread_attr_t attr0;
pthread_attr_init(&attr0);
pthread_create(&tid0, &attr0, process, 1);
pthread_join(tid0, NULL);
//if the time remaining of process is not zero, run process[i] again
return 0;
}
void *process(void *arg) {
//char **pointer = (char**) arg;
//int burst = **pointer;
int burst = 0;
//int i = atoi(arg);
printf("%ls", (int *)arg);
pthread_exit(NULL);
}
如果您阅读 pthread_create()
函数的手册页,您会注意到在最后一个参数中传递的是 int
而不是 void*
。
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg);
要解决此问题,请创建一个变量来存储 int
值,并使用其地址将其传递给 pthread_create()
函数。然后,在 process()
函数中,将参数转换为 int
.
int main () {
// (...)
int arg = 1;
pthread_create(&tid0, &attr0, process, &arg);
// (...)
return 0;
}
void *process (void *arg) {
int arg = *((int *) arg);
// (...)
return NULL;
}
现在,注意我刚刚在 main()
函数中声明的变量 arg
将始终可用,一旦程序只在最后退出 main()
函数。如果在另一个函数中调用 pthread_create()
函数,强烈建议您动态分配内存,以确保即使在函数退出时它也始终可用。不要忘记在退出 process()
函数之前释放内存。
int foo () {
// (...)
int *arg = malloc( sizeof(int) );
if ( !arg ) {
fprintf(stderr, "error: allocating memory");
return 0;
}
*arg = 1;
pthread_create(&tid0, &attr0, process, arg);
// (...)
return 0;
}
void *process (void *arg) {
int value = *((int *) arg);
// (...)
free(arg);
return NULL;
}
如果您想通过 pthread_create()
函数传递更多变量,请声明一个 struct
并重复相同的过程。
typedef struct {
char name[50];
char surname[50];
int age;
} person_t;
int foo () {
// (...)
person_t *arg = malloc( sizeof(person_t) );
if ( !arg ) {
fprintf(stderr, "error: allocating memory");
return 0;
}
sprintf( arg->name , "Miguel" );
sprintf( arg->name , "Carvalho" );
arg->age = 22;
pthread_create(&tid0, &attr0, process, arg);
// (...)
return 0;
}
void *process (void *arg) {
person_t person = *((person_t *) arg);
printf( "%s; %s; %d\n", person.name, person.surname, person.age);
// (...)
free(arg);
return NULL;
}
无法将 pthread 的最后一个参数作为实际 int 传递。我希望能够访问 pthread_create 的最后一个参数中的 1。任何帮助将不胜感激!
#include <pthread.h>
#include <unistd.h>
#include <malloc.h>
#define MAX 5
int arr[MAX];
int p0 = 0;
int p1 = 0;
int p2 = 0;
int p3 = 0;
int p4 = 0;
void *process(void *arg);
int main(int argc, char *argv[]) {
if(argc != 7) {
printf("must be 6 ints");
return -1;
}
int quantum = atoi(argv[1]);
p0 = atoi(argv[2]);
p1 = atoi(argv[3]);
p2 = atoi(argv[4]);
p3 = atoi(argv[5]);
p4 = atoi(argv[6]);
下面是我指的 pthread。
int *pointer0 = malloc(sizeof(*pointer0));
*pointer0 = p0;
/* intialize thread 1 */
pthread_t tid0;
pthread_attr_t attr0;
pthread_attr_init(&attr0);
pthread_create(&tid0, &attr0, process, 1);
pthread_join(tid0, NULL);
//if the time remaining of process is not zero, run process[i] again
return 0;
}
void *process(void *arg) {
//char **pointer = (char**) arg;
//int burst = **pointer;
int burst = 0;
//int i = atoi(arg);
printf("%ls", (int *)arg);
pthread_exit(NULL);
}
如果您阅读 pthread_create()
函数的手册页,您会注意到在最后一个参数中传递的是 int
而不是 void*
。
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg);
要解决此问题,请创建一个变量来存储 int
值,并使用其地址将其传递给 pthread_create()
函数。然后,在 process()
函数中,将参数转换为 int
.
int main () {
// (...)
int arg = 1;
pthread_create(&tid0, &attr0, process, &arg);
// (...)
return 0;
}
void *process (void *arg) {
int arg = *((int *) arg);
// (...)
return NULL;
}
现在,注意我刚刚在 main()
函数中声明的变量 arg
将始终可用,一旦程序只在最后退出 main()
函数。如果在另一个函数中调用 pthread_create()
函数,强烈建议您动态分配内存,以确保即使在函数退出时它也始终可用。不要忘记在退出 process()
函数之前释放内存。
int foo () {
// (...)
int *arg = malloc( sizeof(int) );
if ( !arg ) {
fprintf(stderr, "error: allocating memory");
return 0;
}
*arg = 1;
pthread_create(&tid0, &attr0, process, arg);
// (...)
return 0;
}
void *process (void *arg) {
int value = *((int *) arg);
// (...)
free(arg);
return NULL;
}
如果您想通过 pthread_create()
函数传递更多变量,请声明一个 struct
并重复相同的过程。
typedef struct {
char name[50];
char surname[50];
int age;
} person_t;
int foo () {
// (...)
person_t *arg = malloc( sizeof(person_t) );
if ( !arg ) {
fprintf(stderr, "error: allocating memory");
return 0;
}
sprintf( arg->name , "Miguel" );
sprintf( arg->name , "Carvalho" );
arg->age = 22;
pthread_create(&tid0, &attr0, process, arg);
// (...)
return 0;
}
void *process (void *arg) {
person_t person = *((person_t *) arg);
printf( "%s; %s; %d\n", person.name, person.surname, person.age);
// (...)
free(arg);
return NULL;
}