Win32 Async client multithreaded application

Hello,

I post here after about 2 days, 6 hours per day of reading many many articles over the internet about multithreading and synchronization. I made major improvments to my code thanks to the informations I found myself, but I still have a problem and I would like some help please.

I have to write a Win32 asynchronous client application, which reads data comming from the server in one thread (FD_READ), add the bytes received to a queue, and a second thread will take the queue to process with the data. Basically, the server sends many "variables" which looks like "id=32", or "version=10.0.5" etc... Each "variable" is separated by a '\n'.

My problem is :

- It looks like something is wrong with the data processing thread. When I start the program, the data is well processed, id and version are well extracted, but the debug window only shows the first incoming data strings (about 4-5 FD_READ "loops"), then the program stops reading and processing the data. I do not find where is the problem within my code. Here is the code (only relevant parts) :

Edit : When I try to "play" with my debug window (trying to select the text displayed) the program seems to "wake up" and the debug window displays again many "FOR" (debug line from the processing thread), and some fresh data.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
  case FD_READ:
  {					
    appendTextToEdit(hDebug, "FD_READ event\n");								
					
    int bytes_recv = recv(Socket, readBuffer, sizeof(readBuffer), 0); // char readBuffer[1024]
    readQueueMutex.lock();
    readQueue.push(readBuffer);					
    readQueueMutex.unlock();			
					
    char bytes[256];
    _itoa_s(bytes_recv, bytes, 10);					
    appendTextToEdit(hDebug, "From FD_READ : bytes reveived : \n");
    appendTextToEdit(hDebug, bytes);
    appendTextToEdit(hDebug, "\n");

    appendTextToEdit(hDebug, "End of FD_READ\n");
  }

  case FD_CONNECT: // Thread is launched here to run forever
  {
    appendTextToEdit(hDebug, "FD_CONNECT event\n");
    connectStatus = TRUE;
    statusText=TEXT("Connected");
    
    HANDLE hIncDataThread = (HANDLE)_beginthread(incDataProcess, 0, 0); // Launch incoming data process thread
    appendTextToEdit(hDebug, "From FD_CONNECT : hIncDataThread launched\n");
    appendTextToEdit(hDebug, "End of FD_CONNECT\n");
  }

  void incDataProcess(void *pBuffer)
  {
    appendTextToEdit(hDebug, "From hIncDataThread : starting thread...\n");

    for(;;)
    {
      appendTextToEdit(hDebug, "FOR\n"); 

      while (!readQueue.empty())
      {
	appendTextToEdit(hDebug, "WHILE !QUEUE.EMPTY\n");
	readQueueMutex.lock();
	string sReadBuffer = readQueue.front(); // take the next queue element
	char *debugBuffer = _strdup(sReadBuffer.c_str());
	readQueue.pop(); // removes this element from the queue
	readQueueMutex.unlock();
	
	string var;
	char *pVar = nullptr;					
	char *next_token = nullptr;

	appendTextToEdit(hDebug, "From hIncDataThread : processing : ");
	appendTextToEdit(hDebug, debugBuffer);
	appendTextToEdit(hDebug, "\n");	
		
	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");	
          }
	} //while getline	
      } // while queue is not empty
    } // infinite for loop
    appendTextToEdit(hDebug, "From hIncDataThread : closing thread\n"); // Should never be printed
}				

Last edited on
Topic archived. No new replies allowed.