如何在不同的线程中执行 [英] How to perform in different thread

查看:96
本文介绍了如何在不同的线程中执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!

我想为WriteDataToPort创建一个单独的线程。如何创建cstring并将其作为参数传递给它。

下面是我的代码片段。

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;
}



非常感谢

Sam。


Thanks much
Sam.

推荐答案

请请参阅: http://www.computersciencelab.com/MultithreadingTut1.htm [ ^ ]。



找到创建线程部分在Windows下,使用 _beginthread 的代码示例。以下是这个想法:最后一个参数 arglist 是无效指针。它用于将任何数据传递给作为第一个参数 start_address 传递的线程入口点函数。执行此操作时,将启动 start_address ,因为它是作为唯一参数传递的 arglist 指针调用的。在 start_address 的实现中,您应该将此void指针强制转换为指向您以前传递的数据类型的指针 arglist 当你打电话给 _beginthread



请参阅:

http://msdn.microsoft.com/en-us/library/aa246693%28v = vs.60%29.aspx [ ^ ]。



另请参阅我过去的答案以获取更多想法:从班级中的一个主题开始一个主题 [ ^ ]。



-SA
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


1)分配一个字符串(obj等等CString类) - 但不在堆栈上。您可以使它成为全局或应用程序类的成员,甚至可以动态创建它。

2)使用AfxBeginThread创建一个新线程并将其传递给字符串对象的地址,例如

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)在您的线程函数内(例如YourThreadFunction),在开头将LPVOID p转换为CString *然后您可以将它用作指向您线程中字符串的指针'循环:




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.


这篇关于如何在不同的线程中执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆