如何使Semaphore的值大于1。我正在尝试编写一个启动两个线程的程序,并在满足某个条件后让两个线程同时运行。 该程序试图将大小为5的两个数组(b1和b2)复制到10 * 10阵列。每次迭代,b1的值都被复制到x [0] [i]到x [4] [i],而b2的值被复制到x [5] [i]到x [9] [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);
但它只是效果不好而且我没有得到预期的结果。还有其他方法吗?
答案 0 :(得分:1)
通过对信号量的研究后,我能够修复上面的代码如下:
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);