如何完成c ++中的多线程处理 [英] How Multithreading in c++ is done
问题描述
嗨!
我设计了一个c ++多线程应用程序(第一次),其中一个线程执行以下任务:它从文件&读取值插入动态队列。第二个线程从队列中读取值并写入输出文件;有时我的任务就像'Queue is write complete'(第一个线程的输出)和'Queue read completed'(第二个线程的输出)之后完成,但有时它表现异常意味着它显示第二个线程的输出&然后输出第一个线程;有时也输出第一个线程,而不是第二个和更明智的输出。
下面我复制了我的代码,请建议所需的任何更改。
另外我的疑问:该操作系统是否监控这些线程,因此显示不同的输出?
#include < iostream > ;
使用 命名空间标准;
#include < windows.h >
const int MAX = 2 * 1048576 ;
int 大小;
HANDLE hThread1,hThread2;
DWORD dwID1,dwID2;
DWORD Thread1(LPVOID param);
DWORD Thread2(LPVOID param);
FILE * fp,* fpOut;
class cqueue
{
public :
static int front,rear;
static int * a; // = new int [1048576];
cqueue()
{
front = rear = - 1 跨度>;
a = new int [MAX];
}
~cqueue()
{
delete [] a;
}
静态 void insertWithAck(炭跨度>);
static int deletionWithAck();
};
int cqueue :: front;
int cqueue :: rear;
int * cqueue :: a;
void cqueue :: insertWithAck( char val)
{
while ((后面== MAX- 1 )||(后面+ 1 ==面前)) ;
if (后面== MAX- 1 )
rear = 0 跨度>;
else
rear ++;
a [后方] = val;
if (前== - 1 )
front = 0 跨度>;
}
int cqueue :: deletionWithAck()
{
char k;
while (front == - 1 );
k = a [front];
if (front == rear)
front = rear = - 1 ;
else
{
if (front == MAX- < span class =code-digit> 1 )
front = 0 ;
else
front ++;
}
return k;
}
DWORD Thread1(LPVOID参数)
{
int i,val;
fp = fopen( 1MB_TEST_FILE.txt, rb);
fseek(fp, 0 ,SEEK_END);
尺寸= ftell(fp);
倒带(fp);
for (i = 0 ; i< Size; i ++){
fread (& val, 1 , 1 ,fp);
cqueue :: insertWithAck(val);
}
fclose(fp);
cout<< 队列写完成\ n;
return 0 ;
}
DWORD Thread2(LPVOID参数)
{
fpOut = fopen( OutFile.txt, wb);
char retVal;
for ( int i = 0 ; i< Size; i ++){
retVal = cqueue :: deletionWithAck();
fwrite(& retVal, 1 , 1 ,fpOut);
}
fclose(fpOut);
cout<< 队列读取已完成\ n;
return 0 ;
}
void main()
{
cqueue c1;
hThread1 = CreateThread(NULL, 0 ,(LPTHREAD_START_ROUTINE)Thread1,NULL, 0 ,& ; dwID1);
hThread2 = CreateThread(NULL, 0 ,(LPTHREAD_START_ROUTINE)Thread2,NULL, 0 跨度>,&安培; dwID2);
system( pause)
}
您需要在 cqueue
函数上添加一个锁,以确保线程不会覆盖彼此指针或以其他方式不同步。请参阅 https://www.google.com/search?q=c%2B%2B+lock [ ^ ]
Hi!
I have design one c++ multithreading application (first time) in which one thread does following task: it read values from file & insert into dynamic queue. Second thread does read values from queue and write into output file; Sometimes task is done as i wish like 'Queue is write complete'(output of first thread) and after 'Queue read completed'(output of second thread),but sometimes it behaves abnormal means it shows output of second thread & then output of first thread;also sometimes output of first thread,not second and wiseversa.
Below i copied my code,please suggest any changes required for it.
Also my doubt:Is that operating system monitors these threads,so it shows different output?
#include <iostream>
using namespace std;
#include<windows.h>
const int MAX = 2*1048576;
int Size ;
HANDLE hThread1,hThread2;
DWORD dwID1,dwID2;
DWORD Thread1(LPVOID param);
DWORD Thread2(LPVOID param);
FILE *fp,*fpOut;
class cqueue
{
public :
static int front,rear;
static int *a;//=new int[1048576];
cqueue()
{
front=rear=-1;
a=new int[MAX];
}
~cqueue()
{
delete[] a;
}
static void insertWithAck(char);
static int deletionWithAck();
};
int cqueue::front;
int cqueue::rear;
int* cqueue::a;
void cqueue :: insertWithAck(char val)
{
while((rear==MAX-1) || (rear+1==front)) ;
if(rear==MAX-1)
rear=0;
else
rear++;
a[rear]=val;
if(front==-1)
front=0;
}
int cqueue :: deletionWithAck()
{
char k;
while(front== -1);
k=a[front];
if(front==rear)
front=rear=-1;
else
{
if(front==MAX-1)
front=0;
else
front++;
}
return k;
}
DWORD Thread1(LPVOID param)
{
int i,val;
fp=fopen("1MB_TEST_FILE.txt","rb");
fseek(fp,0,SEEK_END);
Size=ftell(fp);
rewind(fp);
for(i=0;i<Size;i++){
fread(&val,1,1,fp);
cqueue::insertWithAck(val);
}
fclose(fp);
cout<<"Queue write completed\n";
return 0;
}
DWORD Thread2(LPVOID param)
{
fpOut=fopen("OutFile.txt","wb");
char retVal;
for(int i=0;i<Size;i++){
retVal=cqueue::deletionWithAck();
fwrite(&retVal,1,1,fpOut);
}
fclose(fpOut);
cout<<"Queue read completed\n";
return 0;
}
void main()
{
cqueue c1;
hThread1= CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)Thread1,NULL,0,&dwID1);
hThread2= CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)Thread2,NULL,0,&dwID2);
system("pause")
}
You need to add a lock on the cqueue
functions to ensure that the threads do not overwrite each others pointers or otherwise get out of sync. See https://www.google.com/search?q=c%2B%2B+lock[^].
这篇关于如何完成c ++中的多线程处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!