Using SDL Threads and Templated Parameters

Hello,
I need to make an SDL Thread that performs a matrix multiplication. The matrix class I wrote is templated so I can store any type of data in it. From what I understand of SDL Threads, you have to use a struct to pass parameters to the thread, so I'm doing this:
1
2
3
4
struct parameters{
  Matrix<T> m1;
  Matrix<T> m2;
};


And create the thread like this:
1
2
3
4
parameters<T> thrd1;
thrd1.m1 = matrix1;
thrd1.m2 = matrix2;
thread = SDL_CreateThread( TestThread, &thrd1 );


With the threaded function like this:
1
2
3
4
int TestThread( void *data )
{
  return 0;
}


This compiles, but the problem is that if I want to reinterpret and create a matrix in the thread function, it needs to be templated, so I do this:

1
2
3
4
5
6
template <class T>
int TestThread( void *data )
{
  Matrix<T> m1 = (reinterpret_cast<parameters<T>*>(data));
  return 0;
}

But that gives me this error:
error C2664: 'SDL_CreateThread' : cannot convert parameter 1 from 'int (__cdecl *)(void *)' to 'int (__cdecl *)(void *)'

And I have no idea why. How can you not convert a parameter of the same type you're trying to convert to? The problem is with the "template <class T>", because if I take that out and give parameters a type like int rather than a template T, it compiles.

Can anyone please help?

SDL_CreateThread( TestThread, &thrd1 );

The compiler can't know which TestThread you want. You have to specify a specific version of it.
Sorry, what do you mean which one I want? It's just a function, so what is there to specify? How would I do that? Thanks for the response.
Oh, I think I see what you mean, I have to do:

thread = SDL_CreateThread( TestThread<T>, &thrd1 );

?
Last edited on
It is a template function, thus you need to specify which version, e.g.:

SDL_CreateThread( &TestThread<parameters>, &thrd1);
Last edited on
Oh, I see. Awesome, that works, thanks a lot. Now, another question is how would I return the resulting matrix from the multiplication since the thread function has to be of type int as far as I can tell.
As for returning things from threads, you can't really. You either have to use a global variable (with locks or whatever depending on if other threads use it), or you *might* be able to simply modify the void* that was passed to the function (not sure if that will work though).
Alright, it seems that I AM able to modify the void* by just adding that parameter to the struct:
1
2
3
4
5
struct parameters{
  Matrix<T> m1;
  Matrix<T> m2;
  int test;
};


And then this in the threaded function to modify it:
(reinterpret_cast<parameters<T>*>(data)->test) = 5;
Topic archived. No new replies allowed.