Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ VC++
Hi!
I would like to create a separate thread for WriteDataToPort. How to create and pass cstring as parameter to it.
Below is my code snippet.
CString ss=_T("");
while(!feof(fpRead))
{
	size_t count = fread(file_buffer, sizeof(char), l_size, fpRead);
	for(int i=0; i< count; i++)			
	{
		ss = file_buffer[i];
		m_CommPort->WriteDataToPort(ss.GetBuffer(ss.GetLength()));
	}
}
 
void CCommPort::WriteDataToPort(TCHAR* f_WriteData)
{
	WaitForSingleObject(m_hWriteCompleteEvent,INFINITE);	
	ResetEvent(m_hWriteCompleteEvent);
	m_bKeyboardWrite =TRUE;		
	memset(m_szWriteBuff, 0, sizeof(m_szWriteBuff));
	tcscpy(m_szWriteBuff, f_WriteData);	
	SetEvent(m_hWriteEvent);
}
 
i have tried in this way, but getting exception. Please do the needful.
hThread = CreateThread(NULL,0,WriteDataToPortThread,LPVOID)ss.GetBuffer(ss.GetLength()),0,NULL);
 
DWORD __stdcall CMainFrame::WriteDataToPortThread(LPVOID lpParam)
{
    CString* ss = (CString*)lpParam;
    ((CMainFrame*)AfxGetMainWnd())->m_CommPort->WriteDataToPort(ss->GetBuffer(ss->GetLength()));
    return 0;
}
Thanks much
Sam.
Posted 4-Feb-13 17:51pm
Mr Sam701
Edited 4-Feb-13 18:22pm
v2
Comments
H.Brydon at 4-Feb-13 23:38pm
   
Your CreateThread() statement has invalid syntax. Perhaps fix that up...
 
Without you doing that, I am guessing that you want to pass "const char*" data through the LPVOID pointer, and the types on both casts should probably be "const char*" (or "const char*const" or perhaps "char*")...
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Please see: http://www.computersciencelab.com/MultithreadingTut1.htm[^].
 
Locate the section "Creating Threads Under Windows", and the code sample using _beginthread. Here is the idea: last parameter arglist is the void pointer. It is used to pass any data to the thread entry point function passed as the first parameter start_address. When you do that, start_address will be started as it is called with the arglist pointer passed as its only argument. In your implementation of start_address, you should cast this void pointer to the pointer to the data type you used to pass as the arglist when you called _beginthread.
 
Please see:
http://msdn.microsoft.com/en-us/library/aa246693%28v=vs.60%29.aspx[^].
 
See also my past answer for some more ideas: Starting a thread from a thread within a class[^].
 
—SA
  Permalink  
Comments
Albert Holguin at 5-Feb-13 1:47am
   
Good info as usual, +5
Sergey Alexandrovich Kryukov at 5-Feb-13 12:05pm
   
Thank you, Albert.
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

1) Allocate a string (object of CString class) - but not on stack. You can make it a global or a member of the application class, or even create it dynamically.
2) Create a new thread using AfxBeginThread and pass it the address of your string object, e.g.
AfxBeginThread(YourThreadFunction, &g_MyString, THREAD_PRIORITY_NORMAL); 
3) inside your thread function (e.g. YourThreadFunction), cast LPVOID p to CString * in the beginning and then you can use it as a pointer to string in your thread's loop:
 
INT YourThreadFunction(LPVOID p)
{
     CString *pStr = (CString *)p;
     while (!gCancel)
     {
          // your loop
     }
     return 0;
}
 
It is of course crude as, but you get the idea. All the usual pre-cautions about simultaneous access, reallocation and so on will still apply.
 
I normally create a thread interface class that contains everything that is needed to pass data between 2 threads - which could contain strings amongst other things.
  Permalink  
Comments
Albert Holguin at 5-Feb-13 1:46am
   
Just to comment... I'd avoid the global method and use the LPVOID, that's what it's there for, to pass information to the thread.

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



Advertise | Privacy | Mobile
Web02 | 2.8.150327.1 | Last Updated 4 Feb 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100