Thread: Threading

  1. #1
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265

    Threading

    If I access to the same memory adress at the same time it might have a "little" problem right?
    So how do I solve it? (like checking if a specific adress is in use..)
    Thanks.
    gavra.

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I don't know the C# specific way of doing this, but you can use something called a Semiphore or a Mutex. Well... nifty. I just did a quick google search for "c# semiphore" and this came up.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    "Semaphore". The word has nothing to do with "semi".
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Take a look at the lock statement
    Beware of deadlocks though
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Quote Originally Posted by CornedBee View Post
    "Semaphore". The word has nothing to do with "semi".
    Is a semifore just a fo?

  6. #6
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    First of all thanks (:
    What is the first argument (Semaphore) meant to be for?
    and if I don't want diffrent threads to use the same adress concurrently I need to set the second argument to 1?
    and one more thing what is the diffrence between "lock statement" and "semaphore"?
    Last edited by gavra; 09-17-2008 at 08:12 AM.
    gavra.

  7. #7
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Semaphore is a fancy name for a flag. Similar to a critical section. In this context you woudl test the state of the semaphore prior to entering the critical area and wait to procede until the flag is not set. Once enteing the critical area, you set the flag to rpevent other threads form modifying the protected resource, then you operate ont hat resource and when finished you reset the semaphore. Under windows, use CRITICAL_SECTION's. SEMAPHORE's under windows are generally reserved for interprocess locking, CRITICAL_SECTION's are for intraprocess locking.

  8. #8
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    When you say it, it seems to be very simple (and logical) but I don't see it that way \:
    What is those counters meant to do?
    and way they use array?
    gavra.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You may want to look at the Wiki for semaphore:
    http://en.wikipedia.org/wiki/Semaphore_(programming)

    --
    Mats
    Last edited by matsp; 09-17-2008 at 09:39 AM. Reason: Edit Fix the URL.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    This page doesn't exist yet.
    gavra.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by gavra View Post
    This page doesn't exist yet.
    Because of the parsing of URL's, You have to manually add the missing ). Edit: And I fixed it in the orignal post, so you can click the link.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    oh thanks but the explanation is only connected with the "theory".
    and why isn't it use only true or false?
    gavra.

  13. #13
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by CornedBee View Post
    "Semaphore". The word has nothing to do with "semi".
    Yet when I googled it I did spell semaphore correctly... Go figure. And no, google did not just correct my misspelling. I sleep very little lately... leave me alone guys -_-

  14. #14
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by Magos View Post
    Take a look at the lock statement
    Beware of deadlocks though
    Very cool, Magos. I guess C# programmers can be useful afterall.

  15. #15
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    What you are seeking is the pthread_mutex_lock function in C (or a lock { } block in C#), so only one thread can execute a particular piece of code at a time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c++ threading
    By Anddos in forum C++ Programming
    Replies: 4
    Last Post: 12-28-2005, 03:29 PM
  2. C++ Threading?
    By draggy in forum C++ Programming
    Replies: 5
    Last Post: 08-16-2005, 12:16 PM
  3. Problem with threading
    By osal in forum Windows Programming
    Replies: 6
    Last Post: 07-21-2004, 12:41 PM
  4. starting to learn multi threading
    By hanhao in forum C++ Programming
    Replies: 2
    Last Post: 06-09-2004, 01:44 PM
  5. Threading
    By threads in forum C# Programming
    Replies: 0
    Last Post: 01-17-2003, 11:50 PM