multithreading - C thread not behaving correctly - simple code -
i creating new thread in c using _beginthreadex. have written following code. same code written in 2 versions, first version working second 1 not working.
working code
main.c
#include <windows.h> #include <stdio.h> extern void func(unsigned (__stdcall *secondthreadfunc)( void* )); int main() { func(null); }
second.c
#include<windows.h> //when thread start routine declared in same file new thread running fine... //but if routine moved main.c , passed parameter func new thread not working unsigned __stdcall secondthreadfunc( void* parguments ) { printf( "in second thread...\n "); return 0; } void func(unsigned (__stdcall *secondthreadfunc)( void* )) { handle hthread; printf( "creating second thread...\n" ); // create second thread. hthread = (handle)_beginthreadex( null, 0, &secondthreadfunc, null, 0, null ); // wait until second thread terminates. waitforsingleobject( hthread, infinite ); }
not working code(below)
main.c
#include <windows.h> #include <stdio.h> #include <process.h> extern void func(unsigned (__stdcall *secondthreadfunc)( void* )); unsigned __stdcall secondthreadfunc( void* parguments ) { printf( "in second thread...\n "); return 0; } int main() { func(secondthreadfunc); }
second.c
#include<windows.h> void func(unsigned (__stdcall *secondthreadfunc)( void* )) { handle hthread; printf( "creating second thread...\n" ); // create second thread. hthread = (handle)_beginthreadex( null, 0, &secondthreadfunc, null, 0, null ); // wait until second thread terminates. waitforsingleobject( hthread, infinite ); }
i getting access violation inside _beginthreadex while calling secondthreadfunc. can me out. in advance.
in not working code should have (pay attention &s):
hthread = (handle)_beginthreadex( null, 0, secondthreadfunc, null, 0, null );
Comments
Post a Comment