信号量不工作 C++
Semaphore not working C++
我是信号量的新手,正在尝试编写一个非常基本的示例以更好地学习它。我的代码包含三个数组:
b1[5]={1;2;3;4;5}
b2[5]={6;7;8;9;10}
x[10]
我有一个函数接受 b1 或 b2 并更新 x 使其如下:
x={0;1;2;3;4;5;6;7;8;9}
我的代码如下:
#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 <Windows.h> /* windows */
using namespace std;
/* prototype for thread routine */
void handler(int x[], int b[], int start, int id);
/* global vars */
sem_t mutex;
vector<std::thread> threadList;
int main()
{
int i[2];
int x[10];
int b1[5];
int b2[5];
sem_init(&mutex, 0, 1); /* initialize mutex to 1 - binary semaphore */
/* second param = 0 - semaphore is local */
i[0] = 0;
i[1] = 1;
// Update the matrix
for (int j = 0; j < 5; j++)
{
b1[j] = j;
b2[j] = j+5;
}
threadList.push_back(std::thread(handler, x, b1, 0, 0));
threadList.push_back(std::thread(handler, x, b2, 5, 1));
// wait for all threads to finish
for (auto& threadID : threadList){
threadID.join();
}
sem_destroy(&mutex); /* destroy semaphore */
/* exit */
return 0;
} /* main() */
void handler(int x[], int b[], int start, int id)
{
for (int j = start; j < 5; j++)
{
x[j] = b[j];
}
printf("Thread %d: Waiting to print results...\n", id);
sem_wait(&mutex); /* down semaphore */
/* START CRITICAL REGION */
for (int j = start; j < 5; j++)
{
printf("x[%d] = %d\n", j , x[j]);
}
/* END CRITICAL REGION */
sem_post(&mutex); /* up semaphore */
}
代码的输出如下:
Thread 0: Waiting to print results...
x[0] = 0
x[1] = 1
x[2] = 2
x[3] = 3
x[4] = 4
Thread 1: Waiting to print results...
然而,我期待的是以下内容
Thread 0: Waiting to print results...
x[0] = 0
x[1] = 1
x[2] = 2
x[3] = 3
x[4] = 4
Thread 1: Waiting to print results...
x[5] = 5
x[6] = 6
x[7] = 7
x[8] = 8
x[9] = 9
知道为什么第二个线程没有进入代码的打印部分吗?
您对 'handler' 的第二次调用开始=5。
循环
for (int j = start; j< 5; j++)
两个地方都没有执行,因为j >= 5.
我是信号量的新手,正在尝试编写一个非常基本的示例以更好地学习它。我的代码包含三个数组:
b1[5]={1;2;3;4;5}
b2[5]={6;7;8;9;10}
x[10]
我有一个函数接受 b1 或 b2 并更新 x 使其如下:
x={0;1;2;3;4;5;6;7;8;9}
我的代码如下:
#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 <Windows.h> /* windows */
using namespace std;
/* prototype for thread routine */
void handler(int x[], int b[], int start, int id);
/* global vars */
sem_t mutex;
vector<std::thread> threadList;
int main()
{
int i[2];
int x[10];
int b1[5];
int b2[5];
sem_init(&mutex, 0, 1); /* initialize mutex to 1 - binary semaphore */
/* second param = 0 - semaphore is local */
i[0] = 0;
i[1] = 1;
// Update the matrix
for (int j = 0; j < 5; j++)
{
b1[j] = j;
b2[j] = j+5;
}
threadList.push_back(std::thread(handler, x, b1, 0, 0));
threadList.push_back(std::thread(handler, x, b2, 5, 1));
// wait for all threads to finish
for (auto& threadID : threadList){
threadID.join();
}
sem_destroy(&mutex); /* destroy semaphore */
/* exit */
return 0;
} /* main() */
void handler(int x[], int b[], int start, int id)
{
for (int j = start; j < 5; j++)
{
x[j] = b[j];
}
printf("Thread %d: Waiting to print results...\n", id);
sem_wait(&mutex); /* down semaphore */
/* START CRITICAL REGION */
for (int j = start; j < 5; j++)
{
printf("x[%d] = %d\n", j , x[j]);
}
/* END CRITICAL REGION */
sem_post(&mutex); /* up semaphore */
}
代码的输出如下:
Thread 0: Waiting to print results...
x[0] = 0
x[1] = 1
x[2] = 2
x[3] = 3
x[4] = 4
Thread 1: Waiting to print results...
然而,我期待的是以下内容
Thread 0: Waiting to print results...
x[0] = 0
x[1] = 1
x[2] = 2
x[3] = 3
x[4] = 4
Thread 1: Waiting to print results...
x[5] = 5
x[6] = 6
x[7] = 7
x[8] = 8
x[9] = 9
知道为什么第二个线程没有进入代码的打印部分吗?
您对 'handler' 的第二次调用开始=5。
循环
for (int j = start; j< 5; j++)
两个地方都没有执行,因为j >= 5.