[問題] 執行緒在類別內的問題
開發平台(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
08/20 15:34, 1F
推
08/20 15:39, , 2F
08/20 15:39, 2F
→
08/20 15:40, , 3F
08/20 15:40, 3F
→
08/20 22:35, , 4F
08/20 22:35, 4F
推
08/21 01:29, , 5F
08/21 01:29, 5F
→
08/21 01:30, , 6F
08/21 01:30, 6F
→
08/21 18:22, , 7F
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
08/23 15:25, 8F
→
08/23 15:26, , 9F
08/23 15:26, 9F
→
08/23 15:26, , 10F
08/23 15:26, 10F
→
08/23 15:27, , 11F
08/23 15:27, 11F
→
08/23 15:28, , 12F
08/23 15:28, 12F
→
08/23 15:31, , 13F
08/23 15:31, 13F
→
08/23 15:32, , 14F
08/23 15:32, 14F
→
08/26 13:36, , 15F
08/26 13:36, 15F
→
08/26 13:37, , 16F
08/26 13:37, 16F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章