是不是C++ 0x没有信号量?Stack Overflow上已经有一些关于信号量使用的问题.我一直使用它们(posix信号量)让线程等待另一个线程中的某个事件:
void thread0(...)
{
doSomething0();
event1.wait();
...
}
void thread1(...)
{
doSomething1();
event1.post();
...
}
Run Code Online (Sandbox Code Playgroud)
如果我用互斥量做到这一点:
void thread0(...)
{
doSomething0();
event1.lock(); event1.unlock();
...
}
void thread1(...)
{
event1.lock();
doSomethingth1();
event1.unlock();
...
}
Run Code Online (Sandbox Code Playgroud)
问题:它很难看并且不能保证thread1首先锁定互斥锁(假设同一个线程应该锁定和解锁互斥锁,你也无法在thread0和thread1启动之前锁定event1).
因此,由于boost也没有信号量,实现上述目标的最简单方法是什么?
你好,
我想让一个python脚本在(ubuntu)linux上作为服务(守护进程)运行.
在网上有几个解决方案,如:
http://pypi.python.org/pypi/python-daemon/
一个表现良好的Unix守护进程很难做到,但每个守护进程程序所需的步骤大致相同.DaemonContext实例保存程序的行为和配置的进程环境; 使用实例作为上下文管理器来进入守护程序状态.
http://www.jejik.com/articles/2007/02/a_simple_unix_linux_daemon_in_python/
但是,由于我想将我的python脚本专门与ubuntu linux集成,我的解决方案是与init.d脚本的组合
#!/bin/bash
WORK_DIR="/var/lib/foo"
DAEMON="/usr/bin/python"
ARGS="/opt/foo/linux_service.py"
PIDFILE="/var/run/foo.pid"
USER="foo"
case "$1" in
start)
echo "Starting server"
mkdir -p "$WORK_DIR"
/sbin/start-stop-daemon --start --pidfile $PIDFILE \
--user $USER --group $USER \
-b --make-pidfile \
--chuid $USER \
--exec $DAEMON $ARGS
;;
stop)
echo "Stopping server"
/sbin/start-stop-daemon --stop --pidfile $PIDFILE --verbose
;;
*)
echo "Usage: /etc/init.d/$USER {start|stop}"
exit 1
;;
esac
exit 0
Run Code Online (Sandbox Code Playgroud)
并在python中:
import signal
import time
import multiprocessing
stop_event = multiprocessing.Event()
def stop(signum, frame):
stop_event.set() …Run Code Online (Sandbox Code Playgroud) 喂!
我正在寻找一种方法来添加自定义消息来断言语句.我发现这个问题在断言中添加自定义消息?但那里的信息是静态的.我想做这样的事情:
assert((0 < x) && (x < 10), std::string("x was ") + myToString(x));
Run Code Online (Sandbox Code Playgroud)
当断言失败时,我想要正常输出加上例如"x为100".
boost::posix_time::ptime parseDate(const std::string& format, const std::string& localDate)
{
std::istringstream is(localDate);
is.imbue(std::locale(is.getloc(), new boost::local_time::local_time_input_facet(format.c_str())));
boost::posix_time::ptime pt;
is >> pt;
if (pt == boost::posix_time::ptime())
{
throw std::runtime_error("Parse error");
}
return pt;
}
Run Code Online (Sandbox Code Playgroud)
此函数应采用日期和格式字符串和return boost::posix_time::ptime.
例如:2012:06:14 02:50:58和%Y:%m:%d %H:%M:%S.
但是,如果我在多线程程序中调用它,有时抛出异常,虽然format并且localDate是正确且可解析的(我对每次调用都使用相同的日期).我发现了一些关于std::stringstream/ std::locale线程问题但没有更新的东西(我正在使用gcc 4.6.3 64位).
这里有人有同样的问题:
使用Valgrind/drd测试过去几天,我发现我的代码中有很多部分会导致问题.例如,当调用一些提升日期时间转换函数时,我点击std :: locale(),这不是线程安全的.
更新的代码没有问题:
boost::posix_time::ptime parseDate(const std::string& format, const std::string& localDate)
{
std::istringstream is(localDate);
auto* facet = new boost::local_time::local_time_input_facet(format.c_str());
{
boost::unique_lock<boost::mutex> lock(globalLocaleMutex);
is.imbue(std::locale(is.getloc(), facet));
}
boost::posix_time::ptime …Run Code Online (Sandbox Code Playgroud) 我试图用 C++中的语句实现类似python的东西.正如我打算使用它主要使用Qt的OpenGL的方法被调用bind,并release(在python __enter__,__exit__).
我提出的代码:
标题:
#include <iostream>
#include <vector>
class With
{
public:
class A
{
public:
virtual ~A() { }
};
template <typename T>
class B : public A
{
public:
B(T& _t) : t(_t)
{
t.bind();
}
virtual ~B()
{
t.release();
}
T& t;
};
template <typename... Args>
With(Args&... args)
{
set(args...);
}
~With();
template <typename T, typename... Args>
void set(T& t, Args&... args)
{
set(t); …Run Code Online (Sandbox Code Playgroud) 喂!
我想只专注于两种模板类型中的一种.例如,template <typename A, typename B> class X对于单个函数应该有一个特殊的实现X<float, sometype>::someFunc().
示例代码:
main.h:
#include <iostream>
template <typename F, typename I>
class B
{
public:
void someFunc()
{
std::cout << "normal" << std::endl;
};
void someFuncNotSpecial()
{
std::cout << "normal" << std::endl;
};
};
template <typename I>
void B<float, I>::someFunc();
Run Code Online (Sandbox Code Playgroud)
main.cpp中:
#include <iostream>
#include "main.h"
using namespace std;
template <typename I>
void B<float, I>::someFunc()
{
cout << "special" << endl;
}
int main(int argc, char *argv[])
{
B<int, int> …Run Code Online (Sandbox Code Playgroud) 从发电机获取固定数量的物品的最有效方法是什么?
我目前所做的是使用zip和range.在这个例子中,我从发生器中取出大小为3的块.
def f():
x = 0
while x < 21:
yield x
x += 1
g = f()
while True:
x = [i for _, i in zip(range(3), g)]
if not x:
break
print x
Run Code Online (Sandbox Code Playgroud)
背景是我使用的数据库为查询结果提供了一个生成器对象.比我用数据填充固定大小的numpy数组并将其作为一个批处理.
你好,
我有一些麻烦理解python引用计数.我想要做的是使用ctypes模块将元组从c ++返回到python.
C++:
PyObject* foo(...)
{
...
return Py_BuildValue("(s, s)", value1, value2);
}
Run Code Online (Sandbox Code Playgroud)
蟒蛇:
pointer = c_foo(...) # c_foo loaded with ctypes
obj = cast(pointer, py_object).value
Run Code Online (Sandbox Code Playgroud)
我不确定obj的引用计数,所以我试过sys.getrefcount()
了3.我认为它应该是2(getrefcount函数使一个ref本身).
现在我无法Py_DECREF()在C++中返回之前创建,因为该对象被删除了.我可以减少python中的引用计数吗?
编辑 调用强制转换函数时,引用计数会发生什么?我不太确定下面的文档.http://docs.python.org/library/ctypes.html#ctypes.cast
ctypes.cast(obj,type)此函数类似于C中的强制转换运算符.它返回一个新的类型实例,它指向与obj相同的内存块.type必须是指针类型,obj必须是可以解释为指针的对象.
想象一下,您有一个 RGB 图像并且想要处理每个像素:
import numpy as np
image = np.zeros((1024, 1024, 3))
def rgb_to_something(rgb):
pass
vfunc = np.vectorize(rgb_to_something)
vfunc(image)
Run Code Online (Sandbox Code Playgroud)
vfunc现在应该得到每个 RGB 值。问题是 numpy 使数组变平,并且函数r0, g0, b0, r1, g1, b1, ...在它应该得到的时候得到
rgb0, rgb1, rgb2, ...。这可以以某种方式完成吗?
http://docs.scipy.org/doc/numpy/reference/generated/numpy.vectorize.html
也许通过事先将 numpy 数组转换为某种特殊的数据类型?
例如(当然不工作):
image = image.astype(np.float32)
import ctypes
RGB = ctypes.c_float * 3
image.astype(RGB)
ValueError: shape mismatch: objects cannot be broadcast to a single shape
Run Code Online (Sandbox Code Playgroud)
更新:这里的主要目的是提高效率。非矢量化版本可能看起来像这样:
import numpy as np
image = np.zeros((1024, 1024, 3))
shape = image.shape[0:2]
image = …Run Code Online (Sandbox Code Playgroud) 如果从不同进程/线程同时访问文件会发生什么?我知道没有标准的方法来锁定文件,只有os特定的功能.
在我的情况下,文件将经常被阅读并且很少被写入.现在,如果A打开一个文件进行阅读(ifstream)并开始阅读块.并B打开相同的文件进行写入(ofstream)并开始编写.会发生什么?有定义的行为吗?
编辑 我的目标是对许多文件进行并发读取,写入访问.但写访问不会经常发生.如果fstream保证文件内容不会混淆,我会很满意.
例如:进程1和进程2写入文件A.如果它们同时写入,我不在乎是否将1或2的版本写入光盘,只要它是文件的一致版本即可.
如果进程读取文件而另一个进程同时写入该文件,我希望读取过程获得该文件的"旧"版本.
如果fstreams没有处理这个,我将使用数据库.