Thread: global structures

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    16

    global structures

    ok..so my program uses threads..but the basics are this...lets say i have file1.c, file2.c and file3.c..

    atm, i have defined the struct in a file called struct_definitions.h... so basically file1 creates the threads and then the thread is located in file2. the structure is passed to the thread through the thread creation argument. the thread in file2.c writes a value to the struct and ends..then the thread in file3 is supposed to read and print that value.

    the reading of the value is ok, as soon after the read i print it and its correct. but when i print it out in the other function, the value comes as 0, which means that it is clearly wrong.

    struct definition in struct_definitions.h
    Code:
    struct analog_input
    {
    	float analog_input_value;
    };
    since everything works ok, i will post only the problematic read function which is supposed to read the struct
    Code:
    void *analog_printfunction0 (void *analogin0struct)
    {
    struct analog_input *ptr = (struct analog_input*) &analogin0struct;
    
    printf("Analog In 0 value : %f\n",ptr->analog_input_value);
    }
    hope someone can help me!

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Clue:
    Code:
    struct analog_input *ptr = (struct analog_input*) &analogin0struct;
    Why do you feel the "address of" operator is a good idea here?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Also, the use of the type cast is not really needed. The use of void * should be okay to assign to anything without errors.

    That is

    struct analog_input *ptr = analogin0struct;

    should be allowed.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Kennedy View Post
    Also, the use of the type cast is not really needed. The use of void * should be okay to assign to anything without errors.
    In this case, stylistically, there is some justification for making the conversion explicit. There is no chance of masking another error (as is the case when an explicit conversion is done on return from malloc()) and the working of the function relies on a conversion happening. Better to make it obvious.

    Also, in cases where the C compiler is actually a C++ compiler or the code must work in both C and C++, the conversion is required.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    16
    i made the changes as suggested and removed the address operator. but still its the same result..

    in the main file where the thread which writes to the struct is created ive done this
    Code:
    struct analog_input analogin0struct;
    
    //and then create the thread to write to it. write is successful
    also in the file where the struct is read i have included the file with the struct definitions and added
    Code:
    extern struct analog_input analogin0struct;
    would it be easier if i create a global struct in the main file and then read it from the output file?but how do you do that?

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Show us how you are creating the thread, and how you are writing to the structure. Preferably provide a small but complete example of actual code that illustrates your concern, otherwise we are guessing blind.

    I'm guessing the struct you are creating is local to a function, so no longer exists when your thread function attempts to use it. Alternatively, you may have left out a required ampersand.

    Using global variables that are accessed by multiple threads is, often, an effective way to deliver a buggy program.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with Nested Structures and Arrays of Structures
    By Ignoramus in forum C Programming
    Replies: 4
    Last Post: 03-02-2010, 01:24 AM
  2. Whats the best way to control a global class?
    By parad0x13 in forum C++ Programming
    Replies: 3
    Last Post: 11-12-2009, 05:17 PM
  3. TCL Scripting
    By johndejesus in forum Tech Board
    Replies: 5
    Last Post: 08-07-2009, 03:35 AM
  4. Please help... Global structures
    By msmith in forum C Programming
    Replies: 5
    Last Post: 09-16-2007, 06:18 PM