Thread: Threading A function

  1. #1
    Registered User (TNT)'s Avatar
    Join Date
    Aug 2001
    Location
    UK
    Posts
    339

    Threading A function

    Hey,

    I have made a function that swaps 2 files around on an ftp server, of course it slows the prog down and i need to run the function in a seperate thread.

    i call the function like this:

    SwapFiles("ftp.mysite.com", "user1", "pass", 21, "htdocs/ss", "sstatus.asp", "sonline.asp");

    Inside swapfiles a connection is made to the server and then the files are swapped. How can i do all of this in a seperate thread?

    Thanks
    TNT
    TNT
    You Can Stop Me, But You Cant Stop Us All

  2. #2
    Registered User johnnie2's Avatar
    Join Date
    Aug 2001
    Posts
    186
    You could change SwapFiles()'s declaration to DWORD WINAPI SwapFiles(LPVOID) and pass in a struct with members corresponding to the function's current parameters:

    Code:
    typedef struct {
       char site[64];
       char username[32];
       char password[32];
       int port;
       char path[128];
       char fileFrom[32];
       char fileTo[32];
    } swapInfo;
    
    
    swapInfo info;
    // fill in members as needed, etc.
    CreateThread(NULL, 0, SwapFiles, &info, 0);
    "Optimal decisions, once made, do not need to be changed." - Robert Sedgewick, Algorithms in C

  3. #3
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    if your using windows look into beginthread() or beginthreadex() you will need to pass the params as a struct but it will allow multithreading!
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  4. #4
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    Yes, be carefull with that CreateThread() API if you are using a lot of the runtime library routines - they can cause memory leaks when used with CreateThread(). Use beginthread() or the ex version.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  5. #5
    Registered User (TNT)'s Avatar
    Join Date
    Aug 2001
    Location
    UK
    Posts
    339
    Hey,

    Thanks alot for that, i think im going to use the createthread() method, but i just have a question.

    How do i access the structure in SwapFiles()?

    DWORD WINAPI SwapFiles(LPVOID);

    and im passing it the structure, how do i then access the struct inside the function.

    Also i need the function to return an int?

    Thanks
    TNT
    Last edited by (TNT); 06-06-2002 at 07:11 AM.
    TNT
    You Can Stop Me, But You Cant Stop Us All

  6. #6
    Registered User johnnie2's Avatar
    Join Date
    Aug 2001
    Posts
    186
    You'd need to cast the parameter passed to SwapFiles() to get access to the struct:

    Code:
    DWORD WINAPI SwapFiles(LPVOID lParam) {
       swapInfo *thisSwap = (swapInfo *)lParam;
    
       connectToServer(thisSwap->site);
       // and so on
    
       return 0;
    }
    And yes, it's expected to return some kind of value at the end.

    Using CreateThread() while calling library routines may result in a memory leak of 70-80 bytes when ExitThread() is called, so consider _beginthread() under those circumstances.
    "Optimal decisions, once made, do not need to be changed." - Robert Sedgewick, Algorithms in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem threading a member function
    By Syneris in forum C++ Programming
    Replies: 10
    Last Post: 12-31-2008, 07:26 PM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM