我有一个AppConfig文件,如下所示。我试图循环配置并获取节名称,根据节名称,它应该选择适当的appSettings。例如,当第一部分是VehicleConfig时,它应该自动选择VehicleConfig的appSettings。我需要这个自动选择,因为我有多个部分,我必须根据部分名称获取不同部分的appSettings。
#include <iostream>
#include <queue>
#include <mutex>
#include <condition_variable>
#include <thread>
#include <random>
using namespace std;
// modify this to modify the number of consumer threads
#define WORKERS_THREADS 4
// max size of each of four queues
#define MAX_QUEUE_SIZE 100
// debug
#define DEFAULTCOLOR "\033[0m"
#define RED "\033[22;31m"
#define YELLOW "\033[1;33m"
#define GREEN "\033[0;0;32m"
class MultiQueue {
public:
void initThreadPool(void);
void insert(int num);
void remove(void);
void insertPriorityQueue(int num);
int removePriorityQueue(void);
void printQueues(string what);
int getQueue1Size(void);
int getQueue2Size(void);
int getQueue3Size(void);
int getQueue4Size(void);
int getPrioQueueSize(void);
private:
vector<thread> workers;
queue<int>q1;
queue<int>q2;
queue<int>q3;
queue<int>q4;
priority_queue<int, vector<int>, greater<int>> prioq;
// mutex for push/pop in priority queue
mutex priority_queue_mutex;
// 4 mutexes for each queue
mutex m1, m2, m3, m4;
// mutex for printing 4 queues size
mutex print;
// mutex for push/pop to priority_queue
condition_variable prioq_cond;
// 4 conds for consumer threads
condition_variable w1, w2, w3, w4;
};
int MultiQueue::getQueue1Size() { return q1.size(); }
int MultiQueue::getQueue2Size() { return q2.size(); }
int MultiQueue::getQueue3Size() { return q3.size(); }
int MultiQueue::getQueue4Size() { return q4.size(); }
int MultiQueue::getPrioQueueSize() { return prioq.size(); }
void MultiQueue::initThreadPool(void) {
for (int i=0; i<WORKERS_THREADS; i++) {
workers.push_back(thread(&MultiQueue::remove, this));
workers[i].detach();
}
}
void MultiQueue::printQueues(string what) {
lock_guard<mutex> l(print);
if (what == "insert")
cout << GREEN << '(' << getQueue1Size() << ' ' << getQueue2Size() << ' ' << getQueue3Size() << ' ' << getQueue4Size() << ')' << DEFAULTCOLOR << '\n' << flush;
else
cout << YELLOW << '(' << getQueue1Size() << ' ' << getQueue2Size() << ' ' << getQueue3Size() << ' ' << getQueue4Size() << ')' << DEFAULTCOLOR << '\n' << flush;
}
// called from producer thread to tell consumer threads
// what queues to pop() from
void MultiQueue::insertPriorityQueue(int num) {
lock_guard<mutex> prio(priority_queue_mutex);
prioq.push(num);
prioq_cond.notify_one();
}
// called from consumer threads to see what queues
// have elements to pop() from
int MultiQueue::removePriorityQueue(void) {
int ret = 0;
unique_lock<mutex> prio(priority_queue_mutex);
prioq_cond.wait(prio, [this] () { return getPrioQueueSize() > 0; });
ret = prioq.top();
prioq.pop();
return ret;
}
// producer thread
void MultiQueue::insert(int num) {
switch(num) {
case 1: {
unique_lock<mutex> locker(m1);
w1.wait(locker, [this] () { return getQueue1Size() < MAX_QUEUE_SIZE; });
q1.push(num);
break;
}
case 2: {
unique_lock<mutex> locker(m2);
w2.wait(locker, [this] () { return getQueue2Size() < MAX_QUEUE_SIZE; });
q2.push(num);
break;
}
case 3: {
unique_lock<mutex> locker(m3);
w3.wait(locker, [this] () { return getQueue3Size() < MAX_QUEUE_SIZE; });
q3.push(num);
break;
}
case 4: {
unique_lock<mutex> locker(m4);
w4.wait(locker, [this] () { return getQueue4Size() < MAX_QUEUE_SIZE; });
q4.push(num);
break;
}
default: {
cout << "number not 1, 2, 3 nor 4: " << num << '\n' << flush;
break;
}
}
printQueues("insert");
insertPriorityQueue(num);
}
void MultiQueue::remove(void) {
int which_queue = 0;
while (true) {
which_queue = removePriorityQueue();
switch (which_queue) {
case 1: {
lock_guard<mutex> lock(m1);
int ret = q1.front();
q1.pop();
printQueues("remove");
break;
}
case 2: {
lock_guard<mutex> lock(m2);
int ret = q2.front();
q2.pop();
printQueues("remove");
break;
}
case 3: {
lock_guard<mutex> lock(m3);
int ret = q3.front();
q3.pop();
printQueues("remove");
break;
}
case 4: {
lock_guard<mutex> lock(m4);
int ret = q4.front();
q4.pop();
printQueues("remove");
break;
}
default: {
break;
}
}
}
}
int main(void) {
int random_num = 0;
MultiQueue mq;
mq.initThreadPool();
default_random_engine eng((random_device())());
uniform_int_distribution<int> idis(1, 4);
while (true) {
random_num = idis(eng);
mq.insert(random_num);
}
return 0;
}
我尝试过这段代码,当第二个for-each循环被点击时,它会抛出错误“VehicleConfig中无法识别的元素appSettings”。我尝试删除appSettings,但随后抛出“无法识别的元素添加”。我想知道我是否可以在VehicleConfig中拥有这些元素。
<configuration>
<configSections>
<sectionGroup name="group1">
<section name="Vehiclefeature"type="System.Configuration.NameValueSectionHandler" />
<section name="LiveDownloader" type="System.Configuration.NameValueSectionHandler" />
</sectionGroup>
</configSections>
<VehicleFeature>
<add key="FileRoot" value="C:\FilesToUpload" />
<add key="Project" value="BigDataTest" />
<add key="Dataset" value="StoreServer" />
</VehicleFeature>
<LiveDownloader>
<add key="FileRoot" value="C:\FilesToUpload" />
<add key="Project" value="BigDataTest" />
<add key="Dataset" value="BQSMeasure" />
</LiveDownloader>
</configuration>
任何帮助表示赞赏!!
答案 0 :(得分:0)
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
// Get the collection of the section groups.
ConfigurationSectionGroupCollection sectionGroups = config.SectionGroups;
// Show the configuration values
foreach (ConfigurationSectionGroup group in sectionGroups)
// Loop over all groups
{
if (group.Name == "group1")
{
foreach (ConfigurationSection configurationSection in group.Sections)
{
var section1 = ConfigurationManager.GetSection(configurationSection.SectionInformation.SectionName) as NameValueCollection;
}
}
}