信号量发布到大于 1

Semaphore post to greater than one

本文关键字:大于 信号量      更新时间:2023-10-16

如何使信号量的值大于 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);