📄 list8-2.cpp
字号:
// Listing 8-1// This program demonstrates the// use of a lockable global variable Number updated by// two threads#include <iostream.h>#include <pthread.h>#include <mtration.h>#include <math.h>mt_rational *Number;void *threadA(void *X){ long int Q; try{ for(Q =2; Q < 500;Q += 4) { Number->assign((Q/2),Q); cout << pthread_self() << " Thread A: " << *Number << endl; } } catch(general_exception &M) { cout << pthread_self() << M.message() << endl; } }void *threadB(void *X){ long int Y; try{ for(Y = 5;Y < 500;Y += 5){ Number->assign((Y/5),Y); cout << pthread_self() << " Thread B " << *Number << endl; } } catch(general_exception &M){ cout << pthread_self() << M.message() << endl; }}void main(void){ pthread_t ThreadA; pthread_t ThreadB; try{ Number = new mt_rational(1,1); pthread_create(&ThreadA,NULL,threadA,NULL); pthread_create(&ThreadB,NULL,threadB,NULL); pthread_join(ThreadA,NULL); pthread_join(ThreadB,NULL); delete Number; } catch(general_exception &X) { cout << "From main( ) " << X.message() << endl; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -