Thread: Double linked structure between 2 threads

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    12

    Double linked structure between 2 threads

    Hello everyone,


    I am on a little project involving TCP socket-programming and multiple threads but need some help for passing structures between two threads.
    The first thread receives data with respective ID, temp, etc,.. Each ID is a list-item of a linked list and every ID has again a linked list of all the received data.

    The second thread just have to access the list and perform some manipulations on it.
    My solution: pass the head pointer of the linked list. Easy enough, right? Somehow, I am stuck receiving error message:" dereferencing pointer to incomplete type."

    For the sake of ease and simplicity I just added a stripped down version of the project.

    I'd like to thank all of you in advance,
    Kish

    EDIT: The magic (or not) happens in MainProcess.c: The Data thread should receive the pointer (a think) from the Connection thread.
    Attached Files Attached Files
    Last edited by Kishintai; 06-02-2014 at 04:13 AM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Rather than trying to post attachments, it is better to embed code in your posts using [code][/code] tags.

    In any event, even without looking at your code, a message about "dereferencing a pointer to an incomplete type" means you have code which only uses a forward declaration of a type, and tries to dereference a pointer to that type.

    For example
    Code:
    struct Foo;      /*  optional forward declaration of struct Foo */
    
    void func(struct Foo *x) 
    {
        x->i = 42;    /*  struct Foo is an incomplete struct type here */
    }
    and (for example, in a header file)
    Code:
    struct Foo
    {
          int i;
    }
    The solution is that the compiler needs to see the complete struct definition, not just a forward declaration. So either #include the header file that defines the struct type, or ensure every source file that attempts to dereference such a pointer defines the struct type BEFORE dereferencing a pointer (and they all must do it the same way, to avoid clashes with the one-definition rule).
    Last edited by grumpy; 06-02-2014 at 04:51 AM.
    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.

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    12
    Thanks grumpy, I just copy-pasted the struct.
    But I'm still somehow stuck. While I pass a pointer if my list to the 2nd thread, I still can't manage to "see" the data in my list.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    That's not exactly a clear explanation, but ..... Lemme guess. You copy/pasted the struct definition to the end of the source files. You need to copy it to the beginning.

    A C compiler doe not read a whole file, and then work out if the contents make sense. It scans from the beginning to the end (not the other way around) and also needs to see things declared before they are used. So this will compile
    Code:
    struct Foo {int i;};
    
    void func(struct Foo *x)
    {
        x->i = 42;
    }
    but this will not
    Code:
    void func(struct Foo *x)
    {
        x->i = 42;
    }
    
    struct Foo {int i;};
    The only difference is the position of the struct definition.
    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
    Apr 2013
    Posts
    12
    Quote Originally Posted by grumpy View Post
    That's not exactly a clear explanation, but ..... Lemme guess. You copy/pasted the struct definition to the end of the source files. You need to copy it to the beginning.

    A C compiler doe not read a whole file, and then work out if the contents make sense. It scans from the beginning to the end (not the other way around) and also needs to see things declared before they are used.
    The struct definition is in the beginning. Sorry for the unclarity but the error of "dereferencing a pointer to an incomplete type" has been solved.
    So I succesfully pass my struct from one thread to another. But the problem is that while the first thread inserts items in the list my 2nd thread only "sees" the initialised allocated 1st element.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Without showing some representative code, nobody can guess what you problem is. There are many possibilities.

    You don't need to provide you complete code - in fact, it's a good idea not to. Try providing a SMALL but COMPLETE sample of code that exhibits the same problem (without extraneous details that are specific to your code, rather than the problem).
    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. 2 dimensional double linked list structure
    By kyber in forum C Programming
    Replies: 9
    Last Post: 12-12-2010, 07:50 PM
  2. Replies: 1
    Last Post: 04-02-2009, 06:51 AM
  3. double linked-structure nightmare help
    By MalickT in forum C Programming
    Replies: 11
    Last Post: 05-14-2008, 11:33 AM
  4. Circularly Double-Linked List Structure
    By NavyBlue in forum C Programming
    Replies: 2
    Last Post: 10-15-2002, 10:58 PM
  5. Replies: 1
    Last Post: 03-21-2002, 06:10 PM

Tags for this Thread