Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 30th, 2005, 8:19 AM   #1
Symptom
Newbie
 
Symptom's Avatar
 
Join Date: Sep 2005
Posts: 28
Rep Power: 0 Symptom is on a distinguished road
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.
Symptom is offline   Reply With Quote
Old Sep 30th, 2005, 8:42 AM   #2
jim mcnamara
Hobbyist Programmer
 
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4 jim mcnamara is on a distinguished road
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.
jim mcnamara is offline   Reply With Quote
Old Sep 30th, 2005, 8:55 AM   #3
Symptom
Newbie
 
Symptom's Avatar
 
Join Date: Sep 2005
Posts: 28
Rep Power: 0 Symptom is on a distinguished road
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.
Symptom is offline   Reply With Quote
Old Sep 30th, 2005, 4:28 PM   #4
jim mcnamara
Hobbyist Programmer
 
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4 jim mcnamara is on a distinguished road
Do you know about pthread_equal()?
jim mcnamara is offline   Reply With Quote
Old Sep 30th, 2005, 4:34 PM   #5
jim mcnamara
Hobbyist Programmer
 
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4 jim mcnamara is on a distinguished road
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?
jim mcnamara is offline   Reply With Quote
Old Sep 30th, 2005, 6:52 PM   #6
Symptom
Newbie
 
Symptom's Avatar
 
Join Date: Sep 2005
Posts: 28
Rep Power: 0 Symptom is on a distinguished road
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.
Symptom is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:58 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC