信号量 post 大于一
Semaphore post to greater than one
如何使信号量的值大于一。我正在尝试编写一个启动两个线程并在满足特定条件后同时拥有两个线程 运行 的程序。
该程序试图将大小为 5 的两个数组(b1 和 b2)复制到一个 10*10 数组。每次迭代,b1 的值都被复制到 x[0][i] 到 x[4][i],b2 的值被复制到 x[5][i] 到 x[9][i](其中 i是迭代索引)。
最后的结果应该是这样的:
0 1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 10
2 3 4 5 6 7 8 9 10 11
3 4 5 6 7 8 9 10 11 12
4 5 6 7 8 9 10 11 12 13
5 6 7 8 9 10 11 12 13 14
6 7 8 9 10 11 12 13 14 15
7 8 9 10 11 12 13 14 15 16
8 9 10 11 12 13 14 15 16 17
9 10 11 12 13 14 15 16 17 18
我尝试了以下代码:
Driver.cpp
#include <sys/types.h> /* Primitive System Data Types */
#include <errno.h> /* Errors */
#include <stdio.h> /* Input/Output */
#include <stdlib.h> /* General Utilities */
#include <thread> /* C++11 Threads */
#include <string.h> /* String handling */
#include <semaphore.h> /* Semaphore */
#include <vector> /* Vector */
#include <iostream>
#include <iomanip>
#include "handler.h"
using namespace std;
sem_t Sema1;
sem_t Sema2;
int ThreadsNumber = 2; /* shared variable */
int counter;
vector<std::thread> threadList;
int main()
{
int x[10][10];
int b1[5];
int b2[5];
sem_init(&Sema1, 0, 2); /* initialize Sema1 to Lock - binary semaphore */
sem_init(&Sema2, 0, 1); /* initialize Sema2 to UnLock - binary semaphore */
for (int i = 0; i < 10; i++)
{
sem_wait(&Sema2);
counter = ThreadsNumber;
// Update the matrix
for (int j = 0; j < 5; j++)
{
b1[j] = j + i;
b2[j] = j + i + 5;
}
sem_init(&Sema1, 0, 2);
if (i == 0)
{
// Create the threads during the first interations
threadList.push_back(std::thread(handler, x, b1, 0, 0));
threadList.push_back(std::thread(handler, x, b2, 5, 1));
}
// Kill all threads in the last iteration
if (i == 9)
{
for (auto& threadID : threadList){
threadID.join();
}
}
}
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
cout << left << setw(4) << x[i][j];
}
cout << endl;
}
sem_destroy(&Sema1); /* destroy semaphore */
/* exit */
return 0;
} /* main() */
handler.cpp
#include "handler.h"
void handler(int x[10][10], int b[], int start, int id)
{
for (int i = 0; i < 10; i++)
{
sem_wait(&Sema1); /* down semaphore */
for (int j = start; j < 5 + start; j++)
{
x[i][j] = b[j - start];
}
/* printf("Thread %d: Waiting to print results...\n", id);
for (int j = start; j < 5 + start; j++)
{
printf("x[%d][%d] = %d\n", i, j, x[i][j]);
}
*/
counter--;
cout << counter << endl;
if (counter == 0)
{
sem_post(&Sema2);
}
/* END CRITICAL REGION */
}
}
handler.h
#include <iostream>
#include <semaphore.h>
using namespace std;
extern sem_t Sema1;
extern sem_t Sema2;
extern int ThreadsNumber;
extern int counter;
/* prototype for thread routine */
void handler(int x[10][10], int b[], int start, int id);
只是效果不佳,没有得到预期的效果。还有其他方法吗?
通过对信号量的研究后,我能够按如下方式修复上面的代码:
driver.cpp
#include <sys/types.h> /* Primitive System Data Types */
#include <errno.h> /* Errors */
#include <stdio.h> /* Input/Output */
#include <stdlib.h> /* General Utilities */
#include <thread> /* C++11 Threads */
#include <string.h> /* String handling */
#include <semaphore.h> /* Semaphore */
#include <vector> /* Vector */
#include <iostream>
#include <iomanip>
#include "handler.h"
using namespace std;
sem_t Sema1;
sem_t Sema2;
int ThreadsNumber = 2; /* shared variable */
int counter;
vector<std::thread> threadList;
int main()
{
int x[10][10];
int b1[5];
int b2[5];
sem_init(&Sema1, 0, 0); /* initialize Sema1 to Lock - binary semaphore */
sem_init(&Sema2, 0, 2); /* initialize Sema2 to UnLock - binary semaphore */
// Create the threads during the first interations
threadList.push_back(std::thread(handler, x, b1, 0, 0));
threadList.push_back(std::thread(handler, x, b2, 5, 1));
for (int i = 0; i < 10; i++)
{
sem_wait(&Sema2);
sem_wait(&Sema2);
//counter = ThreadsNumber;
// Update the matrix
for (int j = 0; j < 5; j++)
{
b1[j] = j + i;
b2[j] = j + i + 5;
}
for (int i = 0; i < ThreadsNumber; i++)
{
sem_post(&Sema1);
}
}
// Kill all threads in the last iteration
for (auto& threadID : threadList){
threadID.join();
}
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
cout << left << setw(4) << x[i][j];
}
cout << endl;
}
sem_destroy(&Sema1); /* destroy semaphore */
/* exit */
return 0;
} /* main() */
handler.cpp
#include "handler.h"
void handler(int x[10][10], int b[], int start, int id)
{
for (int i = 0; i < 10; i++)
{
sem_wait(&Sema1); /* down semaphore */
for (int j = start; j < 5 + start; j++)
{
x[i][j] = b[j - start];
}
sem_post(&Sema2);
/* END CRITICAL REGION */
}
}
driver.h
#include <iostream>
#include <semaphore.h>
using namespace std;
extern sem_t Sema1;
extern sem_t Sema2;
extern int ThreadsNumber;
/* prototype for thread routine */
void handler(int x[10][10], int b[], int start, int id);
如何使信号量的值大于一。我正在尝试编写一个启动两个线程并在满足特定条件后同时拥有两个线程 运行 的程序。 该程序试图将大小为 5 的两个数组(b1 和 b2)复制到一个 10*10 数组。每次迭代,b1 的值都被复制到 x[0][i] 到 x[4][i],b2 的值被复制到 x[5][i] 到 x[9][i](其中 i是迭代索引)。
最后的结果应该是这样的:
0 1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 10
2 3 4 5 6 7 8 9 10 11
3 4 5 6 7 8 9 10 11 12
4 5 6 7 8 9 10 11 12 13
5 6 7 8 9 10 11 12 13 14
6 7 8 9 10 11 12 13 14 15
7 8 9 10 11 12 13 14 15 16
8 9 10 11 12 13 14 15 16 17
9 10 11 12 13 14 15 16 17 18
我尝试了以下代码:
Driver.cpp
#include <sys/types.h> /* Primitive System Data Types */
#include <errno.h> /* Errors */
#include <stdio.h> /* Input/Output */
#include <stdlib.h> /* General Utilities */
#include <thread> /* C++11 Threads */
#include <string.h> /* String handling */
#include <semaphore.h> /* Semaphore */
#include <vector> /* Vector */
#include <iostream>
#include <iomanip>
#include "handler.h"
using namespace std;
sem_t Sema1;
sem_t Sema2;
int ThreadsNumber = 2; /* shared variable */
int counter;
vector<std::thread> threadList;
int main()
{
int x[10][10];
int b1[5];
int b2[5];
sem_init(&Sema1, 0, 2); /* initialize Sema1 to Lock - binary semaphore */
sem_init(&Sema2, 0, 1); /* initialize Sema2 to UnLock - binary semaphore */
for (int i = 0; i < 10; i++)
{
sem_wait(&Sema2);
counter = ThreadsNumber;
// Update the matrix
for (int j = 0; j < 5; j++)
{
b1[j] = j + i;
b2[j] = j + i + 5;
}
sem_init(&Sema1, 0, 2);
if (i == 0)
{
// Create the threads during the first interations
threadList.push_back(std::thread(handler, x, b1, 0, 0));
threadList.push_back(std::thread(handler, x, b2, 5, 1));
}
// Kill all threads in the last iteration
if (i == 9)
{
for (auto& threadID : threadList){
threadID.join();
}
}
}
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
cout << left << setw(4) << x[i][j];
}
cout << endl;
}
sem_destroy(&Sema1); /* destroy semaphore */
/* exit */
return 0;
} /* main() */
handler.cpp
#include "handler.h"
void handler(int x[10][10], int b[], int start, int id)
{
for (int i = 0; i < 10; i++)
{
sem_wait(&Sema1); /* down semaphore */
for (int j = start; j < 5 + start; j++)
{
x[i][j] = b[j - start];
}
/* printf("Thread %d: Waiting to print results...\n", id);
for (int j = start; j < 5 + start; j++)
{
printf("x[%d][%d] = %d\n", i, j, x[i][j]);
}
*/
counter--;
cout << counter << endl;
if (counter == 0)
{
sem_post(&Sema2);
}
/* END CRITICAL REGION */
}
}
handler.h
#include <iostream>
#include <semaphore.h>
using namespace std;
extern sem_t Sema1;
extern sem_t Sema2;
extern int ThreadsNumber;
extern int counter;
/* prototype for thread routine */
void handler(int x[10][10], int b[], int start, int id);
只是效果不佳,没有得到预期的效果。还有其他方法吗?
通过对信号量的研究后,我能够按如下方式修复上面的代码:
driver.cpp
#include <sys/types.h> /* Primitive System Data Types */
#include <errno.h> /* Errors */
#include <stdio.h> /* Input/Output */
#include <stdlib.h> /* General Utilities */
#include <thread> /* C++11 Threads */
#include <string.h> /* String handling */
#include <semaphore.h> /* Semaphore */
#include <vector> /* Vector */
#include <iostream>
#include <iomanip>
#include "handler.h"
using namespace std;
sem_t Sema1;
sem_t Sema2;
int ThreadsNumber = 2; /* shared variable */
int counter;
vector<std::thread> threadList;
int main()
{
int x[10][10];
int b1[5];
int b2[5];
sem_init(&Sema1, 0, 0); /* initialize Sema1 to Lock - binary semaphore */
sem_init(&Sema2, 0, 2); /* initialize Sema2 to UnLock - binary semaphore */
// Create the threads during the first interations
threadList.push_back(std::thread(handler, x, b1, 0, 0));
threadList.push_back(std::thread(handler, x, b2, 5, 1));
for (int i = 0; i < 10; i++)
{
sem_wait(&Sema2);
sem_wait(&Sema2);
//counter = ThreadsNumber;
// Update the matrix
for (int j = 0; j < 5; j++)
{
b1[j] = j + i;
b2[j] = j + i + 5;
}
for (int i = 0; i < ThreadsNumber; i++)
{
sem_post(&Sema1);
}
}
// Kill all threads in the last iteration
for (auto& threadID : threadList){
threadID.join();
}
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
cout << left << setw(4) << x[i][j];
}
cout << endl;
}
sem_destroy(&Sema1); /* destroy semaphore */
/* exit */
return 0;
} /* main() */
handler.cpp
#include "handler.h"
void handler(int x[10][10], int b[], int start, int id)
{
for (int i = 0; i < 10; i++)
{
sem_wait(&Sema1); /* down semaphore */
for (int j = start; j < 5 + start; j++)
{
x[i][j] = b[j - start];
}
sem_post(&Sema2);
/* END CRITICAL REGION */
}
}
driver.h
#include <iostream>
#include <semaphore.h>
using namespace std;
extern sem_t Sema1;
extern sem_t Sema2;
extern int ThreadsNumber;
/* prototype for thread routine */
void handler(int x[10][10], int b[], int start, int id);