ファーウェイマシン:マルチスレッド
2643 ワード
https://blog.csdn.net/myblog_dwh/article/details/39522099
タイトルの説明:
質問の説明: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タイプのポインタ[値範囲:1–250、テスト用例保証]を入力し、出力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この配列に順番に値を割り当てます.配列が境界を越えているかどうかを考慮する必要はありません.テスト例は保証します.
コード:
コンパイルエラー:
タイトルの説明:
質問の説明: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タイプのポインタ[値範囲:1–250、テスト用例保証]を入力し、出力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この配列に順番に値を割り当てます.配列が境界を越えているかどうかを考慮する必要はありません.テスト例は保証します.
コード:
#include
#include
#include
#include
#include
using namespace std;
string res("");
mutex mtx;
bool done = false;
condition_variable A, B, C, D;
void fun_A(int times) {
while (times--) {
unique_lock locker(mtx);
A.wait(locker);
res += 'A';
B.notify_one();
}
done = true;
}
void fun_B() {
while (!done) {
unique_lock locker(mtx);
B.wait(locker);
res += 'B';
C.notify_one();
}
}
void fun_C() {
while (!done) {
unique_lock locker(mtx);
C.wait(locker);
res += 'C';
D.notify_one();
}
}
void fun_D() {
while (!done) {
unique_lock locker(mtx);
D.wait(locker);
res += 'D';
A.notify_one();
}
}
int main() {
int num;
while (cin >> num) {
res = "";
thread t1(fun_A, num);
thread t2(fun_B);
thread t3(fun_C);
thread t4(fun_D);
A.notify_one();
t1.join();
t2.join();
t3.join();
t4.join();
cout << res << endl;
done = false;
}
return 0;
}
コンパイルエラー:
/tmp/a-5c77a2.o: In function `std::thread::thread(void (&)(int), int&)':
a.cpp:(.text._ZNSt6threadC2IRFviEJRiEEEOT_DpOT0_[_ZNSt6threadC2IRFviEJRiEEEOT_DpOT0_]+0x8d): undefined reference to `pthread_create'
/tmp/a-5c77a2.o: In function `std::thread::thread(void (&)())':
a.cpp:(.text._ZNSt6threadC2IRFvvEJEEEOT_DpOT0_[_ZNSt6threadC2IRFvvEJEEEOT_DpOT0_]+0x87): undefined reference to `pthread_create'
clang++: error: linker command failed with exit code 1 (use -v to see invocation)