How to Call a function from thread entry point;
-
if i have a thread and an entry function like this:
@static void *readerThreadEntryPoint(void *arg)@
@void *MainWindow::readerThreadEntryPoint(void *arg) {
MODES_NOTUSED(arg);}
@and i want to call this @QBytearray data = socket->ReadAll(); @
how can i do ?
i have an error: invalid use of member 'MainWindow::socket' in static member function
QTcpSocket *socket; -
Hi,
Don't make readerThreadEntryPoint static. You are trying to use a member variable in your static function which is not allowed.
Out of curiosity, what thread calls that function ?
-
i use the pthread lib, it is a C code and i try to make a gui, but if the entrypoint is not static function then i have an error: cannot convert 'MainWindow::readerThreadEntryPoint' from type 'void* (MainWindow::)(void*)' to type 'void* ()(void)'
-
Since you are using Qt, why don't you use QThread ?
By the way, are you sure you need threading at all ? Qt's network operations are already synchronous.
-
i dont know how to use Qthread and i am confused.
If i use @connect( socket, SIGNAL(readyRead()), this, SLOT(readTcpData()) );@ looks to work false. Updates the buffer before makes the process of the data. -
[quote author="xmaze" date="1424469708"] Updates the buffer before makes the process of the data. [/quote]
What do you mean by that ?
-
the program copy the data from the TCP port to the data buffer ,
the second thread read this buffer and make the process.
If i use the signal, then the programm writes the new bytes to data buffer before the second thread make the process. that means that i lose the data. -
You are describing concurrent programming, which is an entirely different, and advanced, subject.
Looks up single producer, single consumer queues. Boost has a nice one.
-
Solution found for the call, i changed this line
@pthread_create(&Modes.reader_thread, NULL, readerThreadEntryPoint, NULL); @
to this one
@pthread_create(&Modes.reader_thread, NULL, readerThreadEntryPoint, this); @
I wrote the following code in header, and i wrote some code in function readTCPdata();
@void readTCPdata(void);
static void readerThreadEntryPoint(void arg) { return ((MainWindow)arg)->readTCPdata(); } @The thread is working but cannot read the data, only make a call without results!
-
Any error message on the console ?
By the way, you can't use a QTcpSocket created in one thread from another one.
-
[quote author="SGaist" date="1424554944"]Any error message on the console ?
By the way, you can't use a QTcpSocket created in one thread from another one.[/quote]
i don't have an error, the data Array it;s empty, How can i declare a QtcpSocket in a Thread;
-
Have a look at the Threaded Fortune Server example