求助!c语言创建线程未运行问题
代码如下ULONG _stdcall ThreadFunc1( LPVOID lpParam ) {
AfxMessageBox(_T("ThreadFunc1"));
static int nCount; //产生事件的次数
while(g_keepRunning)
{
//只要事件列表中有一个事件有信号,就结束等待
ULONG RetForWait = 0xffffffff;
RetForWait = WaitForSingleObject(g_hEventSFIFO,INFINITE);
if( RetForWait == WAIT_OBJECT_0 )
{
CString tempStr;
CSFDJController1Dlg* curDlg;
curDlg = (CSFDJController1Dlg*)AfxGetApp()->m_pMainWnd;
//显示进入事件的次数
nCount++;
tempStr.Format(_T("事件次数: %d"), nCount);
// curDlg->SetDlgItemText( IDC_LBL_COUNT, tempStr );
long retCount; //,i;
//还是要判断一下才能读,因为此时真正的数据个数可能不到需要的数
tempStr.Format(_T("SFIFO中数据个数: %d"), ZT8348_GetSFifoDataCount(g_cardNO,1));
//curDlg->SetDlgItemText( IDC_LBL_DATA_COUNT, tempStr );
//ZT8348_GetSFifoDataCount得到驱动缓冲区(SFIFO)中当前有效数据的个数
if( ZT8348_GetSFifoDataCount(g_cardNO,1) >= g_wantCount )
{
//ZT8348_AISFifo定时启动AD或外触发启动AD时,从驱动缓冲区(SFIFO)中读一批数 g_wantCount希望得到的个数
retCount = ZT8348_AISFifo(g_cardNO, 0, g_resultArr, g_wantCount);//g_resultArr采集结果
//显示前8个数
//g_resultArr = 1000.0;
//每次求平均值个数为30000个,每次间隔100个值
ZT8348_AISFifoChAver(g_cardNO, 0,g_resultArr,30000,100);
//ZT8348_AISFifoChAver函数只支持单通道 ,目前状态是1通道值为0,使用2通道,求平均值的时候就需要乘以两倍
float gaiv = (g_resultArr*2)/(float)1000;//转换单位 将mv转换为v
float gaimv = (g_resultArr*2);
tempStr.Format(_T("%7.3f v"), gaimv);//总共七位 其中两位小数点
curDlg->SetDlgItemText(IDC_LBL_AD1+0, tempStr);
float kg = 38.18*gaiv;//36.28五个点位求出的平均值
tempStr.Format(_T("%7.2f KG"), kg);//总共七位 其中两位小数点
curDlg->SetDlgItemText(IDC_LBL_AD0+0, tempStr);
PYali=g_resultArr;
/*如果 压力值减去标准值是区间范围*/
float ca = PYali-chazhi;
if(ca<=2){
// AfxMessageBox(_T("获取稳定压力值的次数 "));
countaaa++;
}
if(countaaa>3){
// PYalir=PYali;
PYalia(kg);
}
chazhi=PYali;
}
}
}
return 0;
}
floatStartCaijiwd(){
cunya=0;
// AfxMessageBox(_T("开始采集无参数"));
//新建文件
if( (g_fp=(fopen("wending.dat","wb")))==NULL)
{
AfxMessageBox(_T("open file failed"));
return -1;
}
// AfxMessageBox("PYali");
//建立一个线程监视事件
DWORD dwThreadId;
HANDLE hThread;
g_keepRunning = 1;
hThread = CreateThread(NULL,0,ThreadFunc1,NULL,0,&dwThreadId);
if (hThread == NULL)
{
AfxMessageBox(_T("CreateThread failed\n"));
}
else
{
CloseHandle( hThread );
}
//启动AD
ZT8348_DisableAD(g_cardNO); //停止定时AD
ZT8348_ClearHFifo(g_cardNO, 1); //清空硬件FIFO(HFIFO)
ZT8348_ClearSFifo(g_cardNO, 1); //清空软件FIFO(SFIFO)
if( ZT8348_GetSFifoSize(g_cardNO, 1) != 2000 )
{
if( ZT8348_SetSFifoSize(g_cardNO, 1, 2000) != 2000 )
{
CString tempStr1;
tempStr1.Format(_T("g_cardNO: %d"), g_cardNO);
AfxMessageBox(tempStr1);
AfxMessageBox(_T("未申请到足够的缓冲区"));
CString tempStr;
tempStr.Format(_T("错误号: %d"), ZT8348_GetLastErr());
AfxMessageBox(tempStr);
return -1;
}
}
g_wantCount = 1024000;//200000;
ZT8348_SetSFifoThreshold(g_cardNO, 1, g_wantCount); //设置SFIFO阀值
//打开中断,注意HFIFO超过半满时打开中断会死机
//建立一个事件
g_hEventSFIFO = CreateEvent(NULL,false,false,NULL);
if( (HANDLE)g_hEventSFIFO != NULL )
ZT8348_OpenIRQ(g_cardNO, (long)g_hEventSFIFO, 0); //打开中断
ULONG AIRangeArr;
ULONG ampInxArr;
//不能用,因为并未把所有元素初始化为2,ULONG AIRangeArr = {2};
//ULONG ampInxArr = {0};
//公式:采样频率= 16000000Hz/(分频系数+1)
const ULONG ADinClock = 16000000;
ULONG ADfreq = 500000;//分频系数
ULONG ADstartModeWord = 1; //定时启动
ULONG i;
//量程为 -10~+10V
for(i=0; i<8; i++)
{//2 :0-- +10000mV
AIRangeArr = 2;// 6;//按照 -10000 -- +10000mV 量程装入零 点和满度值,必须与板卡的实际跳线相一致
ampInxArr = 0;
}
ZT8348_AIinit(g_cardNO, 0, 2, AIRangeArr, ampInxArr, ADstartModeWord, ADinClock / ADfreq - 1, 0, 0);
ZT8348_EnableAD(g_cardNO); //启动定时AD
//返回函数执行的状态
CString tempStr;
tempStr.Format(_T("错误号: %d"), ZT8348_GetLastErr());
/*PYali是在取值区间范围内返回触发停止采集*/
//AfxMessageBox("2333333");
// float att = 5000;
// Sleep(3000);
while(0.02){
Sleep(6000);
if(cunya>0){
// AfxMessageBox(_T("成功获取压力值"));
return cunya;
}
// AfxMessageBox(_T("等待2秒获取稳定压力值"));
}
// return 0;
}
void StopCaiji(){
//停止采集
fclose(g_fp);//关闭
ZT8348_CloseIRQ(g_cardNO); //关闭中断
//返回函数执行的状态
CString tempStr;
tempStr.Format(_T("错误号: %d"), ZT8348_GetLastErr());
g_keepRunning = 0;
if( (HANDLE)g_hEventSFIFO != NULL )
{
if( CloseHandle( g_hEventSFIFO ) != 0L )
{
g_hEventSFIFO = NULL;
AfxMessageBox(_T("关闭采集"));
}
}
return ;
}使用MFC按钮功能调用StartCaiji(a)函数,呈现卡死状态,使用弹框发现创建第一个线程ThreadFunc并没有被执行,然后一直走下面的while(a)方法,在while(a)方法上sleep也不能让ThreadFunc函数执行,不知道要怎么解决,请问有什么解决办法吗?
用这个试试看
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef _MSC_VER
#include <windows.h>
#include <io.h>
#include <process.h>
#defineMYVOID void
#else
#include <unistd.h>
#include <sys/time.h>
#include <pthread.h>
#defineCRITICAL_SECTION pthread_mutex_t
#define_vsnprintf vsnprintf
#defineMYVOID void *
#endif
//Log{
#define MAXLOGSIZE 20000000
#define ARRSIZE(x) (sizeof(x)/sizeof(x))
#include <time.h>
#include <sys/timeb.h>
#include <stdarg.h>
char logfilename1[]="MyLog1.log";
char logfilename2[]="MyLog2.log";
char logstr;
char datestr;
char timestr;
char mss;
CRITICAL_SECTION cs_log;
FILE *flog;
#ifdef _MSC_VER
void Lock(CRITICAL_SECTION *l) {
EnterCriticalSection(l);
}
void Unlock(CRITICAL_SECTION *l) {
LeaveCriticalSection(l);
}
void sleep_ms(int ms) {
Sleep(ms);
}
#else
void Lock(CRITICAL_SECTION *l) {
pthread_mutex_lock(l);
}
void Unlock(CRITICAL_SECTION *l) {
pthread_mutex_unlock(l);
}
void sleep_ms(int ms) {
usleep(ms*1000);
}
#endif
void LogV(const char *pszFmt,va_list argp) {
struct tm *now;
struct timeb tb;
if (NULL==pszFmt||0==pszFmt) return;
if (-1==_vsnprintf(logstr,ARRSIZE(logstr),pszFmt,argp)) logstr=0;
ftime(&tb);
now=localtime(&tb.time);
sprintf(datestr,"%04d-%02d-%02d",now->tm_year+1900,now->tm_mon+1,now->tm_mday);
sprintf(timestr,"%02d:%02d:%02d",now->tm_hour ,now->tm_min,now->tm_sec );
sprintf(mss,"%03d",tb.millitm);
printf("%s %s.%s %s",datestr,timestr,mss,logstr);
flog=fopen(logfilename1,"a");
if (NULL!=flog) {
fprintf(flog,"%s %s.%s %s",datestr,timestr,mss,logstr);
if (ftell(flog)>MAXLOGSIZE) {
fclose(flog);
if (rename(logfilename1,logfilename2)) {
remove(logfilename2);
rename(logfilename1,logfilename2);
}
flog=fopen(logfilename1,"a");
if (NULL==flog) return;
}
fclose(flog);
}
}
void Log(const char *pszFmt,...) {
va_list argp;
Lock(&cs_log);
va_start(argp,pszFmt);
LogV(pszFmt,argp);
va_end(argp);
Unlock(&cs_log);
}
//Log}
int No_Loop=0;
MYVOID testThread(void *pcn) {
int n,i;
n=(int)pcn;
i=0;
while (1) {
sleep_ms(1000);
Log("in testThread %d:i==%ds\n",n,++i);
if (i>=5) No_Loop=1;
}
}
int main(int argc,char * argv[]) {
int i;
#ifdef _MSC_VER
InitializeCriticalSection(&cs_log);
#else
pthread_mutex_init(&cs_log,NULL);
pthread_t threads;
int threadsN;
int rc;
#endif
Log("=========BEGIN==================\n");
#ifdef _MSC_VER
_beginthread((void(__cdecl *)(void *))testThread,0,(void *)1);
#else
threadsN=0;
rc=pthread_create(&(threads),NULL,testThread,(void *)1);if (rc) Log("%d=pthread_create %d error!\n",rc,threadsN-1);
#endif
i=0;
while (1) {
sleep_ms(100);
Log("in main:i==%d\n",++i);
if (No_Loop==1) break;//
}
Log("=========END====================\n");
#ifdef _MSC_VER
DeleteCriticalSection(&cs_log);
#else
pthread_mutex_destroy(&cs_log);
#endif
return 0;
}
//2012-06-14 16:27:21.500 =========BEGIN==================
//2012-06-14 16:27:21.609 in main:i==1
//2012-06-14 16:27:21.718 in main:i==2
//2012-06-14 16:27:21.828 in main:i==3
//2012-06-14 16:27:21.937 in main:i==4
//2012-06-14 16:27:22.046 in main:i==5
//2012-06-14 16:27:22.156 in main:i==6
//2012-06-14 16:27:22.265 in main:i==7
//2012-06-14 16:27:22.375 in main:i==8
//2012-06-14 16:27:22.484 in main:i==9
//2012-06-14 16:27:22.500 in testThread 1:i==1s
//2012-06-14 16:27:22.593 in main:i==10
//2012-06-14 16:27:22.703 in main:i==11
//2012-06-14 16:27:22.812 in main:i==12
//2012-06-14 16:27:22.921 in main:i==13
//2012-06-14 16:27:23.031 in main:i==14
//2012-06-14 16:27:23.140 in main:i==15
//2012-06-14 16:27:23.250 in main:i==16
//2012-06-14 16:27:23.359 in main:i==17
//2012-06-14 16:27:23.468 in main:i==18
//2012-06-14 16:27:23.500 in testThread 1:i==2s
//2012-06-14 16:27:23.578 in main:i==19
//2012-06-14 16:27:23.687 in main:i==20
//2012-06-14 16:27:23.796 in main:i==21
//2012-06-14 16:27:23.906 in main:i==22
//2012-06-14 16:27:24.015 in main:i==23
//2012-06-14 16:27:24.125 in main:i==24
//2012-06-14 16:27:24.234 in main:i==25
//2012-06-14 16:27:24.343 in main:i==26
//2012-06-14 16:27:24.453 in main:i==27
//2012-06-14 16:27:24.500 in testThread 1:i==3s
//2012-06-14 16:27:24.562 in main:i==28
//2012-06-14 16:27:24.671 in main:i==29
//2012-06-14 16:27:24.781 in main:i==30
//2012-06-14 16:27:24.890 in main:i==31
//2012-06-14 16:27:25.000 in main:i==32
//2012-06-14 16:27:25.109 in main:i==33
//2012-06-14 16:27:25.218 in main:i==34
//2012-06-14 16:27:25.328 in main:i==35
//2012-06-14 16:27:25.437 in main:i==36
//2012-06-14 16:27:25.500 in testThread 1:i==4s
//2012-06-14 16:27:25.546 in main:i==37
//2012-06-14 16:27:25.656 in main:i==38
//2012-06-14 16:27:25.765 in main:i==39
//2012-06-14 16:27:25.875 in main:i==40
//2012-06-14 16:27:25.984 in main:i==41
//2012-06-14 16:27:26.093 in main:i==42
//2012-06-14 16:27:26.203 in main:i==43
//2012-06-14 16:27:26.312 in main:i==44
//2012-06-14 16:27:26.421 in main:i==45
//2012-06-14 16:27:26.500 in testThread 1:i==5s
//2012-06-14 16:27:26.531 in main:i==46
//2012-06-14 16:27:26.531 =========END====================
页:
[1]