Thread: passing a structure to a pthread

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    6

    passing a structure to a pthread

    I am just learning pthreads, and the only examples I have pass an int pointer to the shared data which is an integer(obviously).

    I have a structure:

    Code:
    struct player { char form; char first_team; char playing; };

    I have a 2D array of cells each containing a player structure:

    Code:
    struct player myplayer[r][c];

    I am trying to get each of these cells in the 2D array to run its own pthread, in which the thread's function changes some of the variables (form, first_team, playing) of the structure.
    At the moment I keep getting 'myplayer[0][0] is undefined in the function...' so I am not passing it correctly as I am unsure how to do this.

    I am having trouble with the syntax for passing this structure/ a pointer to this structure to the pthread. Any help would be greatly appreciated. Thanks.

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    struct player myplayer[r][c];
    u cont do this. when u declare an array u should specify it size as an const value. It cant be variable which gives you a size. Well see the below code

    Code:
    struct player myplayer[10][10];
    This is a 10 by 10 2D array of type struct player

    ssharish2005

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    6
    yes i actually have used constants, just quickly put r and c for the purpose of the example. thanks.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    So if you have a 10x10 array, that's 100 threads you're creating right - one for each element of the array.
    That's a lot of threads, and a lot of thread overheads.

    When you create the thread, pass something like
    &myplayer[0][0]
    which is a pointer to a specific element.

    In the thread, cast the void* pointer to
    struct player *playerPtr = voidptr;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Dec 2006
    Posts
    6
    Thanks Salem, exactly what I needed. No I won't have an overhead that big fortunately!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing structure by reference or pointer?
    By 7force in forum C Programming
    Replies: 8
    Last Post: 12-13-2010, 06:49 PM
  2. pthread question how would I init this data structure?
    By mr_coffee in forum C Programming
    Replies: 2
    Last Post: 02-23-2009, 12:42 PM
  3. Passing Structure Pointers to Functions
    By samus250 in forum C Programming
    Replies: 15
    Last Post: 03-20-2008, 03:13 PM
  4. passing structure arrays to functions?
    By bem82 in forum C Programming
    Replies: 3
    Last Post: 10-30-2006, 06:17 AM
  5. passing a structure pointer by value
    By Bleech in forum C Programming
    Replies: 6
    Last Post: 07-11-2006, 05:58 PM