Thread: _beginthread when function has more than one parameter

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    13

    Question _beginthread when function has more than one parameter

    I have been testing multithreading in windows. currently the function for the begin thread takes a single argument.

    I want the addem function to take more that one function but I am unsure of the proper sytax.

    currently it looks like this:
    (process.h etc has been include, this version works)

    int addem(int);
    int main() {
    _beginthread((void (*)(void *))addem, 0, (void *)5000);
    /* Do Other Stuff */
    }
    .... etc

    I want addem to take two values:

    int addem(int, char *);

    The syntax of the _beginthread is very confusing, how do I pass two parameters to the addem function in the _beginthread? The documentation just says that the final parameter is *arglist? What is the sytax of the arglist and for that matter are any of the (void(*)(void *))'s part of some kind of function declaration?



    BYron
    Last edited by blundstrom; 10-03-2001 at 08:58 PM.

  2. #2
    Unregistered
    Guest

    Smile

    u may change addem to:

    typedef struct{
    int Num;
    char* pBuf;
    }mystruct;

    void* addem(mystruct* );

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    13

    Thumbs down Struct No Worky!!!

    I tried your suggestion, but struct and struct pointers cannot be converted to (void *), a least the compiler doesn't want to try and if I leave out the (void *) cast it won't link because of the struct then is an unresolved external symbol.

    _beginthread looks like thsI

    _beginthread(__cdecl *(void *)function_name, int, *arglist)
    Last edited by blundstrom; 10-04-2001 at 11:50 AM.

  4. #4
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    this is how you do it

    Code:
    #include <stdio.h>
    #include <process.h>
    
    typedef struct ST{ 
        int num; 
        char* buf; 
    }thestruct; 
    
    void Addem(thestruct* );
    
    int main()
    {
        thestruct st;
        st.num = 6000;
        _beginthread((void(*)(void*))Addem,0,(void*)&st);
        getchar();    // freeze program so you can see output
    }
    
    void Addem(thestruct* s)
    {
        printf("%d\n",(thestruct*)(s)->num);
        _endthread();
    }
    Last edited by no-one; 10-04-2001 at 11:54 AM.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    13

    Post Kewl, Thanks!!! But, what about the fuction with multiple arguments!!!

    Thanks for the solution,

    but what about the if the function has multiple arguments.

    It is impossible?

  6. #6
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    >but what about the if the function has multiple arguments.
    It is impossible? <

    its possible, but finiky and unreliable, most of the time it won't even work.

    and passing int values never seem to work.

    somebody else here may have a solution but as far as i know it dosn't work well.

    mainly because your trying to pass multiple values to a single void pointer, how can a pointer point to multiple values?
    Last edited by no-one; 10-04-2001 at 12:33 PM.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. Replies: 13
    Last Post: 08-24-2006, 12:22 AM
  4. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM