我的函数中有一个变量是静态的,但我希望它在每个线程的基础上是静态的.
如何为我的C++类分配内存,以便每个线程都有自己的类实例副本?
AnotherClass::threadSpecificAction()
{
// How to allocate this with thread local storage?
static MyClass *instance = new MyClass();
instance->doSomething();
}
Run Code Online (Sandbox Code Playgroud)
这是在Linux上.我没有使用C++ 0x,这是gcc v3.4.6.
我对join()Java中的Threads中使用的方法感到困惑.在以下代码中:
// Using join() to wait for threads to finish.
class NewThread implements Runnable {
String name; // name of thread
Thread t;
NewThread(String threadname) {
name = threadname;
t = new Thread(this, name);
System.out.println("New thread: " + t);
t.start(); // Start the thread
}
// This is the entry point for thread.
public void run() {
try {
for (int i = 5; i > 0; i--) {
System.out.println(name + ": " + i);
Thread.sleep(1000);
}
} …Run Code Online (Sandbox Code Playgroud) 我有一个方法应该延迟运行指定的时间.
我应该用吗?
Thread thread = new Thread(() => {
Thread.Sleep(millisecond);
action();
});
thread.IsBackground = true;
thread.Start();
Run Code Online (Sandbox Code Playgroud)
要么
Timer timer = new Timer(o => action(), null, millisecond, -1);
Run Code Online (Sandbox Code Playgroud)
我读过一些关于使用的文章Thread.Sleep是糟糕的设计.但我真的不明白为什么.
但是对于使用Timer,Timer有配置方法.由于执行延迟,我不知道如何配置Timer.你有什么建议吗?
或者如果你有延迟执行的替代代码也很感激.
我已经阅读了关于每个BeginInvoke()是否必须与EndInvoke()匹配的矛盾意见.是否有任何与调用EndInvoke()相关的泄漏或其他问题?
我想指定特定pthread的cpu-affinity.到目前为止,我发现的所有引用都涉及设置进程的cpu-affinity(pid_t)而不是线程(pthread_t).我尝试了一些传递pthread_t的实验,正如预期的那样,它们会失败.我想做一些不可能的事吗?如果没有,你能发一个指针吗?太感谢了.
我如何让一个线程将一个元组或我选择的任何值返回给Python中的父元素?
可能重复:
使用成员函数启动线程
我有一个小班:
class Test
{
public:
void runMultiThread();
private:
int calculate(int from, int to);
}
Run Code Online (Sandbox Code Playgroud)
如何在方法的两个线程中calculate使用两个不同的参数集(例如calculate(0,10),calculate(11,20))运行方法runMultiThread()?
PS谢谢我忘记了我需要传递this,作为参数.
所以我写了一个程序来测试64位kubuntu linux上的线程,版本13.04.实际上我从正在编写测试程序的其他人那里抢了代码.
#include <cstdlib>
#include <iostream>
#include <thread>
void task1(const std::string msg)
{
std::cout << "task1 says: " << msg << std::endl;
}
int main(int argc, char **argv)
{
std::thread t1(task1, "Hello");
t1.join();
return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)
我编译使用:
g++ -pthread -std=c++11 -c main.cpp
g++ main.o -o main.out
Run Code Online (Sandbox Code Playgroud)
然后跑了:
./main.out
Run Code Online (Sandbox Code Playgroud)
顺便说一下,当我使用-l'时,main.out会像所有可执行文件一样以绿色文本显示,但在其名称的末尾也有一个星号.为什么是这样?
回到手头的问题:当我运行main.out时,出现了一个错误,其中说:
terminate called after throwing an instance of 'std::system_error'
what(): Operation not permitted
Aborted (core dumped)
Run Code Online (Sandbox Code Playgroud)
任何有关如何解决此问题的想法?
告诉循环线程停止循环的正确方法是什么?
我有一个相当简单的程序,它在一个单独的threading.Thread类中ping指定的主机.在这个类中,它会休眠60秒,再次运行直到应用程序退出.
我想在我的实现一个'停止'按钮,wx.Frame要求循环线程停止.它不需要立即结束线程,它可以在唤醒后停止循环.
这是我的threading类(注意:我还没有实现循环,但它可能属于PingAssets中的run方法)
class PingAssets(threading.Thread):
def __init__(self, threadNum, asset, window):
threading.Thread.__init__(self)
self.threadNum = threadNum
self.window = window
self.asset = asset
def run(self):
config = controller.getConfig()
fmt = config['timefmt']
start_time = datetime.now().strftime(fmt)
try:
if onlinecheck.check_status(self.asset):
status = "online"
else:
status = "offline"
except socket.gaierror:
status = "an invalid asset tag."
msg =("{}: {} is {}. \n".format(start_time, self.asset, status))
wx.CallAfter(self.window.Logger, msg)
Run Code Online (Sandbox Code Playgroud)
在我的wxPyhton框架中,我从"开始"按钮调用此函数:
def CheckAsset(self, asset):
self.count += 1
thread = PingAssets(self.count, asset, self)
self.threads.append(thread)
thread.start()
Run Code Online (Sandbox Code Playgroud) 我已经开始在线程中学习同步.
同步方法:
public class Counter {
private static int count = 0;
public static synchronized int getCount() {
return count;
}
public synchronized setCount(int count) {
this.count = count;
}
}
Run Code Online (Sandbox Code Playgroud)
同步块:
public class Singleton {
private static volatile Singleton _instance;
public static Singleton getInstance() {
if (_instance == null) {
synchronized(Singleton.class) {
if (_instance == null)
_instance = new Singleton();
}
}
return _instance;
}
}
Run Code Online (Sandbox Code Playgroud)
我什么时候应该使用Synchronized方法和Synchronized块?为什么Synchronized阻止比同步方法更好?
multithreading ×10
c++ ×3
c# ×2
c++11 ×2
java ×2
python ×2
affinity ×1
cpu ×1
delegates ×1
exit-code ×1
linux ×1
new-operator ×1
processor ×1
pthread-join ×1
sleep ×1
synchronized ×1
system-error ×1
timer ×1
wxpython ×1