All pastes #244839 Raw Edit

Unnamed

public text v1 · immutable
#244839 ·published 2006-11-10 07:08 UTC
rendered paste body
Nov 09 17:53:49 (GR|phish)	still don't get this _beginthreadex stuff :<
Nov 09 17:54:36 (@Aaron[])	How come?
Nov 09 17:54:44 (GR|phish)	i just don't understand it.
Nov 09 17:54:51 (GR|phish)	it shows how to use it.
Nov 09 17:55:04 (GR|phish)	i just can't seem to figure out how to..
Nov 09 17:55:12 (GR|phish)	http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt__beginthread.2c_._beginthreadex.asp
Nov 09 17:56:50 (@Aaron[])	I don't see the problem?
Nov 09 17:56:51 (@Aaron[])	lol
Nov 09 17:56:57 (@Aaron[])	It's pretty straight forward.
Nov 09 17:57:07 (@Aaron[])	You call that function and it gives you a thread pointer.
Nov 09 17:57:09 (@Aaron[])	You just plug in arguements.
Nov 09 17:57:18 (GR|phish)	yeah
Nov 09 17:57:22 (GR|phish)	you get it.
Nov 09 17:57:25 (@Aaron[])	lolol.
Nov 09 17:57:31 (@Aaron[])	uintptr_t _beginthread( 
Nov 09 17:57:31 (@Aaron[])	   void( __cdecl *start_address )( void * ),
Nov 09 17:57:31 (@Aaron[])	   unsigned stack_size,
Nov 09 17:57:31 (@Aaron[])	   void *arglist 
Nov 09 17:57:31 (@Aaron[])	);
Nov 09 17:57:38 (GR|phish)	i don't cause i'm a fucking nubski and i don't get it,.
Nov 09 17:57:50 (@Aaron[])	What you'll want to do is make a function that will be called to start your thread.
Nov 09 17:58:03 *	Ranek has quit (Ping timeout)
Nov 09 17:58:04 (GR|phish)	ok
Nov 09 17:58:12 (@Aaron[])	It has to be declared a special way though.
Nov 09 17:58:48 (@Aaron[])	void __cdecl threadStart(void * params) { }
Nov 09 17:58:49 (@Aaron[])	Like that.
Nov 09 17:59:06 (GR|phish)	ok
Nov 09 17:59:07 (GR|phish)	well
Nov 09 17:59:18 (@Aaron[])	Do you understand the concept at least?
Nov 09 17:59:18 (GR|phish)	let's say i have a function called Func1()
Nov 09 17:59:24 (GR|phish)	i'm trying to.
Nov 09 17:59:31 (@Aaron[])	Ok ok let me interject first.
Nov 09 18:00:04 (@Aaron[])	What a thread is going to do is let you branch off your execution so things will be running in parallel.
Nov 09 18:00:09 (GR|phish)	yes
Nov 09 18:00:12 (coKe)	GR|phish: look up calling conventions
Nov 09 18:00:15 *	BradJ has quit (Ping timeout)
Nov 09 18:00:18 (@Aaron[])	So you use beginThread to launch a function in a seperate thread right.
Nov 09 18:00:25 (GR|phish)	yes
Nov 09 18:00:31 (@Aaron[])	And then your original code path continues in it's own thread.
Nov 09 18:00:48 (@Aaron[])	So you get that concept.
Nov 09 18:00:51 (GR|phish)	yes
Nov 09 18:01:08 (GR|phish)	beginthread -> makes my code in a new thread.
Nov 09 18:01:12 (@Aaron[])	Nothing else to get. Throw together your threaded part of your application starting with that function prototype I gave you.
Nov 09 18:01:31 (GR|phish)	ok
Nov 09 18:01:37 (@Aaron[])	Call the function at an appropriate time and start learning about thread management through trial and failure :D
Nov 09 18:09:29 (GR|phish)	Hmm
Nov 09 18:09:59 (GR|phish)	what do i do with:
Nov 09 18:09:59 (GR|phish)	    unsigned stack_size,
Nov 09 18:09:59 (GR|phish)	    void *arglist 
Nov 09 18:10:28 (GR|phish)	i've called the functions with __cdecl
Nov 09 18:11:11 (@Aaron[])	arglist
Nov 09 18:11:12 (@Aaron[])	Argument list to be passed to a new thread or NULL.
Nov 09 18:11:25 (@Aaron[])	If you want you can void* some data you might like in your threaded application.
Nov 09 18:11:42 (@Aaron[])	So you don't have to resort to thread-unsafe global data in order to communicate with your stuff.
Nov 09 18:11:51 (@Aaron[])	stack_size
Nov 09 18:11:51 (@Aaron[])	Stack size for a new thread or 0.
Nov 09 18:11:57 (@Aaron[])	Just give it 0, it'll alloc you default stack size.
Nov 09 18:12:00 (@Aaron[])	And that will be plenty.
Nov 09 18:12:16 (@Aaron[])	Also.
Nov 09 18:12:22 (@Aaron[])	You realize that you don't actually call the function you made.
Nov 09 18:12:29 (@Aaron[])	But pass it in as a parameter to beginThread right?
Nov 09 18:13:34 (GR|phish)	eh
Nov 09 18:13:52 (@Aaron[])	So for example.
Nov 09 18:13:56 (@Aaron[])	You make a function:
Nov 09 18:14:03 (@Aaron[])	void __cdecl threadStart(void * params) {
Nov 09 18:14:11 (@Aaron[])	  .//stuff
Nov 09 18:14:11 (@Aaron[])	}
Nov 09 18:14:18 (@Aaron[])	Then when you call beginthread it should look like:
Nov 09 18:14:55 (@Aaron[])	uintptr_t threadid = _beginThread( threadStart, 0, 0 );
Nov 09 18:17:08 (GR|phish)	40 C:\dev\main.cpp `_beginThread' was not declared in this scope 
Nov 09 18:18:04 (coKe)	don't think he knows about function pointers
Nov 09 18:18:04 (@Aaron[])	Did you #include <process.h>
Nov 09 18:18:05 (@Aaron[])	?
Nov 09 18:18:17 (GR|phish)	yes
Nov 09 18:18:26 (@Aaron[])	What compiler are you using?
Nov 09 18:18:32 (GR|phish)	dev-c++
Nov 09 18:18:38 (GR|phish)	i have msvisc++ ee
Nov 09 18:18:46 (@Aaron[])	You have to ensure that you're compiling for multithreaded applications.
Nov 09 18:19:16 (GR|phish)	how?
Nov 09 18:19:19 (@Aaron[])	The compiler switch on MS compilers is /MT instead of /ST, I'm not sure what it will be for yours.
Nov 09 18:19:22 (@Aaron[])	Try project options.
Nov 09 18:19:24 (@Aaron[])	Compiler options.
Nov 09 18:19:26 (@Aaron[])	Anything you can find.
Nov 09 18:19:44 (@Aaron[])	You might need to delve into the documentation for your compiler.
Nov 09 18:19:51 (@Aaron[])	coKe: I'm pretty sure he doesn't.
Nov 09 18:20:00 (@Aaron[])	In other news. My moving window works.
Nov 09 18:20:00 (GR|phish)	dev uses g++
Nov 09 18:20:01 (@Aaron[])	;D
Nov 09 18:20:09 (coKe)	screenshots
Nov 09 18:20:19 (coKe)	LOL, how'd you figure it out
Nov 09 18:20:31 (@Aaron[])	I found a weird parameter that didn't look right.
Nov 09 18:20:38 (@Aaron[])	Changed it in the SetWindowPos function.
Nov 09 18:20:48 (@Aaron[])	Now it's behaving funny.
Nov 09 18:20:50 (coKe)	lol
Nov 09 18:20:52 (@Aaron[])	But it's better than not at all.
Nov 09 18:20:53 (@Aaron[])	IMO
Nov 09 18:22:15 *	Aaron[] has quit (Read error: Connection reset by peer)
Nov 09 18:23:18 (GR|phish)	lol he crashed irc again..
Nov 09 18:24:22 (coKe)	that would be the funny behavior
Nov 09 18:25:00 (GR|phish)	well it doesn't help me  figure this stuff out
Nov 09 18:25:07 (GR|phish)	i get `_beginThread' was not declared in this scope 
Nov 09 18:25:17 (GR|phish)	i got -mthread defined in the compiler options
Nov 09 18:25:41 (GR|phish)	and my code it [ uintptr_t threadid = _beginThread( Game, 0, 0 ); ]
Nov 09 18:25:44 *	Aaron[] (~aaronl_d@d206-75-28-126.abhsia.telus.net) has joined #C++
Nov 09 18:25:45 *	ChanServ gives channel operator status to Aaron[]
Nov 09 18:25:51 (@Aaron[])	Man I get some wicked crashes with this thing.
Nov 09 18:25:53 (@Aaron[])	lmao
Nov 09 18:25:54 (GR|phish)	Game being an actual fuction called with __cdecl
Nov 09 18:25:54 (@Aaron[])	coKe!
Nov 09 18:26:05 (@Aaron[])	Uh what phish?
Nov 09 18:26:12 (GR|phish)	<GR|phish> i get `_beginThread' was not declared in this scope 
Nov 09 18:26:13 (GR|phish)	<GR|phish> i got -mthread defined in the compiler options
Nov 09 18:26:13 (GR|phish)	<GR|phish> and my code it [ uintptr_t threadid = _beginThread( Game, 0, 0 ); ]
Nov 09 18:26:30 (@Aaron[])	Why is your code in []?
Nov 09 18:26:40 (GR|phish)	just to make it clearer
Nov 09 18:26:47 (@Aaron[])	Does that even work?
Nov 09 18:26:50 (@Aaron[])	I've never tried it.
Nov 09 18:26:59 (Andrew-)	i think he just means for irc :p
Nov 09 18:27:05 (GR|phish)	lol, andrew has it.
Nov 09 18:27:08 (@Aaron[])	Oh ok.
Nov 09 18:27:09 (@Aaron[])	lol.
Nov 09 18:27:16 (@Aaron[])	Does it work then GR|phish?
Nov 09 18:27:21 (GR|phish)	neg
Nov 09 18:27:25 (@Aaron[])	What's wrong now?
Nov 09 18:27:35 (GR|phish)	DEV keeps saying `_beginThread' was not declared in this scope 
Nov 09 18:27:53 (@Aaron[])	Did you #include <process.h>
Nov 09 18:27:54 (@Aaron[])	?
Nov 09 18:28:01 (@Aaron[])	Might crash here hold on.
Nov 09 18:28:05 (GR|phish)	yes i did.
Nov 09 18:28:05 *	Aaron[] has quit (Read error: Connection reset by peer)
Nov 09 18:28:09 (Andrew-)	ha
Nov 09 18:28:15 *	Aaron[] (~aaronl_d@d206-75-28-126.abhsia.telus.net) has joined #C++
Nov 09 18:28:16 *	ChanServ gives channel operator status to Aaron[]
Nov 09 18:28:31 (GR|phish)	i did include process.h
Nov 09 18:28:55 (@Aaron[])	Hm.
Nov 09 18:29:00 (@Aaron[])	I'm unsure why it would be undefined then...
Nov 09 18:29:33 (GR|phish)	well
Nov 09 18:29:38 (GR|phish)	i tried _beginthread
Nov 09 18:29:44 (GR|phish)	with the capitol T
Nov 09 18:29:46 (GR|phish)	and i got
Nov 09 18:29:50 (GR|phish)	40 C:\dev\main.cpp invalid conversion from `void (*)()' to `void (*)(void*)' 
Nov 09 18:29:53 (@Aaron[])	Oh.
Nov 09 18:29:55 (GR|phish)	40 C:\dev\main.cpp   initializing argument 1 of `long unsigned int _beginthread(void (*)(void*), unsigned int, void*)' 
Nov 09 18:29:56 (@Aaron[])	Yeah it has no capital t.
Nov 09 18:29:57 (@Aaron[])	Kekeke.
Nov 09 18:30:05 (@Aaron[])	:$
Nov 09 18:30:22 (@Aaron[])	There's something wrong with your function.
Nov 09 18:30:55 (@Aaron[])	Show me your stuff again please.
Nov 09 18:31:05 (@Aaron[])	I may crash here hold on.
Nov 09 18:31:16 (@Aaron[])	FUCKING RIGHT
Nov 09 18:31:18 (@Aaron[])	coKe: I GOT IT
Nov 09 18:31:21 (GR|phish)	kk, do you mind if i paste 7lines of code?
Nov 09 18:31:22 (@Aaron[])	IT'S TERRIBLE BUT I GOT IT.
Nov 09 18:31:25 (GR|phish)	lol
Nov 09 18:31:26 (@Aaron[])	No I don't give a crap.
Nov 09 18:31:28 (GR|phish)	void __cdecl Game(void)
Nov 09 18:31:29 (GR|phish)	{
Nov 09 18:31:29 (GR|phish)	     cout << "Welcome to the Game" << endl;
Nov 09 18:31:29 (GR|phish)	     cout << "Please enter your name: " << endl;
Nov 09 18:31:29 (GR|phish)	     cin >> FakeName;
Nov 09 18:31:30 (GR|phish)	     cout << "Alright " << FakeName << " Please wait as the game loads" << endl;
Nov 09 18:31:32 (GR|phish)	}
Nov 09 18:31:48 (@Aaron[])	void __cdecl Game(void * params)
Nov 09 18:31:52 (@Aaron[])	That's what it should be.
Nov 09 18:32:00 (@Aaron[])	You have to have the void * in there.
Nov 09 18:32:18 (@Aaron[])	Because it's actually how the function required by _beginthread is expected you know?
Nov 09 18:32:29 (@Aaron[])	I know it's hard for you to know what I'm talking about because you don't understand function pointers.
Nov 09 18:32:31 (@Aaron[])	But that's how it is.
Nov 09 18:32:32 (@Aaron[])	-_-
Nov 09 18:32:47 (GR|phish)	keke
Nov 09 18:32:55 (GR|phish)	tew few freaking functions..
Nov 09 18:32:59 (GR|phish)	i mean
Nov 09 18:33:01 (GR|phish)	uhm...
Nov 09 18:33:10 (GR|phish)	to few arguments!
Nov 09 18:33:15 (GR|phish)	that's what i mean
Nov 09 18:33:17 (@Aaron[])	!?
Nov 09 18:33:18 (Andrew-)	;\
Nov 09 18:33:25 (GR|phish)	void __cdecl Game(void * params)
Nov 09 18:33:37 (@Aaron[])	Yes.
Nov 09 18:34:02 (Andrew-)	works for me in dev-cpp ;o
Nov 09 18:34:48 (@Aaron[])	Does GDI support double buffering?
Nov 09 18:34:54 (@Aaron[])	If so where the hell do I find it. This thing flickers like a whore.
Nov 09 18:36:11 (GR|phish)	oh my gawd
Nov 09 18:36:15 (GR|phish)	i gotzd it.
Nov 09 18:36:53 (GR|phish)	problem was i wasn't putting [ uintptr_t threadid = _beginthread( Game, 0, 0 ); ] in the main function
Nov 09 18:37:02 (Andrew-)	=/
Nov 09 18:37:12 (Andrew-)	where did you have it?
Nov 09 18:37:18 (GR|phish)	before int main()
Nov 09 18:37:30 (GR|phish)	and i had Game(); being called inside main();
Nov 09 18:37:40 (@Aaron[])	Yea don't do that.
Nov 09 18:37:48 (GR|phish)	you never told me >_>
Nov 09 18:37:59 (GR|phish)	but none the less
Nov 09 18:38:02 (GR|phish)	i gotta thank you.
Nov 09 18:38:05 (@Aaron[])	:D
Nov 09 18:38:46 (GR|phish)	but
Nov 09 18:39:02 (GR|phish)	in [ _beginthread( Game, 0, 0 ); ]
Nov 09 18:39:12 (GR|phish)	what are the 0, 0's for?
Nov 09 18:39:17 *	Knio1 (~Knio@66.222.174.231) has joined #C++
Nov 09 18:39:17 *	ChanServ gives channel operator status to Knio1
Nov 09 18:39:41 (@Aaron[])	The first 0 designates stack size.
Nov 09 18:39:54 (@Aaron[])	If you know what a stack for a program or a thread is you'll understand what I mean.
Nov 09 18:39:57 (@Aaron[])	If not, don't worry about it.
Nov 09 18:40:00 (GR|phish)	ok
Nov 09 18:40:05 (@Aaron[])	By passing 0 you're telling Windows to give you default stack size for a thread.
Nov 09 18:40:11 (GR|phish)	i can't wait till my books come here.
Nov 09 18:40:15 (GR|phish)	it will be much easier.
Nov 09 18:40:27 (@Aaron[])	The last 0 is parameters. You know that parameter in your Game function? void * params?
Nov 09 18:40:42 (@Aaron[])	If you wanted to, you could say pass a string, or a struct into that last 0
Nov 09 18:40:44 (@Aaron[])	Like this:
Nov 09 18:40:57 (@Aaron[])	_beginthread( Game, 0, (void*)&myStruct );
Nov 09 18:41:02 (@Aaron[])	And then in your game function you could do this:
Nov 09 18:41:09 (@Aaron[])	void __cdecl Game(void * params) {
Nov 09 18:41:10 *	Knio has quit (Ping timeout)
Nov 09 18:41:35 (@Aaron[])	  MYSTRUCTTYPE * myStruct = (myStruct*)params;
Nov 09 18:41:39 (@Aaron[])	And voila.
Nov 09 18:41:49 (GR|phish)	some jibberish :P
Nov 09 18:41:51 (@Aaron[])	You've pushed your data through from the main program into your threaded application.
Nov 09 18:41:58 (GR|phish)	oh
Nov 09 18:41:59 (GR|phish)	i see
Nov 09 18:42:22 (GR|phish)	so if i had some varible in the main function
Nov 09 18:42:33 (GR|phish)	i could use it in the threaded function
Nov 09 18:42:55 (@Aaron[])	Yes.
Nov 09 18:43:02 (GR|phish)	neat
Nov 09 18:43:07 (@Aaron[])	You could use it by assigning global variables, but they aren't guaranteed to be thread-safe.
Nov 09 18:43:10 (@Aaron[])	So it's not a good idea.
Nov 09 18:43:21 (@Aaron[])	That's why they give you the option to pass parameters or "params" ;)
Nov 09 18:43:25 (GR|phish)	:o
Nov 09 18:43:31 (GR|phish)	how can it not be thread-safe?
Nov 09 18:43:37 (GR|phish)	will it cause a memory leak?
Nov 09 18:43:42 (@Aaron[])	Well you see GR|phish... threads do funny things in programs.
Nov 09 18:43:47 (@Aaron[])	Suppose we have a variable that's shared.
Nov 09 18:43:57 (@Aaron[])	Let's for sake of nonsense call it: int race;
Nov 09 18:44:02 (GR|phish)	k
Nov 09 18:44:09 (@Aaron[])	Now, say the main thread wants to assign race to equal 5.
Nov 09 18:44:21 (@Aaron[])	But in the same processing time slice, the second thread wants to assign race to equal to 6.
Nov 09 18:44:38 (@Aaron[])	When both threads are done assigning. What number is it actually?
Nov 09 18:44:45 (GR|phish)	5.5?
Nov 09 18:45:00 (@Aaron[])	Or... what happens if thread A changes it, while thread B is doing a critical read on it?
Nov 09 18:45:08 (GR|phish)	hmm
Nov 09 18:45:09 (@Aaron[])	Where more than a few functions use this "race".
Nov 09 18:45:19 (@Aaron[])	Then all of a sudden, mid-calculation... your variable changes.
Nov 09 18:45:28 (@Aaron[])	That is what not being thread-safe means.
Nov 09 18:45:37 (GR|phish)	oh ok
Nov 09 18:45:59 (GR|phish)	and this way "copies" the varible into the new thread
Nov 09 18:46:12 (@Aaron[])	Yes exactly.
Nov 09 18:46:16 (GR|phish)	oh ok