⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 线程.cpp

📁 操作系统的线程互斥的程序实现。
💻 CPP
字号:
#include<iostream.h>#include<windows.h>#include<process.h>#include "stdafx.h"#include<stdio.h>#include<stdlib.h>int array1[50],array2[50];int i=0,sum12,sum34;bool th1=FALSE,th2=FALSE,th3=FALSE,th4=FALSE; bool key=FALSE;void thread1(){    while(i<50)	{		  cout<<"Thread1 add array1["<<i<<"] to sum1"<<endl;		  sum12+=array1[i];		  for(int j=0;j<=100000000;j++);		  i++;		  Sleep(10);	}	th1=TRUE;}void thread2(){    while(i<50)	{		  cout<<"Thread2 add array1["<<i<<"] to sum1"<<endl;		  sum12+=array1[i];		  for(int j=0;j<=100000000;j++);		  i++;		  Sleep(10);	}	th2=TRUE;}void thread3(){    while(i<50)	{		while(key);		if(i<50)		{		  key=TRUE;		  cout<<"Thread3 add array2["<<i<<"] to sum2"<<endl;		  sum34+=array2[i];		  for(int j=0;j<=100000000;j++);		  i++;		  key=FALSE;		}		Sleep(100);	}	th3=TRUE;}void thread4(){    while(i<50)	{		while(key);		if(i<50)		{		  key=TRUE;		  cout<<"Thread4 add array2["<<i<<"] to sum2"<<endl;		  sum34+=array2[i];		  for(int j=0;j<=100000000;j++);		  i++;		  key=FALSE;		}		Sleep(100);	}	th4=TRUE;}void main(){	int flag=0,sum1=0,sum3=0;	HANDLE threadH1,threadH2,threadH3,threadH4;	DWORD threadID1,threadID2,threadID3,threadID4;    for(int j=0;j<50;j++)	{array1[j]=j+1;	 array2[j]=j+1;	}    for(j=0;j<50;j++)	{sum1+=array1[j];     sum3+=array2[j];	}    threadH1=CreateThread(0,0,(LPTHREAD_START_ROUTINE) thread1,0,CREATE_SUSPENDED,&threadID1);    if(threadH1==0)		cout<<"Cannot create thread!"<<GetLastError()<<endl;	threadH2=CreateThread(0,0,(LPTHREAD_START_ROUTINE) thread2,0,CREATE_SUSPENDED,&threadID2);	if(threadH2==0)		cout<<"Cannot create thread!"<<GetLastError()<<endl;	threadH3=CreateThread(0,0,(LPTHREAD_START_ROUTINE) thread3,0,CREATE_SUSPENDED,&threadID3);    if(threadH3==0)		cout<<"Cannot create thread!"<<GetLastError()<<endl;	threadH4=CreateThread(0,0,(LPTHREAD_START_ROUTINE) thread4,0,CREATE_SUSPENDED,&threadID4);	if(threadH4==0)		cout<<"Cannot create thread!"<<GetLastError()<<endl;	SetThreadPriority(threadH1,THREAD_PRIORITY_NORMAL);	SetThreadPriority(threadH2,THREAD_PRIORITY_NORMAL);	SetThreadPriority(threadH3,THREAD_PRIORITY_NORMAL);	SetThreadPriority(threadH4,THREAD_PRIORITY_NORMAL);    cout<<"线程1和线程2对数组1进行累加(不考虑互斥)"<<endl;	cout<<"线程3和线程4对数组2进行累加(考虑互斥)"<<endl;		cout<<"Please choose:"<<endl;	cout<<"1.不考虑互斥:"<<endl;	cout<<"2.考虑互斥:"<<endl;	while(cin>>flag)	{	    switch(flag)		{		case 1:			cout<<"不考虑互斥时,线程1和线程2的工作情况是:"<<endl;		    ResumeThread(threadH1);	        ResumeThread(threadH2);	        while(1)			{if(th1 && th2) break;}	        cout<<endl<<"线程1和线程2合作累加的结果是:"<<sum12;	        cout<<endl<<"正确的结果是:"<<sum1;		    break;	    case 2:			cout<<"考虑互斥时,线程3和线程4的工作情况是:"<<endl;				    ResumeThread(threadH3);	        ResumeThread(threadH4);		    while(1)			{if(th3 && th4) break;}		    cout<<endl<<"线程3和线程4合作累加的结果是:"<<sum34;		    cout<<endl<<"正确的结果是:"<<sum3;			break;	    default:cout<<"Wrong choice,choose agin:"<<endl;		}	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -