亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

Chinaunix

標(biāo)題: Liunx 線程交換打印訊息 [打印本頁]

作者: shihyu    時間: 2016-05-15 08:26
標(biāo)題: Liunx 線程交換打印訊息
本帖最后由 shihyu 于 2016-05-15 08:27 編輯


  1. #include <android/sensor.h>
  2. #include <gui/Sensor.h>
  3. #include <gui/SensorManager.h>
  4. #include <gui/SensorEventQueue.h>
  5. #include <utils/Looper.h>
  6. #include <pthread.h>
  7. #include <unistd.h>

  8. #include <sys/syscall.h>
  9. #define gettid() syscall(__NR_gettid)

  10. using namespace android;
  11. static int g_Flag = 1;


  12. void* thread2_fun(void* args) {

  13.     while (1) {
  14.         if (g_Flag != 1) {
  15.             continue;
  16.         }
  17.         printf("thread2_fun g_Flag=%d, g_Flag addr=%p\n",
  18.                g_Flag, &g_Flag);

  19.         g_Flag = 2;
  20.     }
  21. }
  22.    
  23. void* thread1_fun(void* args)
  24. {
  25.     while (1) {
  26.         if (g_Flag != 2) {
  27.             continue;
  28.         }
  29.         printf("thread1_fun g_Flag=%d, g_Flag addr=%p\n",
  30.                g_Flag, &g_Flag);

  31.         g_Flag = 1;
  32.     }
  33. }

  34. int main(int argc, char** argv)
  35. {
  36.     pthread_t thread1;
  37.     pthread_t thread2;

  38.     pthread_create(&thread1, NULL, thread1_fun, NULL);
  39.     pthread_create(&thread2, NULL, thread2_fun, NULL);

  40.     pthread_join(thread1, NULL);
  41.     pthread_join(thread2, NULL);
  42.     return 0;
  43. }
復(fù)制代碼
在linux 上正常不斷交錯打印下面這兩行
thread2_fun g_Flag=1, g_Flag addr=0x557a133008
thread1_fun g_Flag=2, g_Flag addr=0x557a133008



但android 手機上跑這兩thread無法用g_Flag控制交錯打印訊息
只印出就下面兩行沒了
thread2_fun g_Flag=1, g_Flag addr=0x557a133008
thread1_fun g_Flag=2, g_Flag addr=0x557a133008



我沒使用sleep 也沒 block 問題, 就是g_Flag

沒成立就一直在繞回圈 , 一直想不明白會什么只能印這兩行就沒了

請問這可能是什么原因? 還是android 上 while 空轉(zhuǎn)會造成 cpu busy ,

kernel 會對這兩個thread 做什么處理?

謝謝
作者: hellioncu    時間: 2016-05-15 20:40
g_Flag前加個volatile修飾試試




歡迎光臨 Chinaunix (http://www.72891.cn/) Powered by Discuz! X3.2