CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Oct 2009
    Posts
    10

    Question passing Struct as arg to thread

    Code:
    #include<stdio.h>
    #include<windows.h>
    
    struct student
    {
           char name[20];
           int num;
    };
    
    unsigned __stdcall thread2(void *ap) // not working
    {
    struct student *p= (struct  student *) ap;
    printf("name =&#37;s \n num =%d",p->name,p->num);
    printf("\n in thread");
    return 0;
    }
    
    unsigned __stdcall thread(struct student *p)// not working
    {
    printf("name =%s \n num =%d",p->name,p->num); 
    printf("\n in thread");
    return 0;
    }
    
    int main()
    {
    struct student stu;
    stu.num=12345;
    strcpy(stu.name,"li li");
    DWORD threadID=0;
    CreateThread(NULL, 0,(unsigned long (__stdcall *)(void *))thread ,&stu,0,&threadID);
    return 0;
    }
    i want to pass the Struct stu to the Thread thread but it wont work , i tried another form of argument using (void *) and then casting to (struct student *) in thread2 it does not work

    any help would be appreciated
    greetz
    Last edited by this4me; June 12th, 2011 at 06:58 AM. Reason: errors

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: passing Struct as arg to thread

    Quote Originally Posted by this4me View Post
    i want to pass the Struct stu to the Thread thread but it wont work , i tried another form of argument using (void *) and then casting to (struct student *) in thread2 does it does not work
    What do you mean with "doesn't work"? Do you get a compiler error, a crash?

    Note that you should use _beginthreadex instead of CreateThread.
    From MSDN
    A thread in an executable that calls the C run-time library (CRT) should use the _beginthreadex and _endthreadex functions for thread management rather than CreateThread and ExitThread; this requires the use of the multi-threaded version of the CRT. If a thread created using CreateThread calls the CRT, the CRT may terminate the process in low-memory conditions.
    Also, don't cast the argument you are passing. If you are using C++, don't use C-style casts at all. If you use C, use them sparsely, because they will prevent any type checks performed by the compiler.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  3. #3
    Join Date
    Oct 2009
    Posts
    10

    Re: passing Struct as arg to thread

    doesn't work = no compilation error but it does not display anything it just does not execute the thread
    i tried to use _beginthreadex but the compiler says '_beginthreadex' : undeclared identifier
    i try to fix it by project->settings->c++->code generation ->mult-ithreaded
    but its still 'undeclared identifier'

  4. #4
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: passing Struct as arg to thread

    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  5. #5
    Join Date
    Oct 2009
    Posts
    10

    Re: passing Struct as arg to thread

    in fact i already did :P
    working code =
    Code:
    #include <windows.h>
    #include <stdio.h>
    #include <process.h>
    struct student
    {
           char name[20];
           int num;
    };
    unsigned __stdcall SecondThreadFunc( void* pArguments )
    {
    	struct student *p= (struct  student *) pArguments;
    	printf("name =&#37;s \n num =%d",p->name,p->num);
        _endthreadex( 0 );
        return 0;
    } 
    
    int main()
    { 
        HANDLE hThread;
        unsigned threadID;
    	struct student stu;
    	stu.num=12345;
    	strcpy(stu.name,"li li");
    
        printf( "Creating second thread...\n" );
    
        hThread = (HANDLE)_beginthreadex( NULL, 0, &SecondThreadFunc, &stu, 0, &threadID );
    
        WaitForSingleObject( hThread, INFINITE );
    
        CloseHandle( hThread );
    	return 0;
    }
    i figured out what was my error
    Code:
    (unsigned long (__stdcall *)(void *))thread
    should be
    Code:
    (unsigned long (__stdcall *)(void *))&thread
    i forgot to put & before thread
    also for D_Drmmr CreateThread works fine
    thanks all
    Last edited by this4me; June 12th, 2011 at 07:57 AM.

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: passing Struct as arg to thread

    Quote Originally Posted by this4me View Post
    i...
    also for D_Drmmr CreateThread works fine
    Well, it may and may not "work fine".
    But to be 100% sure it will always "work fine" you should use _beginthreadex to start a thread and simply return from the tread procedure to exit thread.

    PS: In MFC using AfxBeginThread would be the best choice.
    Victor Nijegorodov

  7. #7
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: passing Struct as arg to thread

    Quote Originally Posted by this4me View Post
    i figured out what was my error
    Code:
    (unsigned long (__stdcall *)(void *))thread
    should be
    Code:
    (unsigned long (__stdcall *)(void *))&thread
    That's why I suggested not to use a cast. Without the cast you would have gotten a compiler error. When you cast, you are essentially trying to take over the compiler's job.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured