A class(defined by myself) shared by the threads must be lock and unlock?
A class(defined by myself) shared by the threads must be lock and unlock?
yes, using mutex
when I used two threads like what I have said in my program and compile and run it,it will be crashed at radom position,Is it because of my not synchronizing my threads?Must I synchronize them?
if there is a shared object, u have to make it thread-safe..and yes, synchronize..and again, why cant u upgrade Qt? u have a solution waiting for u in there, so i cant understand ur reluctance
Thanks for answer.
1.Why have not upgrate my Qt?
My colleagues and I all use qt4.3.1+ mysql+coin3d+msvc2005+carnac.If I upgrate my qt,they must.And a lot time will be wasted for built platform again.
2.If threads only read(not write) data from the shared class(created by myself),Must it be locked and unlocked?like this:
.....
Do I use mutex in SharedClass ?That is,must all the functions and varables in SharedClass be locked and unlocked?for example ,SharedClass like this:Qt Code:
class Mythread1 { public: Mythread1(SharedClass *sharedclass); void calculate(SharedClass *sharedclass) { //read data from sharedclass and calculate } } SharedClass *sharedClass=new SharedClass (); init(A); thread1=new Mythread1(A); thread2=new Mythread1(A) .....To copy to clipboard, switch view to plain text mode
How could I do?Qt Code:
class SharedClass { public: A *a; B *b; QString str; void function1(); }To copy to clipboard, switch view to plain text mode
Thank you.
Last edited by wysota; 7th April 2009 at 15:09. Reason: missing [code] tags
read about thread-safe classes.. thread-safe classes are those classes for which multiple threads can access the SAME instance of the class..u have to provide synchronization in these classes..i dunno what your application is doing..u have a number of possibilities to implement synchronisation depending upon what ur class does..
and you should consult with your fellow colleagues about the new Qt version...there r lots of other features that u will be missing and implementing yourself..which is MORE wasted time
class SharedClass will be shared by all threads,and it structure is like this:
If I will let it thread-safe,Do I do like this?Qt Code:
class SharedClass { public: A *a; B *b; QString str; void function1() { a=new A(); b=new B(); } }To copy to clipboard, switch view to plain text mode
class a and b must be thread-safe also?Qt Code:To copy to clipboard, switch view to plain text mode
Last edited by wysota; 7th April 2009 at 15:10. Reason: missing [code] tags
no answer ??
Sorry.
I explain my question again.
As we know ,if a class is shared by muti-threads,it should be thread-safe.Now I define a class ,and it will be shared by 2 threads.If I want to let it thread-safe,I do as follow,Am I right or not?
Qt Code:
class SharedClass { public: A *a; B *b; QString str; void function1() { a=new A(); b=new B(); } }To copy to clipboard, switch view to plain text mode
If I will let it thread-safe,I add code like this:
By the way,Must class a and b be thread-safe?Qt Code:To copy to clipboard, switch view to plain text mode
Last edited by wysota; 8th April 2009 at 13:06. Reason: missing [code] tags
If you post a code snippet without appropriate tags one more time, you will get a temporary ban on the forum.
No, a and b don't have to be thread safe.
Bookmarks