看板 C_and_CPP 關於我們 聯絡資訊
開發平台(Platform): (Ex: Win10, Linux, ...) win7 編譯器(Ex: GCC, clang, VC++...)+目標環境(跟開發平台不同的話需列出) visual studio2015 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) 問題(Question): server利用while(1)無限循環client丟回來的值來判斷要丟什麼東西回去 此外我也叫了另一個thread來寫入全域變數a 在while(1)裡面加入if a>1的判斷來控制要不要丟a的值給client 但是發現那個if判斷似乎完全沒有執行 因為我有用cout來做是否有做到該程序的判斷 不過連cout都沒有做 餵入的資料(Input): thread thread1(keyin); void keyin() { while (1) { cout << "please key in value of a:"; cin >> a; cout << "value of a is " << a << "\n"; Sleep(5000); } } if (*ptr >= 1) { strcpy(sendData, "a的值是 "); char buffer[1]; sprintf(buffer, "%d", a); strcat_s(sendData, buffer); strcat_s(sendData, " 的唷\n"); send(current_client, sendData, sizeof(sendData), 0); cout << sendData << "\n"; printf("\n我做完了發送a的值的流程\n"); cleanBuf(); cleanSendData(); } 預期的正確結果(Expected Output): 將if (*ptr >= 1)該做的程序完全做完 錯誤結果(Wrong Output): 沒執行 程式碼(Code):(請善用置底文網頁, 記得排版) http://ideone.com/ytAAcP 補充說明(Supplement): 大學學完C++後完全徹底的荒廢兩年 才重新自學 請鞭小力 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 118.165.156.17 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1481121398.A.52F.html ※ 編輯: airawesu (118.165.156.17), 12/07/2016 22:37:36 ※ 編輯: airawesu (118.165.156.17), 12/07/2016 22:39:45
stupid0319: 跟上一個貼文是同一個作業嗎? 12/07 22:40
airawesu: 從上一個程式碼後已經是大改造了..指標 class之類的都有 12/07 22:41
fatrabitree: 先猜volatile 12/07 22:41
o07608: 不同 12/07 22:42
firejox: volatile 有可能 12/07 23:04
volatile int a = 0; void keyin() { int a_keyin; while (1) { cout << "please key in value of a:"; cin >> a_keyin; a = a_keyin; cout << "value of a is " << a << "\n"; Sleep(5000); } } 還是不行 while內的 if(a > 0) 還是沒有被執行 ※ 編輯: airawesu (118.165.156.17), 12/08/2016 22:47:45