Thread: Passing Parameters To SDL_CreateThread

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    139

    Passing Parameters To SDL_CreateThread

    Hello, I need to pass a parameter (string/filename) into SDL_CreateThread but for some reason I am not able to figure this out.


    Code:
     
    int processFile(string sFilePath)
    { 
        // process file here 
        return 0; 
    }  
    
    int main() 
    {     
        string sFilePath =  "/home/myuser/test.txt";     
        thread = SDL_CreateThread(processFile, "processFile", &sFilePath);  
    }


    The error I get is

    Code:
     
    main.cpp|124|error: invalid conversion from ‘int (*)(std::string) {aka int (*)(std::basic_string)}’ to ‘int (*)(void*)’ [-fpermissive]|

    How can I pass the filename into the thread?

    Thanks in advance!
    Last edited by EverydayDiesel; 10-19-2015 at 09:50 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The problem lies with your callback: the callback is expected to be a function that has a void* parameter and returns an int, but you passed a pointer to a function that has a std::string parameter and returns an int.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2014
    Posts
    139
    thanks for the reply.

    So it needs to change to this?

    Code:
    int processFile(string sFilePath)
    { 
        // process file here 
        return 0; 
    }  
    
    int main() 
    {     
        int threadReturnValue;
        string sFilePath =  "/home/myuser/test.txt";     
        thread = SDL_CreateThread(processFile, sFilePath, threadReturnValue);  
    }
    
    




    I do appologize for the noob question but it still gives me this

    Code:
    main.cpp|124|error: invalid conversion from ‘int (*)(std::string) {aka int (*)(std::basic_string<char>)}’ to ‘int (*)(void*)’ [-fpermissive]|



    I thought that I passed parameters through data but I am unsure how to do so. I also am not sure what 'name' is used for in that fuction either.
    Code:
    SDL_Thread* SDL_CreateThread(SDL_ThreadFunction fn,
                                 const char*        name,
                                 void*              data)
    Last edited by EverydayDiesel; 10-19-2015 at 10:16 AM.

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    SDL_CreateThread - SDL Wiki'

    The first parameter needs to be of type "SDL_ThreadFunction". That means you need a signature of "int fn(void* data)". See remarks.

    The third parameter is a void*. Any pointer can safely be stored as a void*, and then restored back to its original pointer type. However, I would not use a std::string object itself. Just pass in a "const char*" version of the string, then the thread function can cast the void* back to const char*. It is up to you to ensure the lifetime of the memory that the void* points to is valid when the thread function accesses it. In your current code, sFilePath will destruct once main returns (which is probably before the thread would run).

    Also notice in the manual that you are required to call either SDL_WaitThread() or SDL_DetachThread() after a successful CreateThread().
    SDL_WaitThread - SDL Wiki'

    gg

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    No, more like:
    Code:
    int processFile(void* data)
    {
        string& sFilePath = *static_cast<std::string*>(data);
        // process file here
        return 0;
    }
    
    int main()
    {
        string sFilePath =  "/home/myuser/test.txt";
        thread = SDL_CreateThread(processFile, "processFile", &sFilePath);
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jan 2014
    Posts
    139
    Thanks! I think now there is some kind of string problem because it doesnt like the string being passed into data

    Code:
    main.cpp|126|error: invalid conversion from ‘const void*’ to ‘void*’ [-fpermissive]|
    I tried
    Code:
        const string sFilePath =  "/home/myuser/test.txt"; 
        thread = SDL_CreateThread(processFile, "processFile", &sFilePath);  //this is the line it errors on
    but it did not work
    Last edited by EverydayDiesel; 10-19-2015 at 10:33 AM.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Remove the const from the declaration of sFilePath. That said, Codeplug made a point about object lifetime that you should consider.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Dec 2013
    Posts
    241
    you may want to switch to standard C++11 threads .
    with your original function signature, the code would easily turned into

    Code:
    thread = std::thread(processFile,sFilePath);

  9. #9
    Registered User
    Join Date
    Jan 2014
    Posts
    139
    Quote Originally Posted by laserlight View Post
    Remove the const from the declaration of sFilePath. That said, Codeplug made a point about object lifetime that you should consider.
    I added that const to get it to compile but the error happens with or without that line.


    The error happens on the 2nd line listed below.
    Code:
    string sFilePath =  "/home/myuser/test.txt";
    thread = SDL_CreateThread(processFile, "processFile", &sFilePath);

  10. #10
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> The error happens on the 2nd line.
    You are not making it easy for us to help you. Post what you are compiling and the errors your received.

    gg

  11. #11
    Registered User
    Join Date
    Jan 2014
    Posts
    139
    Sorry, its the same error over and over again

    Error:
    Code:
    main.cpp|126|error: invalid conversion from ‘const void*’ to ‘void*’ [-fpermissive]|

    Code:
    Code:
    int processFile(void* data)
    {
        string& sFilePath = *static_cast<std::string*>(data);
    }
    
    int main()
    {
        string sFilePath =  "/home/myuser/test.txt";
        thread = SDL_CreateThread(processFile, "processFile", &sFilePath);
    }
    Last edited by EverydayDiesel; 10-19-2015 at 02:07 PM.

  12. #12

  13. #13
    Registered User
    Join Date
    Jan 2014
    Posts
    139
    line 126 is this line
    Code:
        thread = SDL_CreateThread(processFile, "processFile", &sFilePath);

  14. #14
    Registered User
    Join Date
    Jan 2014
    Posts
    139
    I took a seperate project and just pasted this example I am trying to get working. This is 100% of the code.

    Code:
    #include <iostream>
    #include "SDL/SDL_thread.h"
    #include "SDL/SDL_timer.h"
    
    using namespace std;
    
    int processFile(void* data)
    {
        string& sFilePath = *static_cast<std::string*>(data);
        // process file here
        return 0;
    }
    
    int main()
    {
        SDL_Thread *thread;
        string sFilePath =  "/home/myuser/test.txt";
        thread = SDL_CreateThread(processFile, "processFile", &sFilePath);
        return 0;
    }
    Code:
    ThreadingTest/main.cpp|18|error: invalid conversion from ‘const void*’ to ‘void*’ [-fpermissive]|

    Line 18 referenced above is
    Code:
    thread = SDL_CreateThread(processFile, "processFile", &sFilePath);

  15. #15
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Sample code from the manual - replaced printf with cout - added std::string parameter:
    Code:
    #include "SDL_thread.h"
    #include "SDL_timer.h"
    
    #include <iostream>
    #include <string>
    using namespace std;
    
    // Very simple thread - counts 0 to 9 delaying 50ms between increments
    static int TestThread(void *ptr)
    {
        const string& threadMsg = *static_cast<string*>(ptr);
        int cnt;
        
        cout << threadMsg << endl;
    
        for (cnt = 0; cnt < 10; ++cnt) {
            cout << "Thread counter: " << cnt << endl;
            SDL_Delay(50);
        }
    
        // Return the final value to the SDL_WaitThread function above
        return cnt;
    }
    
    int main(int argc, char *argv[])
    {
        SDL_Thread *thread;
        int         threadReturnValue;
    
        cout << "Simple SDL_CreateThread test:" << endl;
    
        // Simply create a thread
        string threadMsg = "Hello World";
        thread = SDL_CreateThread(TestThread, "TestThread", &threadMsg);
    
        if (NULL == thread) {
            cout << "SDL_CreateThread failed: " << SDL_GetError() << endl;
        } else {
            // Wait for the thread to complete. The thread functions return code will
            //       be placed in the "threadReturnValue" variable when it completes.
            //
            SDL_WaitThread(thread, &threadReturnValue);
            cout << "Thread returned value: " << threadReturnValue << endl;
        }
    
        return 0;
    }
    Code:
    C:\MinGW-Projects\gg>g++ sdl_main.cpp -I c:\MinGW\include\SDL2 -lSDL2main -lSDL2_mixer -lSDL2 -lwinmm -luser32 -lgdi32 -lole32 -limm32 -loleaut32 -lversion -Wall
    
    C:\MinGW-Projects\gg>a
    Simple SDL_CreateThread test:
    Hello World
    Thread counter: 0
    Thread counter: 1
    Thread counter: 2
    Thread counter: 3
    Thread counter: 4
    Thread counter: 5
    Thread counter: 6
    Thread counter: 7
    Thread counter: 8
    Thread counter: 9
    Thread returned value: 10
    "Works for me." TM

    >> This is 100% of the code.
    Very good. It seems strange that it compiles without including <string>.

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing on parameters
    By Allen in forum C++ Programming
    Replies: 8
    Last Post: 01-10-2012, 12:55 AM
  2. Passing along function parameters
    By FlyingIsFun1217 in forum C++ Programming
    Replies: 16
    Last Post: 12-31-2007, 04:43 AM
  3. Passing Parameters
    By fry in forum C++ Programming
    Replies: 2
    Last Post: 10-04-2002, 03:06 AM
  4. Passing parameters
    By pdstatha in forum C++ Programming
    Replies: 1
    Last Post: 06-30-2002, 10:07 AM
  5. Passing parameters to other programs...is it possible?
    By face_master in forum Windows Programming
    Replies: 3
    Last Post: 01-28-2002, 07:48 AM