multithreading c++

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2007
Posts: 12
Reputation: jimmy.rocks1 is an unknown quantity at this point 
Solved Threads: 0
jimmy.rocks1 jimmy.rocks1 is offline Offline
Newbie Poster

multithreading c++

 
0
  #1
Jan 16th, 2008
Hi,
I am having a problem in implementing multithreading in my program.i have a function which goes like this
int x=NodesArray[2]->SendData(routeInfo,countDataPackets,pathInf);

i want to call this function with the help of CreateThread
and i did it this way

hThread[0]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)NodesArray[2]->SendData(routeInfo,countDataPackets,pathInf),NULL,0,&dwID[i]);
i just read some programs online and read microsoft visual c++ implementation of creation of threads.I know that the createthread returns a dword value..i couldnt really understand what this dword means and i want to know how i can send multiple parameters using createthread function as you can see my SendData function is using three parameters.could any suggest me how to go about this procedure..thanks in advance...
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,346
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1460
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: multithreading c++

 
0
  #2
Jan 16th, 2008
First, the new thread must be either a static class method of a simple function. A DWORD is just an unsigned long, but don't count on that being the case in all versions of Windows.

To pass multiple parameters, create a structure with all the variables you want to pass and pass a pointer to that object in CreateThread.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 12
Reputation: jimmy.rocks1 is an unknown quantity at this point 
Solved Threads: 0
jimmy.rocks1 jimmy.rocks1 is offline Offline
Newbie Poster

Re: multithreading c++

 
0
  #3
Jan 17th, 2008
thank you for replying
i have two questions on your reply i am writing my code in visuall c++ 6 and it is saying that create thread is returning a void * . i came to know about this when i tried to typecast it to integer.

and also you have told that to pass multiple parameters i need to create a structure and put all my variables in it and pass the object of that structure.can i not do by declaring a class and putting those variables in the class and creating a class object and passing that object as parameter..

thanks
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,346
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1460
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: multithreading c++

 
0
  #4
Jan 18th, 2008
>>create thread is returning a void *
It actually returns a HANDLE, which is probably a void*. As long as hThread[0] is a HANDLE your code should be ok.

For your 2nd question, yes you can pass a pointer to a class object. just typecast it to void*
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 12
Reputation: jimmy.rocks1 is an unknown quantity at this point 
Solved Threads: 0
jimmy.rocks1 jimmy.rocks1 is offline Offline
Newbie Poster

Re: multithreading c++

 
0
  #5
Jan 21st, 2008
Hi thank you for replying..

could you tell me how to actually declare a class method as thread
for example:-

class X{
int method m1;
}
int X::method m1{
cout<< i m in method 1;
}
how can i declare a method m1 as thread

becoz in the examples that i have seen i could see the declaration as

DWORD WINAPI mythread(LPVOID lparam){
}

i couldnt find any example for the above case...coudl you give me any example of that kind
thanks
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,346
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1460
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: multithreading c++

 
0
  #6
Jan 21st, 2008
  1. class X{
  2. public:
  3. X() {m1 = 0;}
  4. static DWORD WINAPI mythread(LPVOID lparam);
  5. protected:
  6. int method m1;
  7. };

Since mythread() is a static member of that class you can pass it to CreateThread as the thread function.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 12
Reputation: jimmy.rocks1 is an unknown quantity at this point 
Solved Threads: 0
jimmy.rocks1 jimmy.rocks1 is offline Offline
Newbie Poster

Re: multithreading c++

 
0
  #7
Jan 25th, 2008
thanks buddy...i could get the result....thanks for helping me....
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 12
Reputation: jimmy.rocks1 is an unknown quantity at this point 
Solved Threads: 0
jimmy.rocks1 jimmy.rocks1 is offline Offline
Newbie Poster

Re: multithreading c++

 
0
  #8
Jan 31st, 2008
Hi ,
i could actually create the thread but what if i want to terminate a thread.....i found that there is function SuspendThread(threadHandle)...but i could even see in the microsoft documentation that the function has been truncated ...is there any other way that i could suspend a thread, thanks in advance.....
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC