Eric,
I’m afraid I don’t understand your question/problem.
I took your code and compiled it on my machine which has a hard drive. I added a couple more cout’s to the code to help show what’s going on. The places I added them were in your main loop and the EveryRun() functions of the 2 thread classes. So it looks like:
for (m_nCount=0; ; m_nCount++)
{
MsgReceivePulse(m_chMain, &pulse, sizeof(_pulse), NULL);
cout<<"Main Start " << ::GetTime()<<endl;
if (_imu.IsReady())
{
_imu.SendPulse(PRIORITY_IMU, PULSECODE_DATA);
_imu.WaitForEvent(nsecIMU);
}
if (_dlg.IsReady())
{
_dlg.SendPulse(PRIORITY_DLG, PULSECODE_DATA);
_dlg.WaitForEvent(nsecDLG);
}
cout<<"Main End " << ::GetTime()<<endl;
}
int clsIMU::EveryRun()
{
cout<<"IMU Start " << ::GetTime()<<endl;
pthread_mutex_lock(&m_mtxIMU);
m_imuData[m_nIMU].a = 1.0;
m_imuData[m_nIMU].b = 1.0;
m_imuData[m_nIMU].c = 1.0;
m_imuData[m_nIMU].p = 1.0;
m_imuData[m_nIMU].q = 1.0;
m_imuData[m_nIMU].r = 1.0;
m_imuData[m_nIMU].acx = 1.0;
m_imuData[m_nIMU].acy = 1.0;
m_imuData[m_nIMU].acz = 1.0;
m_imuData[m_nIMU].magx = 2.0;
m_imuData[m_nIMU].magy = 2.0;
m_imuData[m_nIMU].magz = 2.0;
m_imuData[m_nIMU++].tmp = 3.0;
pthread_mutex_unlock(&m_mtxIMU);
cout<< "IMU End " <<::GetTime()<<endl;
return TRUE;
}
int clsDLG::EveryRun()
{
// cout<<"[DLG] "<<m_nCount<<endl;
cout<<"DLG Start " << ::GetTime()<<endl;
if (m_nCount%50 != 0)
return 1;
if (_imu.m_nIMU != 0)
{
pthread_mutex_lock(&_imu.m_mtxIMU);
m_nIMU = _imu.m_nIMU;
::memcpy(m_imu, _imu.m_imuData, m_nIMU*sizeof(IMUPACK));
_imu.m_nIMU = 0;
pthread_mutex_unlock(&_imu.m_mtxIMU);
}
// start writing data to the disk
m_pfLogb = ::fopen(m_szFileb, "a");
int i;
for (i=0; i<m_nIMU; i++)
{
if (m_pfLogb!=NULL)
::fwrite(&m_imu[i], sizeof(IMUPACK), 1, m_pfLogb);
}
m_nIMU = 0;
if (m_pfLogb)
{
::fclose(m_pfLogb);
m_pfLogb = NULL;
}
cout<<"DLG End " <<::GetTime()<<endl;
return TRUE;
}
I then ran this code and I got the following output:
[IMU] start
[DLG] Start
Main Start 0.0215247
IMU Start 0.0218264
IMU End 0.0218545
DLG Start 0.0218793
DLG End 0.0221828
Main End 0.0222096
Main Start 0.0415208
IMU Start 0.041585
IMU End 0.0416093
DLG Start 0.0416348
Main End 0.0416577
Main Start 0.0615115
IMU Start 0.061546
IMU End 0.0615685
DLG Start 0.0615926
Main End 0.0616151
Main Start 0.0816569
IMU Start 0.0826545
IMU End 0.0826919
DLG Start 0.0827186
Main End 0.0827411
Main Start 0.101497
IMU Start 0.101521
IMU End 0.101543
DLG Start 0.101566
Main End 0.101588
Main Start 0.12149
IMU Start 0.121517
IMU End 0.121539
DLG Start 0.121563
Main End 0.121585
Main Start 0.141484
IMU Start 0.141507
IMU End 0.141528
DLG Start 0.141552
Main End 0.141574
Main Start 0.161477
IMU Start 0.161505
IMU End 0.161527
DLG Start 0.161551
Main End 0.161573
Main Start 0.18147
IMU Start 0.181496
IMU End 0.181518
DLG Start 0.181541
Main End 0.181563
Main Start 0.201464
IMU Start 0.201498
IMU End 0.20152
DLG Start 0.201544
Main End 0.201566
Main Start 0.221456
IMU Start 0.221479
IMU End 0.221503
DLG Start 0.221527
Main End 0.221549
Main Start 0.241449
IMU Start 0.241475
IMU End 0.241497
DLG Start 0.24152
Main End 0.241542
Main Start 0.261442
IMU Start 0.261466
IMU End 0.261487
DLG Start 0.261511
Main End 0.261533
Main Start 0.281436
IMU Start 0.281476
IMU End 0.281499
DLG Start 0.281523
Main End 0.281546
Main Start 0.301428
IMU Start 0.301452
IMU End 0.301473
DLG Start 0.301497
Main End 0.301519
Main Start 0.321421
IMU Start 0.321448
IMU End 0.32147
DLG Start 0.321494
Main End 0.321516
Main Start 0.341416
IMU Start 0.341478
IMU End 0.341501
DLG Start 0.341526
Main End 0.341548
Main Start 0.361407
IMU Start 0.361431
IMU End 0.361453
DLG Start 0.361476
Main End 0.361499
Main Start 0.3814
IMU Start 0.381427
IMU End 0.381448
DLG Start 0.381472
Main End 0.381494
Main Start 0.401394
IMU Start 0.40142
IMU End 0.401441
DLG Start 0.401465
Main End 0.401487
Main Start 0.421387
IMU Start 0.421413
IMU End 0.421435
DLG Start 0.421459
Main End 0.421481
Main Start 0.44138
IMU Start 0.441405
IMU End 0.441427
DLG Start 0.441454
Main End 0.441477
Main Start 0.461373
IMU Start 0.461399
IMU End 0.461421
DLG Start 0.461445
Main End 0.461467
Main Start 0.481366
IMU Start 0.481395
IMU End 0.481416
DLG Start 0.48144
Main End 0.481462
Main Start 0.501359
IMU Start 0.501382
IMU End 0.501404
DLG Start 0.501427
Main End 0.501449
Main Start 0.521352
IMU Start 0.521379
IMU End 0.521401
DLG Start 0.521425
Main End 0.521447
Main Start 0.541345
IMU Start 0.541383
IMU End 0.541406
DLG Start 0.54143
Main End 0.541453
Main Start 0.561338
IMU Start 0.561367
IMU End 0.561389
DLG Start 0.561412
Main End 0.561435
Main Start 0.581331
IMU Start 0.581355
IMU End 0.581377
DLG Start 0.5814
Main End 0.581422
Main Start 0.601325
IMU Start 0.601375
IMU End 0.601397
DLG Start 0.601422
Main End 0.601444
Main Start 0.621317
IMU Start 0.621342
IMU End 0.621364
DLG Start 0.621387
Main End 0.62141
Main Start 0.64131
IMU Start 0.641336
IMU End 0.641358
DLG Start 0.641382
Main End 0.641405
Main Start 0.661304
IMU Start 0.661327
IMU End 0.661348
DLG Start 0.661375
Main End 0.661397
Main Start 0.681297
IMU Start 0.681326
IMU End 0.681348
DLG Start 0.681372
Main End 0.681394
Main Start 0.70129
IMU Start 0.701313
IMU End 0.701335
DLG Start 0.701358
Main End 0.70138
Main Start 0.721283
IMU Start 0.721316
IMU End 0.721338
DLG Start 0.721361
Main End 0.721384
Main Start 0.741276
IMU Start 0.741303
IMU End 0.741324
DLG Start 0.741348
Main End 0.74137
Main Start 0.761269
IMU Start 0.761292
IMU End 0.761314
DLG Start 0.761337
Main End 0.761359
Main Start 0.781273
IMU Start 0.781539
IMU End 0.781572
DLG Start 0.781598
Main End 0.78162
Main Start 0.801255
IMU Start 0.801279
IMU End 0.801301
DLG Start 0.801324
Main End 0.801346
Main Start 0.821248
IMU Start 0.821275
IMU End 0.821297
DLG Start 0.82132
Main End 0.821342
Main Start 0.841244
IMU Start 0.841303
IMU End 0.841326
DLG Start 0.841351
Main End 0.841374
Main Start 0.861235
IMU Start 0.861269
IMU End 0.861292
DLG Start 0.861316
Main End 0.861338
Main Start 0.881227
IMU Start 0.881256
IMU End 0.881278
DLG Start 0.881302
Main End 0.881328
Main Start 0.90122
IMU Start 0.901244
IMU End 0.901265
DLG Start 0.901289
Main End 0.901311
Main Start 0.921214
IMU Start 0.921241
IMU End 0.921263
DLG Start 0.921287
Main End 0.921309
Main Start 0.941207
IMU Start 0.941232
IMU End 0.941254
DLG Start 0.941278
Main End 0.9413
Main Start 0.9612
IMU Start 0.961226
IMU End 0.961247
DLG Start 0.961271
Main End 0.961293
Main Start 0.981193
IMU Start 0.981217
IMU End 0.981238
DLG Start 0.981262
Main End 0.981285
Main Start 1.00119
IMU Start 1.00122
IMU End 1.00124
DLG Start 1.00126
Main End 1.00129
Main Start 1.02118
IMU Start 1.0212
IMU End 1.02122
DLG Start 1.02125
DLG End 1.02149
So looking at the start of the output I see:
Main Start 0.0215247
IMU Start 0.0218264
IMU End 0.0218545
DLG Start 0.0218793
DLG End 0.0221828
Main End 0.0222096
which shows your main and 2 threads completed running in about 1 ms of time. That’s far quicker than you expected.
Then you delay for 20 ms (0.0415208-0.0215247) till you wake your main thread up again and I then see:
Main Start 0.0415208
IMU Start 0.041585
IMU End 0.0416093
DLG Start 0.0416348
Main End 0.0416577
This time your DLG code exits early so it never completes the write (not sure why you are doing that). Again tho, it all completes in about 1 ms of time.
So what is it exactly that you are seeing that you don’t understand?
All the output I see shows the main thread waking every 20 ms and running 2 other threads which themselves run in about 1 ms of time.
Tim