回 帖 发 新 帖 刷新版面

主题:[讨论]很简单的一个问题

想创建一个线程,然后让其运行,怎么不能实现?

DWORD WINAPI ClientThread(LPVOID lpParam)  
{
 while(1)
    printf("This is in Thread1!\n");
 return NULL;
}



int main(void)
{
    
HANDLE   hThread;
DWORD    dwThreadId;
SOCKET   sClient;
char *str="Thread";
hThread = CreateThread(NULL, 0, ClientThread1, (LPVOID)str, 0, &dwThreadId);
if(hThread == NULL)
       printf("CreateThread() failed: \n") ;
CloseHandle(hThread);
return 0;
}

回复列表 (共8个回复)

沙发


看不明白

板凳

工程设置里,要把Generation Code那里设置为:

Mutil 属性

3 楼

控制台下的多线程,还没试过

4 楼

主线程(main)加上WaitForSingleObject等待子线程结束,main返回后进程就结束了

5 楼

你的主函数直接退出了,所以开得线程还没有运行就结束了

6 楼

你代码的17行里这个参数ClientThread1是什么?

7 楼

修改一下
int main(void)
{
    
HANDLE   hThread;
DWORD    dwThreadId;
SOCKET   sClient;
char *str="Thread";
hThread = CreateThread(NULL, 0, ClientThread, (LPVOID)str, 0, &dwThreadId);
if(hThread == NULL)
       printf("CreateThread() failed: \n") ;
::Sleep(500);//add
CloseHandle(hThread);
return 0;
}

8 楼

hThread = CreateThread(NULL, 0, ClientThread1, (LPVOID)str, 0, &dwThreadId);//线程入口ClientThread

我来回复

您尚未登录,请登录后再回复。点此登录或注册