[問題] 執行緒在類別內的問題

看板C_and_CPP (C/C++)作者 (超越自己)時間10年前 (2015/08/20 15:11), 10年前編輯推噓1(108)
留言9則, 2人參與, 最新討論串1/1
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) Windows -- 各位板友大家好,有執行緒在類別內的問題想和各位請教, 目前參考msdn做了類似的程式, 只是將由類別內部進行無窮迴圈產生thread, 查詢網上資料得知必須將thread宣告為static, 實際使用過後,第一次能正常執行緒後續的動作, GetLastoError為0。 迴圈跑第二次以後,在CreateThread時也有產生執行緒在跑, 不知何故,後續動作有一部份不正常,GetLastError為183。 (自己推估是因用了static所導致,若不用物件導向的方式來做, 只用C語言的方式程式沒有問題。) 故懇請板友能協助,指引解決的方向,謝謝大家。 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 60.251.183.149 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1440054676.A.32C.html

08/20 15:34, , 1F
純c win32不用static 我寫MFC當member function才用static
08/20 15:34, 1F

08/20 15:39, , 2F
有沒有程式碼 這樣很難猜
08/20 15:39, 2F

08/20 15:40, , 3F
我是用C++的類別來包...XD
08/20 15:40, 3F

08/20 22:35, , 4F
謝謝D大... 目前已決定執行緒那圈不包在類別內!
08/20 22:35, 4F

08/21 01:29, , 5F
用static member function可正常使用在CreateThread呀,建議
08/21 01:29, 5F

08/21 01:30, , 6F
你貼出程式來,找出真的問題,而不是找到其他方式可以用就好
08/21 01:30, 6F

08/21 18:22, , 7F
謝謝k大,那麼我稍晚再精簡整理一下喔!
08/21 18:22, 7F
for (;;) { //m_odbcHelper.RunSQL(L"Insert into [DataCenter].[dbo].T_ST_Account values('QQ','835')"); _tprintf( TEXT("\nPipe Server: Main thread awaiting client connection on %s\n"), lpszPipename); hPipe = CreateNamedPipe( lpszPipename, // pipe name PIPE_ACCESS_DUPLEX, // read/write access PIPE_TYPE_MESSAGE | // message type pipe PIPE_READMODE_MESSAGE | // message-read mode PIPE_WAIT, // blocking mode PIPE_UNLIMITED_INSTANCES, // max. instances BufferSize, // output buffer size BufferSize, // input buffer size 0, // client time-out NULL); // default security attribute if (hPipe == INVALID_HANDLE_VALUE) { _tprintf(TEXT("CreateNamedPipe failed, GLE=%d.\n"), GetLastError()); return -1; } // Wait for the client to connect; if it succeeds, // the function returns a nonzero value. If the function // returns zero, GetLastError returns ERROR_PIPE_CONNECTED. fConnected = ConnectNamedPipe(hPipe, NULL) ? TRUE : (GetLastError() == ERROR_PIPE_CONNECTED); Data pTemp; pTemp.hPipe = hPipe; pTemp.lpMem = NULL; if (fConnected) { printf("Client connected, creating a processing thread.\n"); // Create a thread for this client. hThread = CreateThread( NULL, // no security attribute 0, // default stack size NamedPipeThreadProc, // thread proc (LPVOID) &pTemp, // thread parameter 0, // not suspended &dwThreadId); // returns thread ID if (hThread == NULL) { return -1; } else CloseHandle(hThread); } else // The client could not connect, so close the pipe. _tprintf(TEXT("CreateThread failed, GLE=%d.\n"), GetLastError()); CloseHandle(hPipe); } -- k大您好,此迴圈是在某類別的成員函式內, 將NamedPipeThreadProc設為static後, 每次執行緒調用成功後,藉由GetLastError除第1次為0外,其餘皆是印出183, 新執行緒有正常運作,但我擔心是否會有不預期的問題發生... 還請您不吝賜教,謝謝您。 ※ 編輯: BIAO (36.230.203.201), 08/23/2015 01:37:14

08/23 15:25, , 8F
你的code是來自msdn上的,我自己從msdn上copy code下來測試,
08/23 15:25, 8F

08/23 15:26, , 9F
named pipe server:https://goo.gl/wtzi28
08/23 15:26, 9F

08/23 15:26, , 10F
named pipe client:https://goo.gl/DF1Vum
08/23 15:26, 10F

08/23 15:27, , 11F
msdn的code, 即使我把server改成class, thread函式改static
08/23 15:27, 11F

08/23 15:28, , 12F
一樣可以正常work, 所以看不出你的問題到底在哪
08/23 15:28, 12F

08/23 15:31, , 13F
另外你認為thread有問題, 應該另外寫個簡單程式不要含有的沒
08/23 15:31, 13F

08/23 15:32, , 14F
的功能
08/23 15:32, 14F

08/26 13:36, , 15F
謝謝k大的測試,我會再抽空進行檢視,主要是因先前有出現183
08/26 13:36, 15F

08/26 13:37, , 16F
的錯誤代碼,讓我擔心是否自己建構有誤..
08/26 13:37, 16F
文章代碼(AID): #1LrNsKCi (C_and_CPP)
文章代碼(AID): #1LrNsKCi (C_and_CPP)