1
0

hatomic_test.cpp 791 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. #include <stdio.h>
  2. #include "hatomic.h"
  3. #include "hthread.h"
  4. hatomic_flag_t flag = HATOMIC_FLAG_INIT;
  5. hatomic_t cnt = HATOMIC_VAR_INIT(0);
  6. HTHREAD_ROUTINE(test_hatomic_flag) {
  7. if (!hatomic_flag_test_and_set(&flag)) {
  8. printf("tid=%ld flag 0=>1\n", hv_gettid());
  9. }
  10. else {
  11. printf("tid=%ld flag=1\n", hv_gettid());
  12. }
  13. return 0;
  14. }
  15. HTHREAD_ROUTINE(test_hatomic) {
  16. for (int i = 0; i < 10; ++i) {
  17. long n = hatomic_inc(&cnt);
  18. printf("tid=%ld cnt=%ld\n", hv_gettid(), n);
  19. hv_delay(1);
  20. }
  21. return 0;
  22. }
  23. int main() {
  24. for (int i = 0; i < 10; ++i) {
  25. hthread_create(test_hatomic_flag, NULL);
  26. }
  27. for (int i = 0; i < 10; ++i) {
  28. hthread_create(test_hatomic, NULL);
  29. }
  30. hv_delay(1000);
  31. return 0;
  32. }