Public Group

This topic is 2775 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

How do you tell a thread that something has happend and it needs to execute a certain function?

##### Share on other sites
Simplest way can just be an global bool variable, but make sure both threads dont write to it, one thread can just read.

##### Share on other sites
That is along the lines of what I was thinking. But, I wasn't sure if that was the right way or the best way.

Is it possible that there would be timing issues with this though?

For example, if the main thread sent requests 1000 times per second and the thread could only process at 500 times per second. Every second request could possibly get lost couldn't it?

##### Share on other sites
Simplest way can just be an global bool variable, but make sure both threads dont write to it, one thread can just read.
That may work but it's also extremely dangerous without proper synchronisation.

Any variable that is shared between threads must be correctly synchronised if you don't want to get bitten by unforeseen random bugs.LockMutex( g_BoolMutex ); g_bool = true; UnlockMutex( g_BoolMutex );
For example, if the main thread sent requests 1000 times per second and the thread could only process at 500 times per second. Every second request could possibly get lost couldn't it?
The most important question here is what language/APIs are you using?
Instead of a bool, your use a container, such as a queue.
You can use any kind of variable shared between the threads, as long as you protect it with proper synchronisation. On Windows, the usual method is a "critical section" (AKA a "mutex" in comp-sci literature).std::deque<int> g_Queue; LPCRITICAL_SECTION g_QueueMutex; void Init_BeforeStartingTheThreads() { InitializeCriticalSection(&g_QueueMutex);//create the mutex } void Thread1_Write() { int message = 42; // do something to generate a message EnterCriticalSection(&g_QueueMutex);//wait until no one is holding the mutex, then grab it for ourselves g_Queue.push_back( message );//this thread "owns" the queue variable right now, so it's safe to do anything with it LeaveCriticalSection(&g_QueueMutex);//we're done using the queue for now, so let go of the mutex } void Thread2_Read() { int message; bool wasThereAMessage = false; EnterCriticalSection(&g_QueueMutex); if( !g_Queue.empty() ) { wasThereAMessage = true; message = g_Queue.pop_front(); } LeaveCriticalSection(&g_QueueMutex); if( wasThereAMessage ) { std::cout << message;// do something to consume the message } }

##### Share on other sites
Thanks for the detailed reply Hodgman. I'll look into that so I can understand it fully.

I was actually thinking of 'deque' as a possibilty earlier. That would work well, I think. I just have to get to grips with [color="#660066"]EnterCriticalSection() and so on. I haven't had a great deal to do with threading. But I get what you are saying there.

With what I have in mind with my app, if things go out of sync one little bit, it would be ugly.

BTW, I am using C++ and Winsock as the main API in question.

• 12
• 16
• 26
• 10
• 44
• ### Forum Statistics

• Total Topics
633759
• Total Posts
3013720
×