我正在学习boost.thread的基础知识.到目前为止,我可以手动逐个创建每个线程,让它们同时运行.但是,在通过循环创建时,它会依次运行而不是并发运行.
#include <iostream>
#include <boost/thread.hpp>
void workerFunc()
{
boost::posix_time::seconds workTime(3);
std::cout << "Worker: Running" << '\n';
boost::this_thread::sleep(workTime);
std::cout<< "Worker: Finished" << '\n';
}
int main()
{
std::cout << "main: startup" << '\n';
boost::thread workerThread(workerFunc);
std::cout << "main: waiting for thread" << '\n';
//these are ok
boost::thread t(workerFunc), t2(workerFunc), t3(workerFunc), t4(workerFunc);
t.join();
t2.join();
t3.join();
t4.join();
//these are not
for (int i = 0; i < 2; ++i)
{
boost::thread z(workerFunc);
z.join();
}
std::cout << "main:done" << '\n';
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试一个例子,它会导致竞争条件应用互斥锁.但是,即使使用互斥锁,它仍然会发生.怎么了?这是我的代码:
#include <iostream>
#include <boost/thread.hpp>
#include <vector>
using namespace std;
class Soldier
{
private:
boost::thread m_Thread;
public:
static int count , moneySpent;
static boost::mutex soldierMutex;
Soldier(){}
void start(int cost)
{
m_Thread = boost::thread(&Soldier::process, this,cost);
}
void process(int cost)
{
{
boost::mutex::scoped_lock lock(soldierMutex);
//soldierMutex.lock();
int tmp = count;
++tmp;
count = tmp;
tmp = moneySpent;
tmp += cost;
moneySpent = tmp;
// soldierMutex.unlock();
}
}
void join()
{
m_Thread.join();
}
};
int Soldier::count, Soldier::moneySpent;
boost::mutex Soldier::soldierMutex;
int main()
{
Soldier s1,s2,s3; …
Run Code Online (Sandbox Code Playgroud) 根据这篇文章民意调查 vs 选择 vs 基于事件:
select() 每个文件描述符仅使用(最多)3 位数据,而 poll() 通常每个文件描述符使用 64 位数据。因此,在每个系统调用中调用 poll() 都需要将更多内容复制到内核空间。select() 的一个小胜利。
这是fd_set的实现(在Advisories上找到:多个应用程序fd_set结构位图数组索引溢出
#ifndef FD_SETSIZE
#define FD_SETSIZE 1024
#endif
#define NBBY 8 /* number of bits in a byte */
typedef long fd_mask;
#define NFDBITS (sizeof (fd_mask) * NBBY) /* bits per mask */
#define howmany(x,y) (((x)+((y)-1))/(y))
typedef struct _types_fd_set {
fd_mask fds_bits[howmany(FD_SETSIZE, NFDBITS)];
} _types_fd_set;
#define fd_set _types_fd_set
Run Code Online (Sandbox Code Playgroud)
所以,最终,fd_set只是一个long
. 书中还写道:
对 FD_SET 的调用使用套接字号作为索引将一位设置为 1:
这意味着,如果我得到一个套接字 fd 编号为 5,则索引为 5 的元素将被选择,并且其第一位将从 0 …
如何加载另一个Lisp文件而不必指定完整路径?我尝试过(load /path/to/file)
,但似乎只能使用绝对路径.
我知道,对许多Lispers来说,使用Eclipse似乎并不合适.我使用Emacs进行C++编程和shell脚本编写,但是,我不想花时间阅读SLIME的50页手册.我稍后会学习它,但是现在我只想学习语言而不会有太多麻烦.
在C中,我们将代码分成两个实体:客户端接口和实现接口所需工作的实现.接口放入.h
并实现放入.c
.如果我们只想公开接口而不是源代码,这很方便,而且我们不必依赖文档来查找接口,这可以通过接口来执行.
我如何用Lisp实现同样的目标?
根据文档,我们可以执行jmp
一个恒定的远段:
jmp 0x18:00
Run Code Online (Sandbox Code Playgroud)
这里,0x18
是GDT全局描述符表中的有效段选择器.
jmp
可以与包含有效GDT条目的段寄存器一起使用,该条目是代码/数据段描述符:
mov es, 0x18
jmp es:0x0
Run Code Online (Sandbox Code Playgroud)
这里0x18
是TSS(任务状态段)描述符,当跳转到时,CPU执行任务切换,自动将其状态保存到当前TSS中,然后填充新TSS中保存的状态.
但是,TSS是系统段描述符,因此无法加载到任何段寄存器中(如英特尔文档所建议).那么,如何在运行时使用动态分配的TSS跳转到任务?
我能想到的唯一方法是使用该iret
指令,但我觉得它像一个hack,因为我需要修改链接字段,然后在EFLAGS中设置NT位以执行反向链接任务切换.
我正在学习C++中的Windows编程.我创建了我的第一个Windows,但有一点我不太了解:WNDCLASS中的WNDPROC.结构记录如下:
typedef struct tagWNDCLASS {
UINT style;
WNDPROC lpfnWndProc;
int cbClsExtra;
int cbWndExtra;
HINSTANCE hInstance;
HICON hIcon;
HCURSOR hCursor;
HBRUSH hbrBackground;
LPCTSTR lpszMenuName;
LPCTSTR lpszClassName;
} WNDCLASS, *PWNDCLASS;
Run Code Online (Sandbox Code Playgroud)
现在为了分配给lpfnWndProc,我必须有一个像这样的回调函数WindowProc:
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
Run Code Online (Sandbox Code Playgroud)
然后我必须像这样分配lfpnWndProc:
WNDCLASS wc = { };
wc.lpfnWndProc = WindowProc;
Run Code Online (Sandbox Code Playgroud)
据我了解,WindowProc是一个功能.如果我像这样分配,这意味着我正在分配WNDCLASS中的函数指针.但是在WNDCLASS定义中,没有任何东西表明它是一个函数指针.此外,它看起来像我的数据类型而不是函数指针.
通常我会得到这样的函数指针作为参数传入或用作变量:
#include <stdio.h>
void my_int_func(int x)
{
printf("%d\n",x);
}
int main(void)
{
void (*foo) (int);
foo = &my_int_func;
foo(2);
(*foo)(2);
}
Run Code Online (Sandbox Code Playgroud)
但我必须分配WindowProc的方式对我来说没有意义.有人能帮助我理解这个吗?
我正在做一个小游戏.
在BattleRecord.h中:
#ifndef _CHARACTER_H_
#define _CHARACTER_H_
#include "Character.h"
#endif
class BattleRecord
{
public:
Character Attacker;
Character Defender;
Status status;
int DamageDealt;
int GoldEarned;
int ExpGained;
};
Run Code Online (Sandbox Code Playgroud)
在Character.h中:
#ifndef _EQUIPMENT_H_
#define _EQUIPMENT_H_
#include "Equipment.h"
#endif
class BattleRecord;
class Character
{
BattleRecord AttackEnemy(Character &Enemy);
}
Run Code Online (Sandbox Code Playgroud)
在BattleRecord.h中:
#ifndef _CHARACTER_H_
#define _CHARACTEr_H_
#include "Character.h"
#endif
#ifndef _BATLE_RECORD_H_
#define _BATLE_RECORD_H_
#include "BattleRecord.h"
#endif
class GUI
{
public:
//GUI Methods, and two of these:
void ViewStats(Character &Player);
void Report(BattleRecord Record)
}
Run Code Online (Sandbox Code Playgroud)
这里的问题是,我的Character.h和BattleRecord.h需要相互包含,这肯定会导致多重重新定义问题.因此,我在Character.h中使用了前向声明,添加:
class BattleRecord;
Run Code Online (Sandbox Code Playgroud)
这个问题已经解决了.但是,GUI.h再次需要BattleRecord.h来报告战斗,因此我必须将BattleRecord.h包含在GUI.h中.我还必须包含Character.h才能传入ViewStat函数.我得到了错误并坚持到这个piont.
为什么我们需要Boost库中指定的泛型类型?模板不够吗?例如,如果我想要一个特定类型的容器,我会这样做:
template<class Type>
vector<Type> v;
Run Code Online (Sandbox Code Playgroud)
如果我想指定一个包含所有内容的容器,我只需写:
vector v;
Run Code Online (Sandbox Code Playgroud)
解释为boost :: any(http://www.boost.org/doc/libs/1_46_1/doc/html/any/s02.html)
转换可以包含多种可能值类型之一的类型,例如int和string,并在它们之间自由转换,例如将5解释为"5",反之亦然.这些类型在脚本和其他解释语言中很常见.boost :: lexical_cast支持这种转换功能.
为什么我们需要类似PHP等脚本语言的隐式类型?
此外,在boost :: any示例中,为什么:
using boost::any_cast;
typedef std::list<boost::any> many;
void append_int(many & values, int value)
{
boost::any to_append = value;
values.push_back(to_append);
}
Run Code Online (Sandbox Code Playgroud)
可以接受吗?容器是否使用operator ::在boost :: any中实现?
any & operator=(const any &);
Run Code Online (Sandbox Code Playgroud)
这使得boost :: any能够持有任何类型?operator =在boost :: any中定义的解释如下:
效果:将rhs的内容复制到当前实例中,丢弃以前的内容,以使新内容在类型和值上与rhs的内容相同,如果为rhs.empty()则为空.
抛出:std :: bad_alloc或由包含类型的复制构造函数引起的任何异常.分配满足异常安全的有力保证.
http://www.boost.org/doc/libs/1_46_1/doc/html/boost/any.html
我想在后面的代码中添加这样的东西:
<ul>
<li><a>A</a></li>
</ul>
Run Code Online (Sandbox Code Playgroud)
但是,ASP.NET的ListItem似乎只允许文本:
BulletedList UserSubMenuList = new BulletedList();
ListItem EditUserItem = new ListItem("Edit Profile");
Run Code Online (Sandbox Code Playgroud)
有没有其他方法在<li></li>
标签与代码后面添加内容而不是使用HtmlGenericControl?