Why is it not accessing the global struct definition?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Why is it not accessing the global struct definition?
Code:
#include<pthread.h>
int sum1;
int sum2;
struct arg_struct {
int size;
int array1[size];
};
void *thread1() {
struct arg_struct *args = (struct arg_struct *)args;
sum1 = 0;
int i;
for (i = 0; i < arg_struct.size; i++) {
// printf ( "%d ", i );
sum1 += array2
[i];
}
return NULL;
}
void *thread2() {
struct arg_struct *args = (struct arg_struct *)args;
sum2 = 0;
int i;
for (i = 0; i < arg_struct.size; i++) {
// printf ( "%d ", i );
sum2 += array2[i];
}
return NULL;
}
int main ( int argc, char** argv )
{
pthread_t t1, t2;
struct arg_struct args;
arg_struct.size = 5000;
arg_struct.array1 = array1[size];
pthread_create(&t1,NULL,&thread1,(void *)arg_struct));
pthread_create(&t2,NULL,&thread2,(void *)arg_struct));
pthread_join(t1,NULL);
return 0;
}
[james@james Desktop]$
[james@james Desktop]$
[james@james Desktop]$ gcc example2.c -o exam3 -lpthread
example2.c: In function ‘main’:
example2.c:45: error: ‘arg_struct’ undeclared (first use in this function)
example2.c:46: error: ‘array1’ undeclared (first use in this function)
example2.c:49: error: expected ‘;’ before ‘)’ token
example2.c:49: error: expected statement before ‘)’ token
example2.c:50: error: expected ‘;’ before ‘)’ token
example2.c:50: error: expected statement before ‘)’ token
Okay I am trying to debug this program and as I said before,this is my first time usiing struct -so I am new to this.
The only errors I am concerned about is why it is not seeing the arg_struct, says it is undeclared, I defined it in the global variables area, it is a struct.
Also it says array1 is undeclared. Again it is in the globals area so I do not know why it is undeclared. It is a global varible, part of the struct.
There are other things wrong with this program, but the these errors in main are the only two I am concerned about now. They should not be there.
really do.
Also think about if array1 is really a global variable or a member of a global variable.
Think about how you access a struct.
Just some minor corrections of typos. line 48,49 remove one of the ending ')'. The cast to a void pointer only needs () and the function itself needs (). I'm not really sure what you want to do here but I'd say you want to pass a pointer to a variable of the type arg_struct, right? think about how you would pass a pointer to a int variable. Your would not call like this.
Code:
pthread_create(&t1,NULL,&thread1,(void *)int);
Nother thing if you want to assign a value to a struct member variable where the struct variable is a pointer use name_of_struct_variable->name_of_member_variable. This way you can leave out the pointer reference.
I just let you tinker abit about structs for some time so you get it yourself. Hope thats fine with you. If anything needs further hints just hit me. Maybe just write a little program that only copes with structs and not threads and alike. Start with just a struct inside main. Do some work with it. The get a pointer to it. Work with it. Next step is a function that takes a struct and so on.
Thanks you for all of your help. I will try them all. I did get a few of these after
looking at the code last night.
Again thanks, and please remember that this is my initial attempt at pthread_create programming.
I have a lot of c programmers working near my desk work area; when I asked them about this they waved it off. Being a c programmer does
not mean that you know how to parallel program with pthrads.
As you go on two new things in one I'd say step back on one. Learning one of them first and once you got the hang incorperate the next new thing.
As I also never did anything with pthread I'd say you go with struct first as I'd be off better help. Also there for sure are more knowing people on the forum and on the thread so it is definitely your choice.
i think these last two posts have hit the problem squarely. I am using pthread_create to create the two (actully the second thread) and thta is where the program bombs.
Here is what I am using for my instructions set:
1. All aruments must be paased by reference and cast to (void *)
2. Only one arument to the thread start routine
3. For multiple arguments
1. Create a structure that contains all of the arguments
2. Pass a pointer to that structure in pthread_create().
It is the first one "All aruments must be paased by reference and cast to (void *)"
I know how to cast an integer variable to real variable and cast a real variable to integer variable. But,
cast to void? What does that mean?
I am still a little confused as to what you are inquiring. One moment you seem to understand how to pass values through pthread_create(), and then your next statement indicates that you are confused.
How about an example...
Code:
#include <pthread.h>
struct MyStruct
{
int iterations;
};
void* MyThread1(void* arg)
{
int* value = (int*) arg;
for (int i = 0; i < *value; ++i)
{
// blah
}
return NULL;
}
void* MyThread2(void* arg)
{
struct MyStruct* ms = (struct MyStruct*) arg;
for (int i = 0; i < ms->iterations; ++i)
{
// blah
}
return NULL;
}
int main()
{
pthread_t tid1, tid2;
int iterations = 5;
struct MyStruct data = {iterations};
pthread_create(&tid1, NULL, MyThread1, (void*) &iterations);
pthread_create(&tid2, NULL, MyThread2, (void*) &data);
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
return 0;
}
Last edited by dwhitney67; 11-27-2013 at 12:20 PM.
Reason: fixed typo and bad function signatures
"passed by reference and cast to (void *). Please explain.
Lets see void * is a pointer of the type void. Also void is not not really a variable type but more of a generic type that says okay I'm not so sure what I am so I tell you when I come around.
Void can be used in three cases. As the return type of a function. Meaning dont return anything. Other languages call this a procedure.
Second case when used as the argument list of a function. Means this function does does not take any arguments. Think about int main(void).
And the third case is when used with references/pointers. This means the pointer has _no_ datatype. Just a wildcard that makes the compiler see what actually comes upfront then readies some space and moves the pointer onward.
Like in restaurant where the man at the desk does not know how many people want a table. So he counts them readies a table and serves them.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.