一个主要是为了好玩/好奇的问题:如何for在C++中编写循环,迭代两个a bool(ie true和false)的值,只使用操作bool(即没有转换到其他类型)?
背景是我想检查一个等式有多少解决方案存在(A && B) || (!B && !C && !D) == true,并开始编写类似的东西,for (bool A=false; ??? ; ++A) for (bool B=false; ...)但立即被卡住???- 即继续循环的条件是什么?当然我把它重写为使用int,我也知道do ... while循环会起作用,但是如果有可能编写这样的for循环我很好奇吗?既然SO似乎没有答案,我决定问:)
更新:请注意,for(bool A=false; !A; A=true)在至少两个现在删除的答案中建议的"明显"变体将仅运行一次迭代,因为对于第二次迭代,条件!A变为false循环结束.
经过一番思考之后,我相信如果没有第二个变量或者像DietmarKühl建议的基于指针的构造,在C++ 03中这样做是不可能的.条件应该在期望的执行中测试三次,因此bool的两个值是不够的.do-while循环有效,因为第一次迭代是无条件执行的,条件只检查两次,因此可以使用bool值在继续和退出之间进行选择.
我正在尝试编写一个简单的共享库,它可以将malloc调用记录到stderr(如果你愿意,可以使用某种'mtrace').
但是,这不起作用.这是我做的:
/* mtrace.c */
#include <dlfcn.h>
#include <stdio.h>
static void* (*real_malloc)(size_t);
void *malloc(size_t size)
{
void *p = NULL;
fprintf(stderr, "malloc(%d) = ", size);
p = real_malloc(size);
fprintf(stderr, "%p\n", p);
return p;
}
static void __mtrace_init(void) __attribute__((constructor));
static void __mtrace_init(void)
{
void *handle = NULL;
handle = dlopen("libc.so.6", RTLD_LAZY);
if (NULL == handle) {
fprintf(stderr, "Error in `dlopen`: %s\n", dlerror());
return;
}
real_malloc = dlsym(handle, "malloc");
if (NULL == real_malloc) {
fprintf(stderr, "Error in `dlsym`: %s\n", dlerror());
return;
} …Run Code Online (Sandbox Code Playgroud) 我想替换我的NSIS安装程序中的默认徽标(见下图),但我在互联网上找不到任何相关内容.可以吗?

在多处理器编程的艺术中,rev.第1版,在Ch.2,练习9如下(转述):
限定R-界定等待互斥算法意味着d 甲Ĵ ➝d 乙ķ ⇒CS 甲Ĵ ➝CS 乙ķ+ R.有没有办法为Peterson算法定义一个门口,以便提供r-bounded等待?
本书使用➝来定义事件优先顺序的总顺序,其中X➝Y表示事件X在Y开始之前开始并完成.D A是线程A的"门口"事件,它是请求进入关键部分的事件.CS A是线程A的关键部分事件.
对于任何事件X A,X A i是线程A上事件X的第i次执行.
现在回答这个问题:在我看来,Peterson算法是完全公平的(0-有限等待).此外,我认为r-bounded等待意味着k-bounded等待所有k> r.然后这个问题没有意义,因为彼得森应该满足r-bounded等待所有r.
问题是要求Peterson算法的"简化",因为它要求放宽约束?
这是自学,而不是作业.
Peterson锁算法的代码取自本书:
1 class Peterson implements Lock {
2 // thread-local index, 0 or 1
3 private volatile boolean[] flag = new boolean[2];
4 private volatile int victim;
5 public void lock() {
6 int i = ThreadID.get();
7 int j = 1 - i;
8 flag[i] = true; …Run Code Online (Sandbox Code Playgroud) 在C++ 11标准的第23.3.6.2节[vector.cons]中,如下所述:
template <class InputIterator>
vector(InputIterator first, InputIterator last,
const Allocator& = Allocator());
Run Code Online (Sandbox Code Playgroud)
9 效果:
[first,last)使用指定的分配器构造一个等于范围的向量.
10 复杂性:使n只有调用T(其中,N是之间的距离的拷贝构造first和last),并且没有重新分配,如果迭代的第一和最后被正向,双向或随机接入类别.它使命令N调用T的复制构造函数和命令log(N)重新分配,如果它们只是输入迭代器.
(此文本也存在于较旧的标准中).一方面,它不要求解除引用InputIterator应该导致存储在向量中的相同类型的值.另一方面,它讲述了使用复制构造函数,这种类型暗示了相同的类型.
我的问题是:如果可以在类型之间进行转换,那么使用这个构造函数的不同类型的元素序列是否有效?需要参考该标准.
例如,以下代码在ideone上运行正常.它是由标准保证,还是GCC恰好允许它?
#include <vector>
#include <iostream>
struct A {
int n;
A(int n_) : n(n_) {}
};
int main() {
int arr[] = {1,2,3,4,5,6,7,8,9,10};
std::vector<int> int_vec(arr, arr+10);
std::vector<A> A_vec(int_vec.begin(), int_vec.end());
for( std::vector<A>::iterator it=A_vec.begin(); it!=A_vec.end(); ++it )
std::cout<< it->n <<" ";
std::cout<<std::endl;
}
Run Code Online (Sandbox Code Playgroud) 我在弄清楚pthread_key_t和pthread_key_create如何工作时遇到了一些麻烦.据我所知,每个线程都有TLS(线程本地存储),并且一个密钥用于访问线程本地存储.我没有得到的是当创建一个密钥时,每个线程都可以使用它吗?让我们说线程0创建密钥0,线程1可以使用密钥0吗?如果线程1使用键0,它是否会访问自己的TLS或线程0的TLS?
是否有一些全局数组或某些东西可以跟踪所使用的所有密钥?
我在多线程应用程序中使用std :: vector作为共享数据.我将线程封装在一个类中,例如,
class ABC {
public:
double a, b, c;
};
boost::mutex mutex1;
class XYZ {
public:
XYZ(vector<ABC> & pVector) {
ptrVector = &pVector;
m_thread = boost::thread(&XYZ::Start, this);
}
~XYZ() {}
void Start();
public:
vector<ABC> * ptrVector;
boost::thread m_thread;
};
void XYZ::Start() {
try {
while(1) {
boost::this_thread::interruption_point();
for (unsigned int i=0; i<ptrVector->size(); i++) {
{
boost::mutex::scoped_lock lock(mutex1);
ptrVector->at(i).a = double(rand())/10000;
ptrVector->at(i).b = double(rand())/10000;
ptrVector->at(i).c = double(rand())/10000;
}
}
}
}
catch(boost::thread_interrupted) {}
catch(std::exception) {}
}
Run Code Online (Sandbox Code Playgroud)
当我关闭应用程序时,有时候,在调试中会有2条错误消息, …
我有两个包含文件头
#include "stdafx.h"
#include "psapi.h"
Run Code Online (Sandbox Code Playgroud)
但是它给出了一个无法打开源文件"stdafx.h"的编译时错误.我正在使用Visual Studios 2010."stdafx.h"甚至是必要的吗?我是这么认为的,因为如果把它带走,程序就无法编译.
我在tomcat中部署了一个Web应用程序.当我在浏览器中打开应用程序时,地址栏中URL之前的图像和选项卡上应用程序标题之前的图像都显示tomcat图像.我想改变这个并在两个地方放入我自己的徽标.我怎样才能做到这一点?
我有以下"常量"标题:
/* constants.h */
#ifdef __cplusplus
extern "C" {
#endif
#pragma once
#ifndef CONSTANTS_H
#define CONSTANTS_H
const char * kFoo = "foo";
const char * kBar = "bar";
#endif
#ifdef __cplusplus
}
#endif
Run Code Online (Sandbox Code Playgroud)
我是#include在文件中的这个标题X.c和Y.c.
请注意,我没有将此包含在X.h或中Y.h.
这些文件X.c和Y.c编译到被归档到一个名为静态库的目标文件libXY.a.
当我包含X.h和Y.h中Z.h,当我链接到libXY.a,我无法编译Z.c没有错误:
/* Z.h */
#include "X.h"
#include "Y.h"
Run Code Online (Sandbox Code Playgroud)
尝试编译时出现以下编译错误Z.c:
/path/to/libXY.a(X.o):(.data+0x0): multiple definition of `kFoo` …Run Code Online (Sandbox Code Playgroud) 我想使用TBB获得线程池行为.但每当我阅读有关TBB的文档时,他们总是谈论并行,并行 - 等等.相比之下,我需要的是一个主线程,用于将任务分配给线程池,以便这些任务将"自己"执行 - 执行任务异步.这里的任务可以是GUI的事件处理.
TBB任务调度程序是否适合此类行为?我从任务调度程序得到的印象是,如果我有可以分解并相互并行执行的任务,那将是有益的.
我试图仅使用互斥锁实现读/写锁定(仅用于学习).就在我认为我已经覆盖所有角落的情况下(因为程序使用各种组合),我已经意识到,我忽略了事实(因为它在ubuntu中工作),互斥体应该由线程的所有者释放.以下是我的实施,
class rw_lock_t{
int NoOfReaders;
int NoOfWriters, NoOfWritersWaiting;
pthread_mutex_t class_mutex;
pthread_cond_t class_cond;
pthread_mutex_t data_mutex;
public:
rw_lock_t()
: NoOfReaders(0),
NoOfWriters(0), NoOfWritersWaiting(0)
{
pthread_mutex_init(&class_mutex, NULL);
pthread_mutex_init(&data_mutex, NULL);
pthread_cond_init(&class_cond, NULL);
}
void r_lock()
{
pthread_mutex_lock(&class_mutex);
//while(NoOfWriters!=0 || NoOfWritersWaiting!=0) //Writer Preference
while(NoOfWriters!=0)
{
pthread_cond_wait(&class_cond, &class_mutex);
}
if(NoOfReaders==0)
{
pthread_mutex_unlock(&class_mutex);
pthread_mutex_lock(&data_mutex);
pthread_mutex_lock(&class_mutex);
NoOfReaders++;
pthread_mutex_unlock(&class_mutex);
}
else if(NoOfReaders>0) //Already Locked
{
NoOfReaders++;
pthread_mutex_unlock(&class_mutex);
}
}
void w_lock()
{
pthread_mutex_lock(&class_mutex);
NoOfWritersWaiting++;
while(NoOfReaders!=0 && NoOfWriters!=0)
{
pthread_cond_wait(&class_cond, &class_mutex);
}
pthread_mutex_unlock(&class_mutex);
pthread_mutex_lock(&data_mutex);
pthread_mutex_lock(&class_mutex);
NoOfWritersWaiting--; NoOfWriters++;
pthread_mutex_unlock(&class_mutex);
}
void r_unlock() …Run Code Online (Sandbox Code Playgroud) 我试图了解线程的工作原理.我有一些学校的例子.在这一个我必须弄清楚为什么这段代码不能正常工作.它的输出是这样的:
Main: Creating thread 0
Main: Creating thread 1
Main: Creating thread 2
Main: Creating thread 3
Main: Creating thread 4
Main: Creating thread 5
Main: Creating thread 6
Main: Creating thread 7
Main: Creating thread 8
Thread 0: English: Hello World!
Thread 0: English: Hello World!
Thread 0: English: Hello World!
Thread 0: English: Hello World!
Thread 0: English: Hello World!
Thread 0: English: Hello World!
Thread 0: English: Hello World!
Thread 0: English: Hello World!
Thread 0: English: …Run Code Online (Sandbox Code Playgroud) c++ ×5
c ×3
pthreads ×3
asynchronous ×1
boolean ×1
concurrency ×1
definition ×1
for-loop ×1
header ×1
installer ×1
malloc ×1
mutex ×1
nsis ×1
posix ×1
stl ×1
tbb ×1
thread-local ×1
threadpool ×1
tomcat ×1
vector ×1
visual-c++ ×1