Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread control using semaphores...well i think thats what i need

 
Ben Corben
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, so im new here ^^ hello,

Ill start by explaining how my server works, each level creates the level below.

Mainthread

DatabaseGatewaythread

Client1thread Client2thread Client3thread Client4thread

DatabaseConn DatabaseConn DatabaseConn DatabaseConn


OK so now the problem i have is i want the client threads to share a semaphore so i can make sure only one Database connection
is present at one time. But the only way i can see of doing that is to have it in a parent thread which the child threads couldn't access, is
there anyway sibling threads could access the same semaphore?


if anyone understands this id appreciate any ideas, comments regarding my stupidity may also lead to results so are equally welcome.

 
Henry Wong
author
Marshal
Pie
Posts: 21514
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

A semaphore is just an object -- so it can be shared between threads, just like any other object.

Henry
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The easy way would be to centralize the place from where you get the connection (statically available class) and hence put your semaphore there.

Normally people use a datasource or a connection pool.
 
Ben Corben
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks guys, in the end i just declared the semaphore as public static and it worked. il look into "centralize the place from where you get the connection" as well.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic