C ++ Win32 Thread Data DataC++

Программы на C++. Форум разработчиков
Ответить
Anonymous
 C ++ Win32 Thread Data Data

Сообщение Anonymous »

Я написал клиентское приложение Win32 и хотел бы создать поток, чтобы продолжить входящие данные отдельно. У меня есть проблема со следующим кодом. Поток запускается, получает некоторые данные, но не работает должным образом (данные не завершены). Я не вижу, откуда возникает проблема. Не могли бы вы мне помочь? < /P>

void incData(void *arg) // This is the thread
{
WaitForSingleObject(hIncDataMutex, INFINITE);

char *sRead = _strdup(sReadBuffer.c_str());
appendTextToEdit(hDebug, sRead); // Some data is displayed, but incompletely
string var;
char *pVar = nullptr;
char *next_token = nullptr;

istringstream iss(sReadBuffer); // sReadBuffer is the global variable I use to pass argument to my thread

while (getline(iss, var)) // Default delimiter '\n'
{
pVar = _strdup(var.c_str()); // Cast string to char *
appendTextToEdit(hDebug, pVar);
appendTextToEdit(hDebug, "\n");

if(strstr(pVar, "id=") != NULL)
{
char *label = strtok_s(pVar, "=", &next_token);
char *pId = strtok_s(NULL, "\n", &next_token);
strcpy_s(id, pId);
}

if( strstr(pVar, "version") != NULL)
{
char *label = strtok_s(pVar, "=", &next_token);
char *pVersion = strtok_s(NULL, "\n", &next_token);
strcpy_s(version, pVersion);
}

if( strstr(pVar, "Qh57=") != NULL)
{
char *label = strtok_s(pVar, "=", &next_token);
char *pFoFd = strtok_s(NULL, "\n", &next_token);
strcpy_s(foFd, pFoFd);
}
}
ReleaseMutex(hIncDataMutex);
}

//.....

case FD_CONNECT: // I launch the thread here (I want it to run forever in the background)
{
connectStatus = TRUE;
statusText=TEXT("Connected");

hIncDataMutex = CreateMutex(NULL, false, NULL); // Create incoming data process thread mutex
HANDLE hThread2 = (HANDLE)_beginthread(incData, 0, 0); // Launch incoming data process thread
}

//....

case FD_READ:
{
int bytes_recv = recv(Socket, readBuffer, sizeof(readBuffer), 0);

sReadBuffer = readBuffer; // Copy the buffer to global scope string (used to feed thread)

ReleaseMutex(hIncDataMutex);
}
break;
< /code>

edit < /strong> < /p>

Вот мой код с новыми линиями отладки и вывод из окна отладки: < /p>

void incData(void *arg)
{
WaitForSingleObject(hIncDataMutex, INFINITE);
appendTextToEdit(hDebug, "Inside thread...\n");

string var;
char *pVar = nullptr;
char *next_token = nullptr;

istringstream iss(sReadBuffer); // Put into a stream

while (getline(iss, var)) // Default delimiter '\n'
{
pVar = _strdup(var.c_str()); // Cast string to char *

if(strstr(pVar, "id=") != NULL)
{
char *label = strtok_s(pVar, "=", &next_token);
char *pId = strtok_s(NULL, "\n", &next_token);
strcpy_s(id, pId);
}

if( strstr(pVar, "version") != NULL)
{
char *label = strtok_s(pVar, "=", &next_token);
char *pVersion = strtok_s(NULL, "\n", &next_token);
strcpy_s(version, pVersion);
}

if( strstr(pVar, "Qh57=") != NULL)
{
char *label = strtok_s(pVar, "=", &next_token);
char *pFoFd = strtok_s(NULL, "\n", &next_token);
strcpy_s(foFd, pFoFd);
appendTextToEdit(hDebug, "Qh57=");
appendTextToEdit(hDebug, foFd);
appendTextToEdit(hDebug, "\n");
}
}

ReleaseMutex(hIncDataMutex);
appendTextToEdit(hDebug, "Mutex released by thread\n");
}

//....

case FD_READ:
{
appendTextToEdit(hDebug, "FD_READ event\n");

int bytes_recv = recv(Socket, readBuffer, sizeof(readBuffer), 0);
appendTextToEdit(hDebug, "Bytes reveived\n");
sReadBuffer = readBuffer; // Copy the buffer to string
ReleaseMutex(hIncDataMutex);
appendTextToEdit(hDebug, "End of FD_READ\n");
}
break;
< /code>

из окна отладки: < /p>

FD_CONNECT, begining thread
FD_READ event
Bytes reveived
End of FD_READ
FD_READ event
Bytes reveived
End of FD_READ
Inside thread... // Thread is only called here !
FD_READ event
Bytes reveived
End of FD_READ
Mutex released by thread // Thread ends here
FD_READ event
Bytes reveived
End of FD_READ
< /code>

etc...
есть идея? < /p>

Подробнее здесь: https://stackoverflow.com/questions/318 ... ing-thread
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C++»