我在一个线程中按顺序执行以下操作:
int size = hashTable.size();
foreach .... in ... hasTable.values()
做一点事
我的问题是foreach会执行大小的时间吗?(即使另一个线程同时放置/删除元素?
如果我添加一个观察者来观察UILabel的"突出显示"属性,我可以在观察回调块中对另一个UIView进行更改吗?即:我保证这个回调块将始终在主线程上执行吗?
谢谢!
免责声明:Boost和C++ 11都不允许.
我有一个程序,我在其中创建了一个实例,Foo并在多个线程中使用它.然后我想要安全地删除它,以便这些线程不会陷入分段错误.
Foo每次线程函数运行时,我都会添加一个互斥锁成员并将其锁定.为了使不同的线程不相互冲突.
class Foo
{
public:
pthread_mutex_t mutex;
};
void* thread ( void* fooPtr )
{
Foo* fooPointer = (Foo*) fooPtr;
while ( 1 )
{
if ( fooPointer )
{
pthread_mutex_lock ( &fooPointer->mutex );
/* some code, involving fooPointer */
pthread_mutex_unlock ( &fooPointer->mutex );
}
else
{
pthread_exit ( NULL );
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在我想要foo安全删除,因此线程中不会发生错误.我添加了一个析构函数Foo:
Foo::~Foo()
{
pthread_mutex_lock ( &mutex );
}
Run Code Online (Sandbox Code Playgroud)
现在,在所有线程完成当前循环之前,不会删除该对象.
问题是:删除实例后是否会解锁互斥锁?删除实例后,所有线程都会完成吗?我打赌答案是no.所以我改变了析构函数,但现在看起来线程不安全:
Foo::~Foo()
{
pthread_mutex_lock ( …Run Code Online (Sandbox Code Playgroud) 在我的申请中要求"N"个产品可以与报价相关联.屏幕布局将有两个部分.顶部有一个包含报价相关信息的表格,底部部分用于存放多个产品.我通过底部的iframe实现了这个功能.单击按钮(使用javascript)将添加/删除该产品.要在每个Product窗口中显示的内容将由相同的Action(ProductLinesAction.java),JSP(ProductLines.jsp)和其他相关资源呈现.这里的重点是,只要在屏幕上加载新的Product窗口,就会创建该Action类的多个实例.我在加载窗口时没有任何问题,因为它只是准备要显示的表单.在保存引号的同时,将提交所有这些产品表单,并且我合并的逻辑是1到N-1个动作实例将表单值放入VO中,该VO被添加到Vector对象并保存在会话中(以便其他动作实例可以从会话中获取它并在其上添加).第N个动作实例旨在共同保存所有这些产品值.业务规则验证也在保存之前执行,因此第N个操作实例将可用,并且应该在每个产品窗口中显示错误.
为了确保所有其他操作实例也可以利用与其窗口相对应的错误,我实现了wait和notifyAll机制,其中当尝试保存超过6个产品时出现问题.代码如下.这段代码适用于小于或等于6的产品(我的意思是最多6个动作实例).加载并保存第7个产品时,第七个实例在调试模式下根本不可见或无法跟踪(实例在表单提交时未达到预期的方法).
任何人都可以对这里犯下的错误有所了解,这个错误是造成这个问题的原因.
public String submitProducts()
throws Exception {
String resultValue = "";
/* Algorithm: */
// 1. Read the Vector object from Session.
// 2. Check whether the size of the Vector matches the Total Product windows count.
// 3. If yes, call the Save operation and remove the list from session.
// 4. If not, copy the values from current Action instance to VO.
// 5. Add to List object and place in session.
synchronized (productVOsInVector) {
productVOsInVector …Run Code Online (Sandbox Code Playgroud) 检查LunarLander示例,它使用该代码恢复抽屉线程:
public void surfaceCreated(SurfaceHolder holder) {
// start the thread here so that we don't busy-wait in run()
// waiting for the surface to be created
thread.setRunning(true);
thread.start();
}
Run Code Online (Sandbox Code Playgroud)
这结束了:
public void surfaceDestroyed(SurfaceHolder holder) {
// we have to tell thread to shut down & wait for it to finish, or else
// it might touch the Surface after we return and explode
boolean retry = true;
thread.setRunning(false);
while (retry) {
try {
thread.join();
retry = false;
} catch (InterruptedException …Run Code Online (Sandbox Code Playgroud) public class Test{
private MyObj myobj = new MyObj(); //it is not volatile
public class Updater extends Thred{
myobje = getNewObjFromDb() ; //not am setting new object
}
public MyObj getData(){
//getting stale date is fine for
return myobj;
}
}
Run Code Online (Sandbox Code Playgroud)
更新定期更新myobj
其他类使用getData获取数据
IS此代码线程安全而不使用volatile关键字?
我想是的.有人可以证实吗?
如果libsqlite不是线程安全的,则类似这样的代码
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
__block NSArray *__albumsCollection = albumCollections;
dispatch_apply(count, queue, ^(size_t i)
{
MPMediaItem *albumObj = [[__albumsCollection objectAtIndex:i] representativeItem];
///// making some sqlite queries
});
Run Code Online (Sandbox Code Playgroud)
会引发一个BAD_EXEC。
那么如何使此代码线程安全?
我的解决方案是使用主队列
dispatch_apply(count, dispatch_get_main_queue(), ^(size_t i)
{
/// my sqllite queries
});
Run Code Online (Sandbox Code Playgroud)
但我对此不满意。如何使它更好?
简单的问题.以下代码是否是线程安全的......?
我ArrayList内心的主要疑问是Hashtable因为ArrayList不是线程安全的.如果它的一部分也会发生同样的事情Hashtable.
Hashtable<Thread, List<String>> threadObjects = new Hashtable<Thread, List<String>>();
// lets assume some object is added.
synchronized (threadObjects)
{
thread = Thread.currentThread();
List<String> v = threadObjects.get(thread);
if (null != v)
{
// do something
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢
我想知道这段代码有什么问题.有时我会得到脓毒症,有时候不会.这是我在更大的软件中遇到的一个问题,这个想法是只有一个线程同时执行MyClass :: print方法.即使有这个简单的例子,它也会因为分段错误而失败.代码有什么问题?我怎么解决这个问题?
谢谢!
#include <iostream>
#include <ctime>
#include <QMutex>
#include <QtConcurrentRun>
class MyClass : QThread {
public:
void print(std::string str) {
mutex.lock();
std::cout << "In some thread: " << str << "\n";
mutex.unlock();
}
private:
QMutex mutex;
};
int main() {
MyClass myCl;
for(int i=0; i < 10; i++) {
QtConcurrent::run(&myCl, &MyClass::print,std::string("bla"));
}
}
Run Code Online (Sandbox Code Playgroud) 这是我在许多大型项目中发现的常见问题:
一个非常简单的生产解决方案是每隔几个小时就杀死并重新启动该过程.
如果100%的时间可以正常工作,并且试图让程序运行数小时(天)而没有这个问题那么很难,为什么要花费工程资源来解决问题呢?
multithreading garbage-collection memory-leaks thread-safety
thread-safety ×10
java ×5
c++ ×2
concurrency ×2
objective-c ×2
android ×1
arraylist ×1
hashtable ×1
ios ×1
memory-leaks ×1
mutex ×1
qt ×1
sqlite ×1