[SDL] Passing an integer to a thread?
Joshua Oreman
oremanj at gmail.com
Thu Aug 24 15:09:02 PDT 2006
On 8/24/06, L-28C <leo28c at gmail.com> wrote:
> Hello everyone!
> OK, so I declared my thread function with one void* parameter, and I
> need to pass an int parameter... How do I convert void* to int? Thanks!
A few ways.
One: void *foo_as_ptr = (void *)(long)foo_as_int;
int foo_as_int = (long)foo_as_ptr;
Two: if it's a global or a static, just pass its address
(&foo_as_int), and in your thread function have something like
void foo_thread (void *argp) {
int arg = *(int *)argp;
/* ... */
}
Three: malloc() a bit of memory for it:
int *foop = malloc (sizeof(int));
*foop = foo_int;
/* create your thread with foop as an arg */
/* ... */
void foo_thread (void *argp) {
int arg = *(int *)argp;
free (argp);
/* ... */
}
Which way is up to you. The first is certainly the easiest; however,
it won't work on systems with smaller pointers than ints, and it'll
generate warnings on systems with different-sized pointers than longs.
Option 2 works if it's a global or a static. Option 3 works for
anything, but is certainly a bit cumbersome.
-- Josh
>
> --
> ~ Leo, M@$73r L3170, Sulfurik, Kixdemp, L-28C... Whatever you wanna call me.
>
>
> _______________________________________________
> SDL mailing list
> SDL at libsdl.org
> http://www.libsdl.org/mailman/listinfo/sdl
>
More information about the SDL
mailing list