LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-21-2013, 03:36 PM   #1
jyunker
Member
 
Registered: Aug 2009
Posts: 167

Rep: Reputation: 0
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.

Any help appreciated. Thanks in advance.

Respectfully,


jyunker
 
Old 11-22-2013, 01:10 AM   #2
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Hi,

have you tried using
Code:
struct foo {
...
};
use
Code:
typdef struct {
...
} foo;
See for example http://stackoverflow.com/questions/6...ef-struct-in-c for a nice discussion.

Evo2.
 
Old 11-22-2013, 02:15 AM   #3
zhjim
Senior Member
 
Registered: Oct 2004
Distribution: Debian Squeeze x86_64
Posts: 1,748
Blog Entries: 11

Rep: Reputation: 233Reputation: 233Reputation: 233
Think about what the lines
Code:
struct arg_struct  {
   int size;
   int array1[100];
};
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.
 
Old 11-22-2013, 09:54 AM   #4
jyunker
Member
 
Registered: Aug 2009
Posts: 167

Original Poster
Rep: Reputation: 0
Thanks!!

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.

You help is sincerely appreaciated.

Respectfully,

jyunker
 
Old 11-22-2013, 10:22 AM   #5
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by jyunker View Post
Any help appreciated. Thanks in advance.
Your thread functions have a malformed function-signature; they should define a parameter such as:
Code:
void *thread1(void* arg) {

    struct arg_struct *args = (struct arg_struct *)arg;
    ...
}

void *thread2(void* arg) {

    struct arg_struct *args = (struct arg_struct *)arg;
    ...
}

Last edited by dwhitney67; 11-22-2013 at 10:24 AM.
 
Old 11-25-2013, 01:49 AM   #6
zhjim
Senior Member
 
Registered: Oct 2004
Distribution: Debian Squeeze x86_64
Posts: 1,748
Blog Entries: 11

Rep: Reputation: 233Reputation: 233Reputation: 233
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.
 
Old 11-27-2013, 11:57 AM   #7
jyunker
Member
 
Registered: Aug 2009
Posts: 167

Original Poster
Rep: Reputation: 0
cast to (void *)

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?


Any help appreciated. Thanks in advance.

Respectfully,


jyunker
 
Old 11-27-2013, 12:00 PM   #8
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by jyunker View Post
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?
What do you mean by a "real" variable? Are there such things as "unreal" or "fake" variables?
 
Old 11-27-2013, 12:06 PM   #9
jyunker
Member
 
Registered: Aug 2009
Posts: 167

Original Poster
Rep: Reputation: 0
elucidation

Okay, you are casting a value or an argument. Sorry about that.

I think that you know what I mean. I am confused by:

"passed by reference and cast to (void *). Please explain.


Respectfully,

jyunker
 
Old 11-27-2013, 12:17 PM   #10
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
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
 
Old 11-28-2013, 01:36 AM   #11
zhjim
Senior Member
 
Registered: Oct 2004
Distribution: Debian Squeeze x86_64
Posts: 1,748
Blog Entries: 11

Rep: Reputation: 233Reputation: 233Reputation: 233
Quote:
Originally Posted by jyunker View Post
"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.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
problem accessing global variables in script ziggycat Programming 2 04-10-2011 07:16 AM
GCC compile problem:struct A have a member variable which is just a struct type name? leon.zcom Programming 3 04-18-2008 04:40 PM
Accessing a struct within a struct? smoothdogg00 Programming 3 12-21-2006 01:38 AM
switch statement converting struct char to struct int oceaneyes2 Programming 2 12-10-2003 04:30 PM
Accessing a struct inside struct cxel91a Programming 1 09-17-2003 04:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:10 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration