【编程题】多线程 (C++)
来源:互联网 发布:京东销量查询软件 编辑:程序博客网 时间:2024/05/17 05:11
题目
来做题了
问题描述
有4个线程和1个公共的字符数组。线程1的功能就是向数组输出A,线程2的功能就是向字符输出B,线程3的功能就是向数组输出C,线程4的功能就是向数组输出D。要求按顺序向数组赋值ABCDABCDABCD,ABCD的个数由线程函数1的参数指定。[注:C语言选手可使用WINDOWS SDK库函数]
接口说明:
void init(); //初始化函数
void Release(); //资源释放函数
unsignedint__stdcall ThreadFun1(PVOID pM) ; //线程函数1,传入一个int类型的指针,用于初始化输出A次数,资源需要线程释放
unsignedint__stdcall ThreadFun2(PVOID pM) ;//线程函数2,无参数传入
unsignedint__stdcall ThreadFun3(PVOID pM) ;//线程函数3,无参数传入
Unsigned int __stdcall ThreadFunc4(PVOID pM);//线程函数4,无参数传入
char g_write[1032]; //线程1,2,3,4按顺序向该数组赋值。
输入
10
输出
ABCDABCDABCDABCDABCDABCDABCDABCDABCDABCD
思路
具体的函数说明可以在这个博客中找到 windows.h
利用信号量,把多线程当成单线程来用。。。。
顺便说一下markdown的图片调整大小
<img src="URL" width="200" height="200" />
代码
这里给出网上一个大神写的代码分享一下
#include <iostream>#include <cstring>#include <cstdlib>#include <windows.h>using namespace std;HANDLE hThread1=NULL;HANDLE hThread2=NULL;HANDLE hThread3=NULL;HANDLE hThread4=NULL;HANDLE hMutex=NULL;char g_write[1032];int g_writeCnt=0; //已输出字符个数int g_cnt=0; //ABCD已输出次数unsigned long __stdcall ThreadFun1(PVOID pM)//线程函数1,传入一个int类型的指针[取值范围:1 – 250,测试用例保证],用于初始化输出A次数,资源需要线程释放{ int N=*(int*)pM; while(1) { WaitForSingleObject(hMutex,INFINITE); if(g_cnt==N) { //cout<<endl<<"time enough!"<<endl; exit(0); } g_write[g_writeCnt]='A'; cout<<g_write[g_writeCnt]; g_writeCnt++; g_cnt++; ReleaseMutex(hMutex); Sleep(10);//防止while(1) CPU飙升 ResumeThread(hThread2); SuspendThread(hThread1); } return 0;}unsigned long __stdcall ThreadFun2(PVOID pM)//线程函数2,无参数传入{ while(1) { WaitForSingleObject(hMutex,INFINITE); g_write[g_writeCnt]='B'; cout<<g_write[g_writeCnt]; g_writeCnt++; ReleaseMutex(hMutex); Sleep(10); ResumeThread(hThread3); SuspendThread(hThread2); } return 0;}unsigned long __stdcall ThreadFun3(PVOID pM)//线程函数3,无参数传入{ while(1) { WaitForSingleObject(hMutex,INFINITE); g_write[g_writeCnt]='C'; cout<<g_write[g_writeCnt]; g_writeCnt++; ReleaseMutex(hMutex); Sleep(10); ResumeThread(hThread4); SuspendThread(hThread3); } return 0;}unsigned long __stdcall ThreadFun4(PVOID pM)//线程函数4,无参数传入{ while(1) { WaitForSingleObject(hMutex,INFINITE); g_write[g_writeCnt]='D'; cout<<g_write[g_writeCnt]; g_writeCnt++; ReleaseMutex(hMutex); Sleep(10); ResumeThread(hThread1); SuspendThread(hThread4); } return 0;}void init() //初始化进程函数{ int N=0;//1-125 cin>>N; N=N>125?125:N; N=N<1?1:N; hMutex=CreateMutex(NULL,false,NULL); hThread2=CreateThread(NULL,0,ThreadFun2,NULL,CREATE_SUSPENDED,NULL); hThread3=CreateThread(NULL,0,ThreadFun3,NULL,CREATE_SUSPENDED,NULL); hThread4=CreateThread(NULL,0,ThreadFun4,NULL,CREATE_SUSPENDED,NULL); hThread1=CreateThread(NULL,0,ThreadFun1,(void*)&N,0,NULL); HANDLE hThreads[4]={hThread1,hThread2,hThread3,hThread4}; WaitForMultipleObjects(4,hThreads,TRUE,5000);}void Release()// 资源释放{ CloseHandle(hThread1); CloseHandle(hThread2); CloseHandle(hThread3); CloseHandle(hThread4); CloseHandle(hMutex);}int main(void){ // freopen("in.txt","r",stdin);// init(); Release(); return 0;}
代码好多好复杂。。。。为什么不能简单一些,于是我上午学习了一下最新的C++支持的多线程特性。可以用thread函数,这样就方便多了。
下面根据C++11中的thread函数进行操作。
这里主要是采用信号量mutex来保证顺序性的。
#include <iostream> // std::cout#include <thread> // std::thread#include <mutex> // std::mutexusing namespace std;volatile int counter(0); // non-atomic countermutex mtx1; // locks access to countermutex mtx2; // locks access to countermutex mtx3; // locks access to countermutex mtx4; // locks access to countervoid displayForThread1(){ while(1) { if(counter>0) { if (mtx1.try_lock()) { cout<<"A"; mtx2.unlock(); counter--; } } else { break; } }}void displayForThread2(){ while(1) { if(counter>0) { if (mtx2.try_lock()) { cout<<"B"; mtx3.unlock(); counter--; } } else { break; } }}void displayForThread3(){ while(1) { if(counter>0) { if (mtx3.try_lock()) { cout<<"C"; mtx4.unlock(); counter--; } } else { break; } }}void displayForThread4(){ while(1) { if(counter>0) { if (mtx4.try_lock()) { cout<<"D"; mtx1.unlock(); counter--; } } else { break; } }}int main (int argc, const char* argv[]){ thread threads[4]; int number; cin>>number; counter = number*4-1;//这里要减1 否则会多输出一个线程 mtx2.lock(); mtx3.lock(); mtx4.lock(); threads[0] = thread(displayForThread1); threads[1] = thread(displayForThread2); threads[2] = thread(displayForThread3); threads[3] = thread(displayForThread4); for (auto& th : threads) th.join();//所有线程 join return 0;}
- 【编程题】多线程 (C++)
- c#.net 多线程编程学习(1)
- c#.net 多线程编程学习(2)
- c#.net 多线程编程学习(3)
- Linux C 多线程编程学习(二)
- C#.net 多线程编程(一)
- C#.net 多线程编程(二)
- Linux下多线程编程(C语言)
- linux c 多线程编程练习(一)
- linux c 多线程编程练习(二)
- linux c 多线程编程练习(三)
- linux c编程 多线程(初级)
- linux c编程 多线程(中级)
- linux c编程 多线程(初级)
- (转)linux c多线程编程
- c语言多线程编程
- C#:多线程编程探索
- linux c 多线程编程
- ES5中关于数组的一些方法(检测数组、遍历数组等)
- monkey定制脚本
- OkHttp3 (三)——创建与执行网络请求
- ioctl() fcntl() setsockopt() ioctlsocket() 用法
- 10-bat学习一
- 【编程题】多线程 (C++)
- Android getevent,sendevent,input keyevent
- Android lint工具检测出的各种情况与解决方案
- 雾霾“倾国倾城” 谣言肆虐你中招了么?
- 写在js文件里的变量如何通过FORM表单提交到action
- $(document).on('selectstart',function(){ return false; })是什么意思
- iOS开发 下拉刷新上拉加载更多详解
- 虚拟机vmware workstation unrecoverable error: (vmui)错误解决方法
- 自定义View(一)自定义View的分类