我使用C++ 14的shared_timed_mutex编写了一个读写器问题的实现.在我看来,下面的代码应该导致Writer饿死,因为太多的读者线程一直在数据库上工作(在这个例子中是一个简单的数组):作者没有机会获得锁.
mutex cout_mtx; // controls access to standard output
shared_timed_mutex db_mtx; // controls access to data_base
int data_base[] = { 0, 0, 0, 0, 0, 0 };
const static int NR_THREADS_READ = 10;
const static int NR_THREADS_WRITE = 1;
const static int SLEEP_MIN = 10;
const static int SLEEP_MAX = 20;
void read_database(int thread_nr) {
shared_lock<shared_timed_mutex> lck(db_mtx, defer_lock); // create a lock based on db_mtx but don't try to acquire the mutex yet
while (true) {
// generate new …Run Code Online (Sandbox Code Playgroud) 我现在正在进入Java图形,我正在阅读Andrew Davison撰写的Java中的Killer游戏编程.他编写了一个简单的窗口应用程序,它使用Image类进行离屏渲染,然后将图像复制到组件.我觉得令人困惑的是,人们可以创建图像然后获取图形上下文:
dbg = dbImage.getGraphics();
Run Code Online (Sandbox Code Playgroud)
我发现它令人困惑,因为那时我们只使用dbg Graphics对象来绘制东西但后来我们在paintComponent方法中使用dbImage来显示我们绘制到dbg Object的所有东西:
g.drawImage(dbImage, 0, 0, null);
Run Code Online (Sandbox Code Playgroud)
那么dbImage如何"知道"它包含我们绘制到dbg Graphics对象上的所有图形内容?
这是整个代码:
public class GamePanel extends JPanel implements Runnable{
private static final int PWIDTH = 500;
private static final int PHEIGHT = 400;
private Thread animator;
private volatile boolean running = false;
private volatile boolean gameOver = false;
private Graphics dbg;
private Image dbImage = null;
private Counter counter;
public GamePanel(){
setBackground(Color.white);
setPreferredSize(new Dimension(PWIDTH, PHEIGHT));
// create game components eg. counter.
counter = new Counter(10);
counter.start();
} …Run Code Online (Sandbox Code Playgroud) 好的,这已经被覆盖了,例如:静态数组类变量"多重定义"C++
但我在这里遗漏了一些细节.
我有以下课程:Foo.cpp
#include <iostream>
#include "Model.h"
int main(int argc, char** argv){
std::cout << "hello" << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Model.h
#ifndef MODEL_H
#define MODEL_H
#include <string>
#include "md2Loader.h"
class Model{
public:
Model();
Model(const std::string& model_file);
private:
md2_header_t header;
modelData_t model;
};
#endif
Run Code Online (Sandbox Code Playgroud)
Model.cpp
#include "Model.h"
#include "md2Loader.h"
Model::Model(){}
Model::Model(const std::string& model_file){
model = md2Loader::load_model(model_file);
}
Run Code Online (Sandbox Code Playgroud)
和md2Loader.h
#ifndef MD2LOADER_H
#define MD2LOADER_H
struct modelData_t{
int numVertices;
int numTextures;
// etc
};
struct md2_header_t {
std::string version;
};
class md2Loader{
public: …Run Code Online (Sandbox Code Playgroud)