- 論壇徽章:
- 1
|
本帖最后由 shihyu 于 2016-05-15 08:27 編輯
- #include <android/sensor.h>
- #include <gui/Sensor.h>
- #include <gui/SensorManager.h>
- #include <gui/SensorEventQueue.h>
- #include <utils/Looper.h>
- #include <pthread.h>
- #include <unistd.h>
- #include <sys/syscall.h>
- #define gettid() syscall(__NR_gettid)
- using namespace android;
- static int g_Flag = 1;
- void* thread2_fun(void* args) {
- while (1) {
- if (g_Flag != 1) {
- continue;
- }
- printf("thread2_fun g_Flag=%d, g_Flag addr=%p\n",
- g_Flag, &g_Flag);
- g_Flag = 2;
- }
- }
-
- void* thread1_fun(void* args)
- {
- while (1) {
- if (g_Flag != 2) {
- continue;
- }
- printf("thread1_fun g_Flag=%d, g_Flag addr=%p\n",
- g_Flag, &g_Flag);
- g_Flag = 1;
- }
- }
- int main(int argc, char** argv)
- {
- pthread_t thread1;
- pthread_t thread2;
- pthread_create(&thread1, NULL, thread1_fun, NULL);
- pthread_create(&thread2, NULL, thread2_fun, NULL);
- pthread_join(thread1, NULL);
- pthread_join(thread2, NULL);
- return 0;
- }
復(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 做什么處理?
謝謝 |
|