信号量发布到大于 1
Semaphore post to greater than one
如何使信号量的值大于 1。我正在尝试编写一个程序来启动两个线程,并在满足某个条件后让两个线程同时运行。程序正在尝试将大小为 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
我尝试了以下代码:
驱动程序.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() */
处理程序.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] = %dn", i, j, x[i][j]);
}
*/
counter--;
cout << counter << endl;
if (counter == 0)
{
sem_post(&Sema2);
}
/* END CRITICAL REGION */
}
}
处理程序.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);
但它只是效果不佳,我没有得到预期的结果。还有其他方法可以做到这一点吗?
在对信号灯进行了研究之后,我能够修复上面的代码,如下所示:
驱动程序.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() */
处理程序.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 */
}
}
司机.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);
相关文章:
- 删除旧的信号量系统V
- 父进程和子进程之间的 POSIX 信号量
- 访问共享内存而不使用易失性、std::atomic、信号量、互斥锁和自旋锁
- 多线程.如果我使用信号量,我可以在开始时创建很多线程还是应该只有几个线程?
- C/C++ - 用于按顺序打印数字的 sem_t 类型的单个信号量
- 单车道桥 使用信号量进行同步
- 用于 64 位/32 位 IPC 的 POSIX 信号量的替代方案?
- 这个餐饮哲学家问题(dpp)的解决方案是如何工作的?互斥体和信号量
- 发布信号量返回错误 6(无效句柄)
- 在 C Linux 中使用三个线程使用信号量同步按顺序打印 3 4 5 50 次
- 在使用 pthread 和信号量实现生产者-消费者问题时需要帮助
- 如何让一个线程继续,而另一个线程正在等待C++中的信号量
- 实现信号量
- 计算信号量还是互斥体?
- POSIX 信号量在高争用/负载下不起作用
- C++:提升:托管共享内存是否需要信号量锁
- 信号量的问题
- 如何使用Windows API直接将进程"assign"到信号量?
- 从主线程C++更新信号量
- 信号量发布到大于 1