Passing an integer to a thread?

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!–
~ Leo, M@$73r L3170, Sulfurik, Kixdemp, L-28C… Whatever you wanna call me.

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.

– JoshOn 8/24/06, L-28C wrote:


~ 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

#3 worked! Thanks! :wink:

Joshua Oreman wrote:> On 8/24/06, L-28C <@Leo_Cabrera> 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


~ Leo, M@$73r L3170, Sulfurik, Kixdemp, L-28C… Whatever you wanna call me.

Hello L-28C,

Thursday, August 24, 2006, 11:03:52 PM, you 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!

Unless you’re ever going to compile the code on a system where
pointers are smaller than ints, just casting it will work fine.

SDL_CreateThread(threadfunc, (void *) myint);

and then cast it back in threadfunc.

Yes, it’ll generate a warning, but it’s safe enough to do. Similarly
on 64-bit, pointers are LARGER than int’s so that’s OK too…–
Best regards,
Peter mailto:@Peter_Mulholland