C поток не ведет себя правильно - простой код - PullRequest
0 голосов
/ 26 января 2011

Я создаю новую тему в c, используя _beginthreadex.Я написал следующий код.один и тот же код, написанный в двух версиях, первая версия работает нормально, но вторая не работает.

рабочий код

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 );
}

Не рабочий код (ниже)

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 );
}

Я получаю нарушение прав доступа внутри _beginthreadex при вызове SecondThreadFunc.Могут ли некоторые помочь мне.Заранее спасибо.

1 Ответ

1 голос
/ 26 января 2011

В неработающем коде вы должны иметь (обратите внимание на & s):

hThread = (HANDLE)_beginthreadex( NULL, 0, SecondThreadFunc, NULL, 0, NULL );
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...