Old February 25th, 2004, 11:40 AM
Member
 
Join Date: Oct 2003
Posts: 204
urika is an unknown quantity at this point (<10)
putting values in void *arglist

hi ,
i want to run a threads using _beginthread(...)
(no MFC)
inside the threadfunc i want to call a function with many variables.
how do i use the void *arglist so it points to a few variables ?
i hope my question makes sence 
thank u
Reply With Quote
  #2    
Old February 25th, 2004, 11:47 AM
gstercken's Avatar
Moderator
Power Poster
 
Join Date: Sep 2002
Location: 14° 39'19.65"N / 121° 1'44.34"E
Posts: 9,815
gstercken has a reputation beyond repute (3000+)gstercken has a reputation beyond repute (3000+)gstercken has a reputation beyond repute (3000+)gstercken has a reputation beyond repute (3000+)gstercken has a reputation beyond repute (3000+)gstercken has a reputation beyond repute (3000+)gstercken has a reputation beyond repute (3000+)gstercken has a reputation beyond repute (3000+)gstercken has a reputation beyond repute (3000+)gstercken has a reputation beyond repute (3000+)gstercken has a reputation beyond repute (3000+)
Re: putting values in void *arglist

Quote:
Originally posted by urika 
how do i use the void *arglist so it points to a few variables ?
Usually by creating a class or struct which contains the variables.
You would then use the void* to pass the pointer to an instance of
that class or struct, which you would have to cast back to the actual
type before accessing the members.
__________________
Guido Stercken-Sorrenti
Reply With Quote
  #3    
Old February 25th, 2004, 11:51 AM
Member
 
Join Date: Oct 2003
Posts: 204
urika is an unknown quantity at this point (<10)
thanks!
Reply With Quote
  #4    
Old February 29th, 2004, 05:34 AM
Member
 
Join Date: Oct 2003
Posts: 204
urika is an unknown quantity at this point (<10)
i call a function :
Code:
 bool MapViewInit(GEO p1, GEO p2, DISPLAY Disp, CONTROL Ctrl, HINSTANCE hInst)
{
		MapThreadArgList maparglist;  ////?????
		maparglist.Ctrl=Ctrl;
		maparglist.Disp=Disp;
		maparglist.p1=p1;
		maparglist.p2=p2;
		maparglist.hInst=hInst;
		HANDLE handle1 =(HANDLE)_beginthread( MapThreadFunc,0,
(void *)&maparglist); // create thread COUT << "\n >>> MapViewInit Run Successfully <<<\n"; return true; }
in order to run the thread i do :
Code:
void MapThreadFunc( void* args )
{
		GEO p1;
		GEO p2;
		DISPLAY Disp;
		CONTROL Ctrl;
		HINSTANCE hInst;
		MapThreadArgList arglist = *((MapThreadArgList*)args);
		p1=arglist.p1;
		p2=arglist.p2;
		Disp=arglist.Disp;
		Ctrl=arglist.Ctrl;
		hInst=arglist.hInst;
...
...
...
after i cast back to MapThreadArgList the info in arglist.p1 (and the others ) is lost.
what am i doing wrong?
Reply With Quote
  #5    
Old February 29th, 2004, 06:02 AM
gstercken's Avatar
Moderator
Power Poster
 
Join Date: Sep 2002
Location: 14° 39'19.65"N / 121° 1'44.34"E
Posts: 9,815
gstercken has a reputation beyond repute (3000+)gstercken has a reputation beyond repute (3000+)gstercken has a reputation beyond repute (3000+)gstercken has a reputation beyond repute (3000+)gstercken has a reputation beyond repute (3000+)gstercken has a reputation beyond repute (3000+)gstercken has a reputation beyond repute (3000+)gstercken has a reputation beyond repute (3000+)gstercken has a reputation beyond repute (3000+)gstercken has a reputation beyond repute (3000+)gstercken has a reputation beyond repute (3000+)
Quote:
Originally posted by urika 
what am i doing wrong?
You are creating maparglist on the stack - it will be destroyed when it gets out of
scope (when you return from MapViewInit()). Allocate it on the heap, or keep it as a
member variable instead.
__________________
Guido Stercken-Sorrenti
Reply With Quote
  #6    
Old February 29th, 2004, 07:55 AM
Member
 
Join Date: Oct 2003
Posts: 204
urika is an unknown quantity at this point (<10)
thanks , it now works
Reply With Quote

결론
구조체를 사용하여 쓰레드에 넘겨줄 인수를 모두 넣는다. (구조체 정의를 전역변수로 해야 함)
구조체의 객체를 만든 다음 객체멤버에 값을 넣어준다.
통째로 객체를 넘겨준다.
스레드에서 구조체의 객체를 만들어 파라메터의 객체를 받는다.
쓰면된다~ 

'Development > C/C++' 카테고리의 다른 글

[Mutex Thread 동기화 예제]  (0) 2011.11.30
[Sleep함수]  (0) 2011.11.29
[CreateThread(), _beginthread(), _beginthreadex()의 차이]  (0) 2011.11.22
[select FD_SET FD_Zero FD_Clr FD_IsSet]  (0) 2011.11.22
[_beginthreadex 형변환문제]  (0) 2011.11.22
Posted by cyj4369
,