【问题标题】:'cpu_set_t' does not name a type C++'cpu_set_t' 没有命名类型 C++
【发布时间】:2023-03-06 08:07:02
【问题描述】:

我正在为分配构建速率单调调度程序,我需要让所有线程在同一个处理器上运行。我不确定我做错了什么,但我不断收到同样的错误(见下图)。我一直在查看一堆 Linux 文档,试图弄清楚该怎么做,但我就是做不到。我真的很感激一些帮助。提前致谢。

我的 Scheduler.cpp 文件:

#define _GNU_SOURCE
#include <iostream>
#include <pthread.h>
#include <unistd.h>
#include <mutex>
#include <condition_variable>
#include <semaphore.h>
#include <sched.h>
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>

using namespace std;

sem_t semaphore;
sem_t mutex1;
sem_t mutex2;
sem_t mutex3;
sem_t mutex4;

// initialze variables
int i = 0;
int overrun1 = 0;
int overrun2 = 0;
int overrun3 = 0;
int overrun4 = 0;

int doWork();
void* p1(void *param);
void* p2(void *param);
void* p3(void *param);
void* p4(void *param);



int main(int argc, char const *argv[])
{
cpu_set_t cpus;

CPU_ZERO(&cpus);
CPU_SET(1, &cpus);

sem_init(&mutex1, 0, 0);
sem_init(&mutex2, 0, 0);
sem_init(&mutex3, 0, 0);
sem_init(&mutex4, 0, 0);

// initialze all threads
pthread_t thread1;
pthread_t thread2;
pthread_t thread3;
pthread_t thread4;

// actually create all threads
pthread_create(&thread1, NULL, p1, NULL);
pthread_create(&thread2, NULL, p2, NULL);
pthread_create(&thread3, NULL, p3, NULL);
pthread_create(&thread4, NULL, p4, NULL);

while (i < 160)
{
    if (i == 0) // initial case.  at time 0 schedule all threads
    {
        sem_post(&mutex1);
        sem_post(&mutex2);
        sem_post(&mutex3);
        sem_post(&mutex4);
    }

    else if (i % 16 == 0) // last case which happens every 16 units which schedules all threads again
    {
        sem_post(&mutex1);
        sem_post(&mutex2);
        sem_post(&mutex3);
        sem_post(&mutex4);
    }

    else if (i % 4 == 0) // case that happens every 4 units of time
    {
        sem_post(&mutex1);
        sem_post(&mutex2);
        sem_post(&mutex3);
    }

    else if (i % 2 == 0) // case that happens every other unit of time
    {
        sem_post(&mutex1);
        sem_post(&mutex2);
    }

    else if (i % 1 == 0) // case that happens every unit of time
    {
        sem_post(&mutex1);
    }
    i++; // increment i to go through the loop again
}

// join all threads back to the main one
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
pthread_join(thread3, NULL);
pthread_join(thread4, NULL);

return 0;
}

// doWork function

int doWork()
{
int lousyArray[10][10];
int product = 1;

for (int i = 0; i < 10; i++)
{
    for (int j = 0; j < 10; j++)
    {
        lousyArray[i][j] = 1;
    }
}
for (int k = 0; k < 1; k++)
{
    for (int j = 0; j < 10; j++)
    {
        for (int i = 0; i < 10; i++)
        {
            product *= lousyArray[i][j];
        }
    }
}
return 1;
}


void* p1(void *param)
{
    bool thread1FinishFlag = false;
    while(1)
    {
        sem_wait(&mutex1);
        thread1FinishFlag = false;
        for (int i = 0; i < 1; i++)
        {
            doWork();
        }
        //increment counter here
        thread1FinishFlag = true;
    }
}
void* p2(void *param)
{
    bool thread2FinishFlag = false;
    while(1)
    {
        sem_wait(&mutex2);
        thread2FinishFlag = false;
        for (int i = 0; i < 2; i++)
        {
            doWork();
        }
        //increment counter here
        thread2FinishFlag = true;
    }
}
void* p3(void *param)
{
    bool thread3FinishFlag = false;
    while(1)
    {
        sem_wait(&mutex3);
        thread3FinishFlag = false;
        for (int i = 0; i < 4; i++)
        {
            doWork();
        }
        //increment counter here
        thread3FinishFlag = true;
    }
}
void* p4(void *param)
{
    bool thread4FinishFlag = false;
    while(1)
    {
        sem_wait(&mutex4);
        thread4FinishFlag = false;
        for (int i = 0; i < 16; i++)
        {
            doWork();
        }
        //increment counter here
        thread4FinishFlag = true;
    }
}

void nsleep()
{
    struct timespec delay;

    delay.tv_sec = 0;
    delay.tv_sec = 100000000L;
    nanosleep(&delay, NULL);
}

【问题讨论】:

  • edit您的问题提供minimal reproducible example
  • @Barmar 不确定你是不是在开玩笑,但无论如何:发布包含一些错误的整个源文件几乎不是最小
  • @BaummitAugen 我相信你明白,发帖人并不总是知道哪些部分是重要的,所以他们很难只缩小到那个部分。更严重的问题是,当他们发布的代码不完整或无法验证时,他并没有犯这个错误。
  • @Barmar 在这种情况下,我们谈论的是编译错误。编译器指向有问题的确切行。做出比“仅仅转储所有代码”更简单的努力可以而且必须得到提问者的期望,即使它“很难”。
  • @BaummitAugen 来吧,你知道事情没那么简单。错误消息中未提及的行可能存在各种依赖关系。

标签: c++ g++ scheduler cpuset


【解决方案1】:

致所有回复此帖子的人:感谢您的快速回复。首先,我可能应该提到我正在使用 Cygwin for Windows 来编译这个程序。其次,我为没有缩短帖子而道歉。最后,在询问了一位同事后,我发现 Cygwin 本身并不支持在代码本身中设置处理器亲和性。

谢谢

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 2015-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多