简体   繁体   English

如何使用argv [1]作为预定义的宏?

[英]How to use argv[1] as a predefined macro?

[Coding in POSIX, using Geany in Ubuntu] [在POSIX中编码,在Ubuntu中使用Geany]

I have these classes: main.c , Ac , Ah where Ah is included in main.c. 我有以下类: main.cAcAh ,其中main被包含在main.c中。 I started all my project using a macro in Ac, namely #define PNUM 10 . 我使用Ac中的宏(即#define PNUM 10开始了所有项目。 I already defined structures, several arrays, I created threads and so on and eventually when I got everything done, I decided to ask the user to enter an integer as PNUM using argv in main.c file. 我已经定义了结构,几个数组,创建了线程等等,最终当我完成所有工作后,我决定要求用户在main.c文件中使用argv输入一个整数作为PNUM ( PNUM determines the number of threads that I'm going to deal with.) PNUM决定了我要处理的线程数。)

  • I don't want to pass this value to Ac file using functions in main.c: I tried so but it's my first experience in POSIX and the level of difficulty went over my knowledge. 我不想使用main.c中的函数将此值传递给Ac文件:我曾尝试过,但这是我在POSIX上的第一次经验,而其难度却超过了我的知识。

  • I know that #define PNUM 10 is a symbolic name to numeric constants and cannot be reassigned at all. 我知道#define PNUM 10是数字常量的符号名称,根本无法重新分配。 I also don't insist on keeping this line of my code, however, it seems crucial here.(eg creating PNUM number of threads in initialization step and allocating memory to arrays of condition variables, etc.) 我也不必坚持要保留这行代码,但是,这似乎很关键(例如,在初始化步骤中创建PNUM数量的线程,并为条件变量数组分配内存等)。

  • My question seems to have an easy and straightforward answer but I am not very comfortable with C . 我的问题似乎有一个简单直接的答案,但我对C不太满意。


The general scheme: All functions mentioned in Ac need to know PNUM. 通用方案:Ac中提到的所有功能都需要知道PNUM。

main.c main.c

#include "philos.h"

void* philosopher(int *);
void* waiter(int *);

int main(int argc, char** argv)
{
    int philNum = atoi(argv[1]);
    void * tab = tableinit(philosopher, waiter);
    return 0;
}

void * philosopher(int * who)
{   ...
    pickup((*who));
    putdown((*who));
    ...
}
void * waiter(int * who)
{   ...
    replaceBottles();
    refillBowls();
    ...
}

Ac 交流电

#define PNUM 10
#define WAITERNUM 2
typedef struct tablestruct
{
    pthread_t t[PNUM];
    pthread_t w[WAITERNUM];
    int self[PNUM];
    int wself[WAITERNUM];
    pthread_mutex_t mutex;
    pthread_cond_t condition[PNUM];
    philstat status[PNUM];
    int snack_wine[PNUM];
 }table;
void printstate(){...}
void pickup(int k){...}
void putdown(int k){...}
int test (int i){...}
int consumptions(int i){...}
int finishedServing(int){...}
void replaceBottles(int){...}
void refillBowls(int){...}
table * tableinit(void *(* philosopher)(void *), void*(* waiter)(void *)){...}

The question as it stands doesn't quite work. 目前的问题并不奏效。 You can't take user input and use it as a macro. 您不能接受用户输入并将其用作宏。 You know this: "...numeric constants and cannot be reassigned". 您知道这一点:“ ...数字常量,无法重新分配”。 As in, it's never going to be anything other than 10. 与之类似,它将永远不会是10。

So if you want the number of threads to vary depending on user-input, you're not going to be able to use a macro. 因此,如果您希望线程数根据用户输入而有所不同,则将无法使用宏。

I mean, you could do something lame like #define PNUM global_num_threads or '#define PNUM getNumThreads()`, and add a global or a function to go get that variable. 我的意思是,您可以执行类似#define PNUM global_num_threads或'#define PNUM getNumThreads()`的操作,然后添加一个全局变量或函数来获取该变量。 While that might be the.... least intrusive into your codebase, it's a bad design that indicates rot. 虽然这可能是对代码库的最小干扰,但这是一个糟糕的设计,表明它是腐烂的。 If you're going to make this sort of change to the code, do it right. 如果要对代码进行这种更改,请正确执行。

Yeah, even though it "seems crucial" to use a macro, it's not. 是的,尽管使用宏“似乎至关重要”,但事实并非如此。 You can swap that out with a variable. 您可以将其替换为变量。

And passing variables is almost fundamental, so get used to it. 传递变量几乎是最基本的,因此请习惯它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM