passing a structure to a pthread

This is a discussion on passing a structure to a pthread within the C Programming forums, part of the General Programming Boards category; I am just learning pthreads, and the only examples I have pass an int pointer to the shared data which ...

  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,682
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,496
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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, 05: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, 11:42 AM
  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, 05: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21