线程函数为啥不停止了????

mirroatl156 2013-09-16 11:21:59


#include <time.h>
#include "afxmt.h"
CWinThread* m_pThread[2];
CCriticalSection g_cs;
using namespace std;
//#include "./basefile/DbAccess.h"
int loop = 10000;
int loop_array[10000] = {0};
//DB_access song_db_access;

bool database_conrast()
{
while(loop)
{
loop_array[loop - 1] = loop;
loop --;
}
return 0;
}

static UINT ThreadFun(LPVOID pParam)
{
g_cs.Lock();
int result =0;//= song_db_access.Connect(L"./Debug/20130902.mdb");
if(!result)
{
return 0;
}
g_cs.Unlock();
return 1;
}

static UINT ThreadFun2(LPVOID pParam)
{
g_cs.Lock();
int result2 =0;//= song_db_access.Connect(L"./Debug/20130902.mdb");
if(!result2)
{
return 0;
}
g_cs.Unlock();
return 1;
}

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;

// 初始化 MFC 并在失败时显示错误
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: 更改错误代码以符合您的需要
_tprintf(_T("致命错误: MFC 初始化失败\n"));
nRetCode = 1;
}
else
{
// TODO: 在此处为应用程序的行为编写代码。
m_pThread[0] = AfxBeginThread(ThreadFun,NULL,THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED);
m_pThread[1] = AfxBeginThread(ThreadFun2,NULL,THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED);

clock_t t1 = clock();

m_pThread[0]->ResumeThread();
m_pThread[1]->ResumeThread();
HANDLE thread_handle[2];
thread_handle[0] = m_pThread[0]->m_hThread;
thread_handle[1] = m_pThread[1]->m_hThread;

WaitForMultipleObjects(2,thread_handle,TRUE,INFINITE);
clock_t t2 = clock();
std::cout<<"time: "<<t2-t1<<std::endl;
}
return nRetCode;
}
...全文
67 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
mirroatl187 2013-09-16
  • 打赏
  • 举报
回复
static UINT ThreadFun(LPVOID pParam) { g_cs.Lock(); int result =song_db_access.Connect(L"./Debug/1.txt");//为什么返回null值 打不开。。。 g_cs.Unlock(); return 1; } static UINT ThreadFun2(LPVOID pParam) { g_cs.Lock(); int result2 =song_db_access.Connect(L"./Debug/1.txt"); g_cs.Unlock(); return 1; }
引用 1 楼 oyljerry 的回复:
ThreadFun 直接return,没有解锁吧
oyljerry 2013-09-16
  • 打赏
  • 举报
回复
ThreadFun 直接return,没有解锁吧
oyljerry 2013-09-16
  • 打赏
  • 举报
回复
./Debug/1.txt 是不是路径正确,同时是否可以被打开

15,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧