C++ 将信号量post设置为大于1
如何使信号量的值大于1。我试图编写一个程序,启动两个线程,并在满足特定条件后使两个线程同时运行。 该程序正在尝试将两个大小为5的阵列(b1和b2)复制到一个10*10的阵列。每次迭代,b1的值被复制到x[0][i]到x[4][i],b2的值被复制到x[5][i]到x[9][i](其中i是迭代索引) 最后的结果应该如下所示:C++ 将信号量post设置为大于1,c++,multithreading,semaphore,C++,Multithreading,Semaphore,如何使信号量的值大于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
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() */
#包含/*基本系统数据类型*/
#包括/*错误*/
#包括/*输入/输出*/
#包括/*一般公用设施*/
#包括/*C++11个线程*/
#include/*字符串处理*/
#include/*信号量*/
#包含/*向量*/
#包括
#包括
#包括“handler.h”
使用名称空间std;
sem_t Sema1;
sem_t Sema2;
int ThreadsNumber=2;/*共享变量*/
整数计数器;
向量线程列表;
int main()
{
int x[10][10];
int b1[5];
int b2[5];
sem_init(&Sema1,0,2);/*初始化Sema1以锁定二进制信号量*/
sem_init(&Sema2,0,1);/*初始化Sema2以解锁-二进制信号量*/
对于(int i=0;i<10;i++)
{
sem_wait(&Sema2);
计数器=线程数;
//更新矩阵
对于(int j=0;j<5;j++)
{
b1[j]=j+i;
b2[j]=j+i+5;
}
sem_init(&Sema1,0,2);
如果(i==0)
{
//在第一次交互期间创建线程
threadList.push_back(std::thread(handler,x,b1,0,0));
threadList.push_back(std::thread(handler,x,b2,5,1));
}
//在上一次迭代中杀死所有线程
如果(i==9)
{
用于(自动和线程ID:threadList){
threadID.join();
}
}
}
对于(int i=0;i<10;i++)
{
对于(int j=0;j<10;j++)
{
cout通过对信号量的研究,我能够修复上述代码,如下所示:
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() */
#包含/*基本系统数据类型*/
#包括/*错误*/
#包括/*输入/输出*/
#包括/*一般公用设施*/
#包括/*C++11个线程*/
#include/*字符串处理*/
#include/*信号量*/
#包含/*向量*/
#包括
#包括
#包括“handler.h”
使用名称空间std;
sem_t Sema1;
sem_t Sema2;
int ThreadsNumber=2;/*共享变量*/
整数计数器;
向量线程列表;
int main()
{
int x[10][10];
int b1[5];
int b2[5];
sem_init(&Sema1,0,0);/*初始化Sema1以锁定二进制信号量*/
sem_init(&Sema2,0,2);/*初始化Sema2以解锁-二进制信号量*/
//在第一次交互期间创建线程
threadList.push_back(std::thread(handler,x,b1,0,0));
threadList.push_back(std::thread(handler,x,b2,5,1));
对于(int i=0;i<10;i++)
{
sem_wait(&Sema2);
sem_wait(&Sema2);
//计数器=线程数;
//更新矩阵
对于(int j=0;j<5;j++)
{
b1[j]=j+i;
b2[j]=j+i+5;
}
对于(int i=0;i cout-well允许使用不同于0
的值初始化信号量。不清楚什么实际不适合您的使用。您必须调用sem\u wait()
至少两次,将信号量从初始状态解锁。请参见上面编辑的问题。您没有真正添加关键信息:什么在您的使用中实际不起作用?一个线程在将信号量设置为2时运行,另一个线程不起作用。因此,x数组的值与预期值不符.由于您一直在寻求其他方法,我建议您看看,这对我来说似乎最接近。
#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);
#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() */
#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 */
}
}
#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);