如何用多线程(一个线程不断写文件A,另一个线程不断从文件A读入写入另一文件B)!

hsrcm7612 2004-09-02 03:10:58
现在,,两个函数我已写好,,请问怎么用多线程来实现!
/////////////////////
DWORD WINAPI fun1(void *)
{
FILE *fp;
char ch;
CRITICAL_SECTION cm_fp;
if ((fp=fopen("d:\\1.txt","a+"))==NULL)
{
printf("cannot open file\n");
exit(1);
}
ch=getchar();
while (ch!='#')
{
while (ch!='\n')
{
fputc(ch,fp);
fflush(fp);
ch=getchar();
}
ch=getchar();
}
fclose(fp);
}

DWORD WINAPI fun2(void *)
{
FILE *in,*out;
char buf[10];
CRITICAL_SECTION cm_fp;
if((in=fopen("d:\\1.txt","rb"))==NULL)
{
printf("cannot open infile\n");
exit(0);
}
if((out=fopen("d:\\2.txt","a+"))==NULL)
{
printf("cannot open outfile\n");
exit(0);
}
while (!feof(in))
{
fputs(fgets(buf,10,in),out);
fflush(out);
}
fclose(in);
fclose(out);
}
...全文
278 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
freefalcon 2004-09-02
  • 打赏
  • 举报
回复
呵呵,楼主用到了临界区(CRITICAL_SECTION),不过使用方法不对
不能定义一个局部变量,那样没有意义
另外需要用EnterCriticalSection和LeaveCriticalSection进行加锁和解锁
北极猩猩 2004-09-02
  • 打赏
  • 举报
回复
不过事情并不仅仅是创建两个线程那么简单。
你这个需求是一个变形的生产者/消费者问题,需要进行线程同步,建议楼主回去看一看有关操作系统进程管理的部分
freefalcon 2004-09-02
  • 打赏
  • 举报
回复
使用API
CreateThread()
或者
_beginthread, _beginthreadex

使用方法很简单,看看msdn就知道了
北极猩猩 2004-09-02
  • 打赏
  • 举报
回复
#include <windows.h>
#include <conio.h>

DWORD WINAPI ThreadFunc( LPVOID lpParam )
{
char szMsg[80];

wsprintf( szMsg, "Parameter = %d.", *(DWORD*)lpParam );
MessageBox( NULL, szMsg, "ThreadFunc", MB_OK );

return 0;
}

VOID main( VOID )
{
DWORD dwThreadId, dwThrdParam = 1;
HANDLE hThread;
char szMsg[80];

hThread = CreateThread(
NULL, // default security attributes
0, // use default stack size
ThreadFunc, // thread function
&dwThrdParam, // argument to thread function
0, // use default creation flags
&dwThreadId); // returns the thread identifier

// Check the return value for success.

if (hThread == NULL)
{
wsprintf( szMsg, "CreateThread failed." );
MessageBox( NULL, szMsg, "main", MB_OK );
}
else
{
_getch();
CloseHandle( hThread );
}
}
北极猩猩 2004-09-02
  • 打赏
  • 举报
回复
CreateThread

The CreateThread function creates a thread to execute within the virtual address space of the calling process.

To create a thread that runs in the virtual address space of another process, use the CreateRemoteThread function.


HANDLE CreateThread(
LPSECURITY_ATTRIBUTES lpThreadAttributes,
SIZE_T dwStackSize,
LPTHREAD_START_ROUTINE lpStartAddress,
LPVOID lpParameter,
DWORD dwCreationFlags,
LPDWORD lpThreadId
);

Parameters
lpThreadAttributes
[in] Pointer to a SECURITY_ATTRIBUTES structure that determines whether the returned handle can be inherited by child processes. If lpThreadAttributes is NULL, the handle cannot be inherited.
The lpSecurityDescriptor member of the structure specifies a security descriptor for the new thread. If lpThreadAttributes is NULL, the thread gets a default security descriptor. The ACLs in the default security descriptor for a thread come from the primary or impersonation token of the creator.

dwStackSize
[in] Initial size of the stack, in bytes. The system rounds this value to the nearest page. If this parameter is zero, the new thread uses the default size for the executable. For more information, see Thread Stack Size.
lpStartAddress
[in] Pointer to the application-defined function to be executed by the thread and represents the starting address of the thread. For more information on the thread function, see ThreadProc.
lpParameter
[in] Pointer to a variable to be passed to the thread.
dwCreationFlags
[in] Flags that control the creation of the thread. If the CREATE_SUSPENDED flag is specified, the thread is created in a suspended state, and will not run until the ResumeThread function is called. If this value is zero, the thread runs immediately after creation.

Windows Server 2003 and Windows XP: If the STACK_SIZE_PARAM_IS_A_RESERVATION flag is specified, the dwStackSize parameter specifies the initial reserve size of the stack. Otherwise, dwStackSize specifies the commit size.


lpThreadId
[out] Pointer to a variable that receives the thread identifier. If this parameter is NULL, the thread identifier is not returned.

Windows Me/98/95: This parameter may not be NULL.


Return Values
If the function succeeds, the return value is a handle to the new thread.

If the function fails, the return value is NULL. To get extended error information, call GetLastError.

Note that CreateThread may succeed even if lpStartAddress points to data, code, or is not accessible. If the start address is invalid when the thread runs, an exception occurs, and the thread terminates. Thread termination due to a invalid start address is handled as an error exit for the thread's process. This behavior is similar to the asynchronous nature of CreateProcess, where the process is created even if it refers to invalid or missing dynamic-link libraries (DLLs).


Windows Me/98/95: CreateThread succeeds only when it is called in the context of a 32-bit program. A 32-bit DLL cannot create an additional thread when that DLL is being called by a 16-bit program

64,676

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧