| |

VerySource

 Forgot password?
 Register
Search
View: 585|Reply: 2

Why can't my communication thread use the CSokcet global variable of the main thread

[Copy link]

2

Threads

16

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

Post time: 2020-2-7 12:30:01
| Show all posts |Read mode
In CMyDialog:
CSocket m_pSocket;
CMyThread m_pThread;

In CMyDialog :: InitDialog ()

m_pSocket = new CSocket ();
m_pSocket-> Create (MYPORT);
... Socket generation is perfectly fine

m_pThread = AfxBeginThread (RUNTIME_CLASS (CMyThread)); The establishment of Thread is also no problem at all

In CMyDialog :: OnMyButtonClick ()
m_pThread-> PostThreadMessage (MYMESSAGE);

In the CMyThread message loop:

case MYMESSAGE:
   (This is a CMyDlg class global variable generated when the Thread is initialized)
    m_pDlg-> RecvData ();

Everything was fine before:
But when m_pThread executes CMyDlg :: RecvData ()
CMyDlg :: RecvData () {
m_pSocket-> send (...) Here m_pSocket == 0, there is no problem if you use this-> RecvData () without going through the thread, why?
Does the CSocket class have a dependency on its spawning thread?
Reply

Use magic Report

0

Threads

36

Posts

22.00

Credits

Newbie

Rank: 1

Credits
22.00

 China

Post time: 2020-4-17 10:30:01
| Show all posts
MFC objects are thread specifical, you need to do Detach / Attach, you can find more detail in the following post.
Reply

Use magic Report

0

Threads

3

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-4 13:15:01
| Show all posts
CSocket is not thread-safe, it must rely on a window main thread
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list