线程间通信
我正在开发客户端服务器应用程序,我需要发送从脚本文件读取的命令。
脚本文件的格式如下。
CONNECT <www.abcd.com,80>
SEND : AB 40 01 FF 00 00 00 09 01 01 07 00 00 C0 A8 01 87 AE
MATCH<s,10>: AB 40 01 FF 00 00 00 09 01 01 07 00 00 C0 A8 01 87 AE
SEND : AB 34 01 FF00 00 00 0C 01 01 07 00 01 01 07 00 FF FF FF FF AE
DISCONNECT
note: s in match is wait time in seconds.
here second byte is Msg ID.
当遇到匹配命令时,程序应等待指定秒数的匹配,然后继续执行下一个命令。
我有两个线程在应用程序
- 侦听器线程中运行 - 它将从服务器接收数据。(此处使用 select())
当程序遇到连接命令时它将启动,当
在配置中遇到断开连接。 - 主线程将从配置文件中读取命令并执行。
现在,当遇到匹配时,主线程应该将匹配字符串发送到 侦听器线程用于匹配并等待来自侦听器线程的信号。
侦听器线程会将字符串与从服务器接收到的数据进行匹配,如果匹配,则会将事件(SetEvent() windows)单一到主线程,然后主线程将记录“找到匹配”,否则如果时间已过,它将记录作为“未找到匹配”,
我想到了一个全局变量 char* g_MatchString。只要有匹配命令,主线程就会更新这个变量,并等待事件(windows 事件)被选中,等待时间将等于匹配时间。
无论我的方法正确与否,我都需要你们的意见。
I am working on client server application where I need to send the command read from the script file .
The format of the script file is as follows.
CONNECT <www.abcd.com,80>
SEND : AB 40 01 FF 00 00 00 09 01 01 07 00 00 C0 A8 01 87 AE
MATCH<s,10>: AB 40 01 FF 00 00 00 09 01 01 07 00 00 C0 A8 01 87 AE
SEND : AB 34 01 FF00 00 00 0C 01 01 07 00 01 01 07 00 FF FF FF FF AE
DISCONNECT
note: s in match is wait time in seconds.
here second byte is Msg ID.
When it encounter Match Command the Program should wait for match for specified second and then proceed to next command.
I have two thread running in the application
- Listener Thread- it will receive data from server.(select() is used here)
it will be launched when the program encounter the connect command and goes off when
encounter disconnect in config. - main thread which will read command from the config file and execute.
Now when match is encounter main thread should send the match string to the
Listener Thread for matching and wait there for signal from Listener Thread.
The Listener Thread will match the string with data received from the server if it matches it will single the event(SetEvent() windows) to main thread and then main thread will log "Match found" other wise if time is elasped then it will log as "Match not found"
I thought of having a global variable char* g_MatchString.The main thread will update this variable whenever there is match command and wait for event(windows event) to be singled and the wait time will be equal to match time.
I need input from you guys whether my approach is correct or not.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要使用全局。当有人在未来增加复杂性时,这只会产生竞争条件的可能性。匹配字符串应作为输入参数传递给线程。您没有说明如何启动线程,但如果您使用 _beginthread(),您只需分配一个缓冲区并将其传递给 arglist 参数中的 _beginthread() 即可。当侦听器线程终止时,主线程可以安全地释放匹配字符串缓冲区。如果添加额外的线程,这可以使其保持良好的独立性,并且不会出现潜在的竞争条件。如果您使用 CreateThread() 启动线程,则可以通过 lpParameter 参数传递它。
除了全球范围之外,我认为你的方法是正确的。
Don't use a global. That just creates the potential for race conditions when someone adds complexity in the future. The match string should be passed to the thread as an input argument. You don't say how you're launching the thread, but if you're using _beginthread() you simply allocate a buffer and pass it to _beginthread() in the arglist parameter. When the listener thread terminates, the main thread can safely free the match string buffer. This keeps it nicely self-contained and free of potential race conditions if additional threads are ever added. If you're launching the thread with CreateThread(), you would pass it via the lpParameter parameter.
Other than the global, I think your approach is sound.
由于主线程正在等待侦听器线程,并且侦听器线程的唯一目的是读取入站数据,因此我建议完全摆脱侦听器线程并让主线程直接进行读取。
Since the main thread is waiting on the listener thread, and the listener thread's sole purpose is to read inbound data, I would suggest just getting rid of the listener thread altogether and let the main thread do the reading directly.