Hi,
I create a class named SyncronizedPointer, it look like a QSharedPointer except that "->" operator make that all call become thread Safe here a simple example of use.
class Foo
{
void criticalMethod()
{
//All work done here is completely thred-safe
}
};
int main()
{
SyncronizedPointer<Foo> sp( new Foo() );
sp->criticalMethod();
/* This precedent line will automatically:
- Lock an mutex inside SyncronizedPointer object.
- Call the criticalMethod
- Unlock the mutex
With this system it's easy to make the Foo class thread safe, I can use copy of the
SyncronizedPointer object in multiple thread, I am sure that no more than one thread can
call a method inside Foo object at the same time.
*/
return (0);
/*
The Foo object will be automatically delete using a reference counting inside SyncronizedPointer.
*/
}
class Foo
{
void criticalMethod()
{
//All work done here is completely thred-safe
}
};
int main()
{
SyncronizedPointer<Foo> sp( new Foo() );
sp->criticalMethod();
/* This precedent line will automatically:
- Lock an mutex inside SyncronizedPointer object.
- Call the criticalMethod
- Unlock the mutex
With this system it's easy to make the Foo class thread safe, I can use copy of the
SyncronizedPointer object in multiple thread, I am sure that no more than one thread can
call a method inside Foo object at the same time.
*/
return (0);
/*
The Foo object will be automatically delete using a reference counting inside SyncronizedPointer.
*/
}
To copy to clipboard, switch view to plain text mode
So my problem is when I am trying to use SyncronizedPointer with QObject like
{
Q_OBJECT
public:
void criticalMethod();
public slot:
void criticalSlot();
}
int main()
{
SyncronizedPointer<Foo> sp(new Foo());
/*
Here no problem I can send the sp var across thread, call to criticalMethod will stay
thread safe.
*/
/*
Now I want to connect the timer timeout signal to the criticalSlot
Note that call to this slot MUST be totaly thread safe
*/
QObject::connect(&timer,
SIGNAL(timeout
()), sp
->data
(),
SLOT(criticalSlot
()));
/*
The precedent line does not protect the access to the Foo class because Qt signal system
will directly call the slot without locking the Mutex inside the SyncronizedPointer
*/
return (0);
}
class Foo : public QObject
{
Q_OBJECT
public:
void criticalMethod();
public slot:
void criticalSlot();
}
int main()
{
SyncronizedPointer<Foo> sp(new Foo());
/*
Here no problem I can send the sp var across thread, call to criticalMethod will stay
thread safe.
*/
QTimer timer;
/*
Now I want to connect the timer timeout signal to the criticalSlot
Note that call to this slot MUST be totaly thread safe
*/
QObject::connect(&timer, SIGNAL(timeout()), sp->data(), SLOT(criticalSlot()));
/*
The precedent line does not protect the access to the Foo class because Qt signal system
will directly call the slot without locking the Mutex inside the SyncronizedPointer
*/
return (0);
}
To copy to clipboard, switch view to plain text mode
Is there a solution to make this work, for example by making Qt call the slot using the
SyncronizedPointer object ?
An easy way would be to put the mutex inside the Foo class and lock it inside both slot and method
but the code would be much less sexy 
PS: Sorry if my English is not perfect.
Bookmarks