私は_beginthreadexを使ってcで新しいスレッドを作成しています。私は次のコードを書いています。 同じコードが2つのバージョンで書かれていますが、最初のバージョンは正常に動作していますが、2番目のバージョンは正常に動作していません。Cスレッドが正しく動作しない - シンプルコード
作業コード(下)
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);
}
SecondThreadFuncを呼び出している間、私は_beginthreadexの内側に、アクセス違反を取得しています。助けてもらえますか?前もって感謝します。あなたが持つべき機能していないコード(&のに注意を払う)で
のprintf()などのstdio関数はスレッドセーフではありません。 – Lundin