getting thread-ID from another thread

getting thread-ID from another thread

Post by bern » Sun, 30 May 2010 00:11:34


Hi,

how is it possible to get the thread-ID for another thread which is
already running?

The postmessage is generated in thread A -> but the message should be
transmitted to thread B; the problem is, that I don`t have a handle to
thread B.

PostThreadMessage(GetThreadId(), UWM_SEND_DATA, (WPARAM)msg, 0);

Thread-A: proceed incoming data by ethernet
Thread-B: write some code to the com-port1
Thread-C: write some code to the com-port2
and son on (8 com ports) - the code is nearly the one provided by
Joseph M. Newcomer
( http://www.yqcomputer.com/ #SerialParameters)

I`ve 8 com ports installed (each is using a different thread to write
to the com port); depending on the ethernet-data I have to call com-
port1 or com-port2 and so on.

How is it possible for me to know which thread which com port is
using? Or is it much better to use one thread for all 8 com ports
(transmitting the data)? Of course I will also need the ability to
receive information from the com ports (using worker-threads - for
each com port one thread, because I don`t know when data will be
arrived to one of these ports)

best regards
Bernd
 
 
 

getting thread-ID from another thread

Post by Seethara » Sun, 30 May 2010 00:48:32

See if this article helps you:

Traversing the Thread List

http://www.yqcomputer.com/ (v=VS.85).aspx

-Seetharam

 
 
 

getting thread-ID from another thread

Post by Scott McPh » Sun, 30 May 2010 00:54:18

You don't need and don't want thread-ID.

When you create the threads save the pointer that is returned by
AfxBeginThread. To post a message to the thread you can then use:

pointertothread->PostThreadMessage(....);

using?

When you create a thread initialize it to TELL it which com port to use.
You're in charge.

(transmitting the data)?

A transmit thread and a receive thread for each com port is a good plan.

--
Scott McPhillips [VC++ MVP]
 
 
 

getting thread-ID from another thread

Post by Joseph M. » Sun, 30 May 2010 09:02:17

See below...


****
How are you creating the thread? This is extremely important!

Note that you should use AfxBeginThread, and therefore you would never call
::PostThreadMessage; You should call CWinThread::PostThreadMessage, which would know how
to find the thread ID which is stored as part of the CWinThread-derived object.
joe
****
Joseph M. Newcomer [MVP]
email: XXXX@XXXXX.COM
Web: http://www.yqcomputer.com/
MVP Tips: http://www.yqcomputer.com/
 
 
 

getting thread-ID from another thread

Post by bern » Tue, 01 Jun 2010 02:39:38

> A transmit thread and a receive thread for each com port is a good plan.


Is this always a good plan independent how many com ports I`ve
installed? What`s about the performance (using only one processor)?

I mean in my case there`s only one transmit thread (sending some
message coming via ethernet to the com port) at the same time
possible, because the ethernet stream is serial. But it is always
possible to get some data at different com ports at the same time.

I`m creating the ui-thread with AfxBeginThread.

Another question: how is it possible to get the com-port number if I
receive some data? I`m using the whole function from Mr. Newcomer
( http://www.yqcomputer.com/ )? Each tx ethernet packets has an
information from which com port the data was received.

m_serialWriteThread->parms = new CSerialPort(m_hComm, AfxGetMainWnd(),
shutdown);

With HANDLE port = this->parms->hCom; I only get the handle

best regards
 
 
 

getting thread-ID from another thread

Post by ScottMcP [ » Tue, 01 Jun 2010 03:20:26


COM ports are slow. Seems better to me to be able to transmit
simultaneously on more than one of them concurrently. For anything up
to maybe 32 ports or so the impact of one thread per output port
should be rather miniscule at COM port speeds. But this is a minor
issue, and if you prefer doing all the writes in one thread I don't
see any problem with that.



The COM read thread uses PostMessage to pass received data to the main
thread. You can pass the COM port number as part of that message:

CString* s = new CString(....);
PostMessage(UWM_DATA_READ, (WPARAM)s, (LPARAM)port_number);
 
 
 

getting thread-ID from another thread

Post by Joseph M. » Tue, 01 Jun 2010 10:22:49

See below...



****
This is pretty much how I would do it, except I wouldn't worry too much about the number
of ports.

If you start it with AfxBeginThread, as I pointed out, you do not need the thread ID for
anything, because you will use CWinThread::PostThreadMessage and therefore there's nothing
you need to do special to get it.
****
****
It is your responsibility to identify the information you need. In my example, I
PostMessage a length and pointer, but you could create a little object on the heap that
had the length, pointer, and any information you need to identify the source of the data.
There's nothing that limits what you transmit to the receiving thread.
*****
****
That's right. If you want anything else, you have to provide it.
****
***
Or you can do

CMyStuff * d = new CMyStull(length, data, port_number);
wnd->PostMessage(UWM_DATA_READ, (WPARAM)d);


The point is, YOU get to define what is in that message! If all you need is a length and
pointer, you can use WPARAM and LPARAM; if you need more, you put more into what you pass
to the thread, and it is entirely your decision as to what you put there.
joe
****
Joseph M. Newcomer [MVP]
email: XXXX@XXXXX.COM
Web: http://www.yqcomputer.com/
MVP Tips: http://www.yqcomputer.com/
 
 
 

getting thread-ID from another thread

Post by bern » Tue, 01 Jun 2010 16:55:17

thanks for the answers.


/* transmit eth-messages to the specific com port (is working) */
unsigned char* buffer=new unsigned char[400];
memcpy((void *)&buffer[0], (void *)&ptr->Data[0], 400);
m_Ports[number].m_serialWriteThread-

void CSerialWriter::OnSendData(WPARAM wParam, LPARAM lParam)
{
unsigned char * schar = (unsigned char *)wParam;
UINT length = strlen((const char *)wParam); //<- 3 instead of 400

BOOL ok = ::WriteFile(parms->hCom,(unsigned char
*)schar,lParam,&bytesWritten,&ovl);
//....

delete [] schar;
}

How can I delete the unsigned char array at the end of the
OnSendData() function?


best regards
Bernd
 
 
 

getting thread-ID from another thread

Post by Faisa » Tue, 01 Jun 2010 20:14:13


You can write this simpler
memcpy( buffer, ptr->Data, 400);


Here you are passing the address of buffer. No need for this
You can have
m_Ports[number].m_serialWriteThread->PostThreadMessage(UWM_SEND_DATA2,
(WPARAM)buffer, 400);


Because you passed the wrong pointer (&buffer). >> >> BOOL ok = ::WriteFile(parms>>hCom,(unsigned char >> *)schar,lParam,&bytesWritten,&ovl); >> //.... >> >> delete [] schar; >> >> } >> >> How can I delete the unsigned char array at the end of the >> OnSendData() function?
>> >> best regards >> Bernd
 
 
 

getting thread-ID from another thread

Post by Scott McPh » Tue, 01 Jun 2010 22:23:08


The &buffer is the address of the pointer, not the address of the buffer.
Use buffer, not &buffer


Why write 400 bytes when strlen has determined there are only 3 bytes
received?


delete [] schar is the correct way to delete the buffer.

--
Scott McPhillips [VC++ MVP]
 
 
 

getting thread-ID from another thread

Post by bern » Thu, 03 Jun 2010 22:38:17

On 28 Mai, 17:54, "Scott McPhillips [MVP]" <org-dot-mvps-at-scottmcp>


A small additional question: is it enough to have one thread for the
ethernet communication (socket) or could it be much better to have one
thread for the receive and one for the transmit part? Or another way:
using one receive-list and one transmit-list...

best regards
Bernd
 
 
 

getting thread-ID from another thread

Post by ScottMcP [ » Thu, 03 Jun 2010 23:19:40


If you are using CAsyncSocket (or equivalent in winsock) then you
can't use one ethernet port in two threads. The socket notifications
are, by design, all posted to one thread's message queue. The calls
you make to socket functions are non-blocking (they all return
quickly) so there would be no advantage to using two threads.
 
 
 

getting thread-ID from another thread

Post by Joseph M. » Thu, 03 Jun 2010 23:22:45

See below...



****
For networking, it is impossible to have one thread for receive and one thread for send if
you are using CAsyncSocket, because this relies on the handle map to map incoming
notifications to the CAsyncSocket-derived class, and the handle map is per-thread.
Therefore, you can have one thread per connection (although with CAsyncSocket, this is
unnecessary) but you cannot split a connection's two operations (send/receive) across two
threads. It works for serial ports because only the HANDLE object needs to be shared
across the threads, and MFC doesn't come into the picture.

If you do raw SOCKET programming, you are on your own (not a recommended practice, because
of the complexity), and therefore could do the split, but it doesn't actually buy much to
allow it.

joe
****
Joseph M. Newcomer [MVP]
email: XXXX@XXXXX.COM
Web: http://www.yqcomputer.com/
MVP Tips: http://www.yqcomputer.com/