0

I am creating a new thread in c using _beginthreadex. I have written the following code. same code written in two versions, first version is working properly but second one is 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 is declared in the same file new thread is running fine...
//but if this routine is moved to main.c and passed as parameter to func new thread is 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 the 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 the second thread.
    hThread = (HANDLE)_beginthreadex( NULL, 0, &SecondThreadFunc, NULL, 0, NULL );

    // Wait until second thread terminates.
    WaitForSingleObject( hThread, INFINITE );
}

I am getting access violation inside _beginthreadex while calling SecondThreadFunc. Can some help me out. Thanks in advance.

1
  • printf() etc stdio functions are not thread safe. Commented Jan 26, 2011 at 13:50

1 Answer 1

1

In the not working code you should have (pay attention to the &s):

hThread = (HANDLE)_beginthreadex( NULL, 0, SecondThreadFunc, NULL, 0, NULL );
Sign up to request clarification or add additional context in comments.

3 Comments

both are function calls but why the above correction is working... in c function pointer need not use & when passing to a function. Is that correct?
@Vineel Kumar Reddy: The change to the func() call is unnecessary, for the reason you state. The second change, however, is necessary - the SecondThreadFunc in func() is a function pointer value, so when you take its address you get the address of a function pointer, not the address of a function. In this case, SecondThreadFunc and *SecondThreadFunc are the equivalent expressions.
U r awesome though i have been programming in c from 6 year this is really a very subtle example. Thanks a lot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.