News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Windows Threading Question

Started by travism, May 12, 2009, 01:11:09 AM

Previous topic - Next topic

travism

I was reading Iczelion's tutorial on threading since I rarely use it. Basically im trying to create a infinity loop to send and receive data over winsock. 2 problems i ran into is that CreateThread only accepts 1 argument (I have 2 sockets a sender and receiver) so another one of my questions is if they are global can I still refer to them outside of the thread even though I didn't pass them as a argument to the thread? Im trying to figure out how to have a infinity loop without freezing the application basically. :\ Thanks again for your help :) :U

Slugsnack

well the thread procedure is just another function.  so yes it can see global variables.  but the proper way to do it would be to make a structure that has 2 parts for each of the arguments.  declare one of those structures and fill it with your arguments.  then you can pass a pointer to that structure as the thread parameter

if you wanna avoid a tight loop, then make it so on each iteration of the loop it invokes sleep.

travism

Ah I will try that out, Thank you!

Tedd

The usual method for WinSock programs is to use WSAAsyncSelect - you then receive a message whenever there's a notable event you're interested in, no extra threads required, no looping, and no blocking :wink

In the general case of using multiple threads, it's probably best to use the thread's single argument as a pointer to a structure containing all the information you actually need to pass it. And yes, you 'can' refer to global variables from different threads, but you must be careful when modifying them - two threads modifying the same variable at the same time is trouble.
No snowflake in an avalanche feels responsible.

Slugsnack

Quote from: Tedd on May 15, 2009, 02:32:06 PMAnd yes, you 'can' refer to global variables from different threads, but you must be careful when modifying them - two threads modifying the same variable at the same time is trouble.

critical sections, semaphores, mutexs  :U  fun fun fun !!!   :bg

travism

Got this one all worked out thanks guys! :)