Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using synchronous sockets

Status
Not open for further replies.

s1ckboy

Programmer
Mar 9, 2003
1
US
Hi, I am writing a program where I need to execute server-side code whenever a connection is accepted to a server sockect. The problem I am having is when two connections are received at the same time. Since the accepting code needs to be executed as an entire transaction, I would like to use the threaded synchronous sockets that borlands socket components offer, only it doesn't seem to work. Does anyone have any tips for using this feature? Thanks for your help!!!
 
I'm not exactly sure what you are asking. If you mean that you only want one thread to execute your accepting code at a time, you can use a mutex. There are many ways of doing this. Here's something I often use for simple tasks. Call Lock() at the beginning of your accepting code and call Unlock() at the end.

class Mutex {
private:
CRITICAL_SECTION lock;

public:
__fastcall Mutex(void) {
InitializeCriticalSection(&lock);
}

__fastcall ~Mutex() {
DeleteCriticalSection(&lock);
}

void __fastcall Lock(void) {
EnterCriticalSection(&lock);
}

void __fastcall Unlock(void) {
LeaveCriticalSection(&lock);
}
};
 
there are issues when working with multiple threads, i.e when two threads are trying to do the same thing at the same time. I'm not adept at thread programming but I know that you must ensure that the threads are syncronized. so to speak they need to be able to look outside of themselves for the ok to do something. Your threads are perhaps knocking heads. The logic of thread programming is more intense.

goto



you must take heed the notice in the help files to write threadsafe code.

tomcruz.net
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top