NO

Author Topic: Possible thread bug?  (Read 299 times)

Offline John Z

  • Member
  • *
  • Posts: 154
Possible thread bug?
« on: December 13, 2020, 01:59:43 pm »
This program uses thrd_sleep under 9.00.9 and 10.00.6.  have I made a mistake?
Used C11 and multihread.lib, no optimize, level 2 , console program. Tried C17, and multithread dll too.
Code: [Select]
#include <threads.h>
#include <time.h>
#include <stdio.h>

int main(void)
{
    printf("Time: %s", ctime(&(time_t){time(NULL)}));
    thrd_sleep(&(struct timespec){.tv_sec=15}, NULL); // sleep 15 sec
    printf("Time: %s", ctime(&(time_t){time(NULL)}));
}
The clock should show 15 seconds difference but it appears thrd_sleep does not sleep.
Actually time.h is included in threads.h so that is redundant - but not a problem....

Any thoughts?

John Z

Update: I found and tried _nanosleep instead, it DOES work but the time is limited in total to less than 100,000,001 nanoseconds, even though there is a sec input. If set sec to 1 then an error results.

Update2: Well …. it seems thrd_sleep seems to have the same .1 second limitation ???  Why do the structures have tv_sec and tv_nsec if only 100 million nanosecs are allowed?  Docs for thrd_sleep and web resources don't mention .1 sec limitation.
« Last Edit: December 13, 2020, 03:10:41 pm by John Z »

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 1915
Re: Possible thread bug?
« Reply #1 on: December 13, 2020, 06:34:13 pm »
In Windows 7
Code: [Select]
#include <threads.h>
//#include <time.h>
#include <stdio.h>
 
int main(void)
{
    printf("Time: %s", ctime(&(time_t){time(NULL)}));
    thrd_sleep(&(struct timespec){.tv_sec=15}, NULL); // sleep x sec
    printf("Time: %s", ctime(&(time_t){time(NULL)}));
return 0;
}
Code: [Select]
Time: Sun Dec 13 19:32:08 2020
Time: Sun Dec 13 19:32:23 2020
Press any key to continue...
May the source be with you

Offline Stefan Pendl

  • Global Moderator
  • Member
  • *****
  • Posts: 569
    • Homepage
Re: Possible thread bug?
« Reply #2 on: December 13, 2020, 08:09:02 pm »
Output of Timo's code on Windows 10 with Pelles 10:
Code: [Select]
Time: Sun Dec 13 20:07:33 2020
Time: Sun Dec 13 20:07:48 2020
Press any key to continue...
---
Stefan

Proud member of the UltraDefrag Development Team

Offline John Z

  • Member
  • *
  • Posts: 154
Re: Possible thread bug?
« Reply #3 on: December 13, 2020, 09:18:48 pm »
OK - you guys messing with me?  :)

Here is what I get -
Time: Sun Dec 13 14:03:30 2020
Time: Sun Dec 13 14:03:30 2020
Press any key to continue...

Windows 10 - Pelles 9.00.9 although also on version 10 (which I'll retry - different computer)
I even used your posted code and pasted into a new project....  :'(

I have something corrupted?

I'll do the reboot but I do that every day  :)

if you have a moment could you test this minor mod?
Code: [Select]
#include <threads.h>
//#include <time.h>
#include <stdio.h>
 
int main(void)
{ int ans;
    printf("Time: %s", ctime(&(time_t){time(NULL)}));
    ans = thrd_sleep(&(struct timespec){.tv_sec=15}, NULL); // sleep x sec
if (ans < 0)
{ puts("error returned");}
    printf("Time: %s", ctime(&(time_t){time(NULL)}));
return 0;
}

I get an error with .tv_sec > .1 as in:
Time: Sun Dec 13 14:15:25 2020
error returned
Time: Sun Dec 13 14:15:25 2020
Press any key to continue...


Thanks for the help,
John Z

argggg - Worked fine on Windows 10 with Pelles 10.00.6 this time

Thank you all, need to see what is going on with this laptop ;( spent several hours trying to
figure out a code issue that's not a code issue......at least it is the weekend.
« Last Edit: December 13, 2020, 09:28:58 pm by John Z »

Offline Stefan Pendl

  • Global Moderator
  • Member
  • *****
  • Posts: 569
    • Homepage
Re: Possible thread bug?
« Reply #4 on: December 14, 2020, 08:16:34 am »
I created a 64-bit project, since I am running a 64-bit system.
Is there any difference, if the project is a 32-bit one?
Might be a problem with mixing 32-bit and 64-bit, system and application.
---
Stefan

Proud member of the UltraDefrag Development Team

Offline John Z

  • Member
  • *
  • Posts: 154
Re: Possible thread bug?
« Reply #5 on: December 14, 2020, 11:24:14 am »
Thanks Stefan.

Good thought.  It turns out it is a version 9 vs. version 10 difference.
I had been keeping version 9 on one machine, the one showing the error and where I still do the most ;( .  Now installed version 10 as well.  Both on the same machine.

Version 10 works, version 9 gives the error.  Checked the change log for version 10 and found:
"and the thrd_sleep function is perhaps less buggy"

The reason it initially failed on the version 10 machine is that on the version 9 machine I zipped the project from version 9, unzipped it into the version 10 machine. Then pulled up the project on the version 10 machine and saw the code intact and then did execute.  Well that did not rebuild the exe since it did not detect any source changes, so the version 9 exe was actually run. 

Appreciate the help from you and TimoVJL !  (the forum needed some activity anyway  :) )

John Z