Click here to Skip to main content
15,908,931 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey,
I'm a beginner in this. I'm writing a program that will keep displaying random letters in an infinite loop till the user hits 'enter'. The thread seems to work fine but the program execution after the execution of the thread gets messed up. As in, for the questions that follow after that part the input is not accepted properly. If the thread is removed it works fine. I tried looking everywhere but couldn't find any solution. Would really appreciate some help. Thanks.


This is the part of the code that deals with the thread. Please let me know of any mistakes. Also, can you please elaborate on how to fix it if the problem is with the memory allocation/access? By the way, I also tried using _beginthread() but experienced the same problem.

// The function passed in the thread
// c has been declared as a global variable
DWORD WINAPI StopMatrix(LPVOID)
{
 c='\0';
 c=getchar();
 while(c!='\n')
  c=getchar();
 return 0;
}
 
// Function containing the thread creation.
void RandomMatrixPrint()
{
 HANDLE ThreadVar=0;
 for(int x=0;x<;999;x++)
 {
  for(int i=0;i<10;i++)
  {
   for(int j=0;j<;10;j++)
   cout<<matrix[i][j];
   cout<<"\n";
   if(x<3)
    Sleep(1);
   else
    Sleep(35);
  }
  ThreadVar=CreateThread(NULL,0,StopMatrix,NULL,0,NULL);
  if(c=='\n')
  break;
 }
 system("cls");
 CloseHandle(ThreadVar);
}
Posted
Updated 4-May-11 3:58am
v4
Comments
Rick Shaub 3-May-11 11:09am    
Can you please edit your question to include the code that is causing you problems?
Albert Holguin 4-May-11 9:56am    
edit: added pre tags
Albert Holguin 4-May-11 10:09am    
updated my solution
Member 7893039 4-May-11 10:18am    
Ooh! Yes, I see the problem now. Feel stupid for not realizing it before! I've just begun experimenting with threads so the link to the tutorials is appreciated. Thanks a ton! :)
Albert Holguin 4-May-11 10:37am    
no prob, don't forget to accept my solution :)

1 solution

You need to put some code up to see what's going wrong, its likely you're using shared memory and not allocating/accessing correctly for use with independent thread.

Update: Looking at your code, you have CreateThread() within a for loop, the for loop will not wait for the thread to be done before moving on, therefore you may end up with multiple threads being created and all accessing the same global variable c. This is threads gone wrong... :)

I would suggest going through a CreateThread tutorial (here's one[^]) to learn how to use it properly.
 
Share this answer
 
v2
Comments
Joan M 4-May-11 7:37am    
Ouch! you should definitely put this in your question... to do so, press the IMPROVE QUESTION button under your question (right side).
Albert Holguin 4-May-11 9:54am    
did you post this as a reply to me?
Joan M 4-May-11 9:58am    
No... there was a message here from the OP... :O The code had been pasted here and I was asking the OP to remove it from here and to put it in the question he asked before... What it surprises me is that you've been the final recipient of this message... I'm not sure if I've missed the reply button or if there have been a misterious mistake inside the CP routers... but I'd say that it is the second of the options... ^^¡ so sorry for any inconvenience!
Albert Holguin 4-May-11 10:00am    
this is weird... maybe the messages get automatically "promoted" when a message is deleted, which is sort of odd...
Joan M 4-May-11 10:15am    
Really weird... but who knows...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900