![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Sep 2005
Posts: 28
Rep Power: 0
![]() |
Passing array as argument to a thread
I am currently busy developing an implementation of the floyd parallel algorithm using POSIX threads.I have an array named node_array[NODE_NUM][NODE_NUM] (where NODE_NUM is the number of nodes in the graph) defined globaly that holds the data read from a file.
I want to pass that array as an argument to each thread but in fuction pthread_create(&thread, NULL, (void*)function, (void*) arg) i can give only one pointer of type void as arguments to pass to the calling fuction.I have two questions: 1) Can i pass a two dimensional array as a void pointer? 2) Suppose that thread is defined within a struct and i have an array of structs, each holding a thread and some thread information.How can i find from within the thread to which struct does it belong.(i could use an integer to identify each thread but i cannot pass it to the thread calling function due to the one argument restriction of the pthread_create funtion. One idea was to build another struct that holds all the data that I want to pass to the thread function and use a pointer to that struct.But I think that is not the optimum solution because of the number of the threads(up to 16) and the size of the array(up to 512 nodes). Anyone who can help , plz do ![]() If you have any questions ask. thanks in advance
__________________
The geeks shall inherit the earth. |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4
![]() |
1) yes.
Pass a pointer like this: &myarray[0][0] 2) All global data is visible to all threads. Add some kind of integer identifier to the struct to let the thrread know which one is "me". Put you &myarray[0][0] pointer into the struct as well, then pass the address of the struct. |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Sep 2005
Posts: 28
Rep Power: 0
![]() |
thnx.i'll try the first.
as for the thread struct, it is : typedef struct _mythread_t
{
int iter;
int id;
pthread_t thread;
mybuffer_t row;
mybuffer_t column;
} mythread_t;but suppose that i have an array mythread_t threads[THREAD_NUM]; If, for example, threads[1] is the thread that is running the fuction parallel_sorting.From within the function how can I get the thread id? pthread_self() will be no good as I have used other ids for the threads than the ones returned by the pthread_create().
__________________
The geeks shall inherit the earth. |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4
![]() |
Do you know about pthread_equal()?
|
|
|
|
|
|
#5 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4
![]() |
Oops - I see you are creating your own thread id's. Nomrally that is not a good idea - as you've discovered.
Are any of the the thread struct's members unique and indentifiable? ie., can you use one of them to reverse-engineer it into your version of the thread id? |
|
|
|
|
|
#6 |
|
Newbie
Join Date: Sep 2005
Posts: 28
Rep Power: 0
![]() |
I use my ids because that way it's easier to define which thread takes care of which part of the node_array.
As for the reverse-engineering i don't think it's possible because the only unique member of the thread struct is the thread id... maybe i'll change the program to use the ids that pthread_self() returns and maybe use an other int to store the numbers that are the thread array indexes for defining the workspace of each thread. thnx for the help
__________________
The geeks shall inherit the earth. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|