-
June 12th, 2011, 06:47 AM
#1
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 =%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
-
June 12th, 2011, 07:00 AM
#2
Re: passing Struct as arg to thread
Originally Posted by this4me
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
-
June 12th, 2011, 07:30 AM
#3
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'
-
June 12th, 2011, 07:48 AM
#4
Re: passing Struct as arg to thread
-
June 12th, 2011, 07:52 AM
#5
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 =%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.
-
June 12th, 2011, 08:58 AM
#6
Re: passing Struct as arg to thread
Originally Posted by this4me
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
-
June 12th, 2011, 05:24 PM
#7
Re: passing Struct as arg to thread
Originally Posted by this4me
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|