Thread: thread synchronization

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    35

    thread synchronization

    Hi everybody,

    I'm programming an application with severe dependency conditions between three threads. I want to ask you for ADVICE:

    I will try to describe my problem: (Thr1, Thr2, Thr3 are semaphores, but I'm thinking about mutex)

    Thread1

    wait(Thr3) wait(Thr2)

    release(Thr1)

    ==============

    Thread2

    wait(Thr1) wait(Thr3)

    release(Thr2)

    ===============

    Thread3

    wait(Thr2) wait(Thr1)

    release(Thr3)

    =================

    the task begins when thread 1 request data from a machine. When it finishes, thread2 begins to receive the data from the machine and storage it in memory. when thread2 is ready thread 3 can begin to read the data from memory and send it to another machine and at the same time thread1 can request for data again. (actually in the middle of thread2 and thread3 there is another application that makes a processing of the data and when it finishes thread3 can read this data and send it)

    My first question is a beginner question about semaphore and mutex. A semaphore with the maximum count to 1 is like a mutex? I want to have the maximum count for my semaphore to 1 is it the same for me to use semaphore and mutex? I think I could have problems if I use a semaphore with max count=1 because it could be that when thread2 releases (count +1) thread1 and thread3 are waiting, if thread3 takes the control (wait, count -1) thread1 will wait and doesn't take the control anymore? what is your opinion about this?can you give me some advice?

    When I create a semaphore I set the parameter inicial count and with it I can fix what thread begins but with mutex what can I determine what thread begins?

    I need to set thread priority too because when thread3 have something to send must be first (thread1 and thread3 communicate with other machine over the same port).

    What do you think about this idea?

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Let's back up a bit and start with this 3-thread design.
    So you have 3 three threads:

    * Thread 1
    This thread just requests data from a server? Why is this a dedicated thread?

    * Thread 2
    Receives requested data into memory and processes it (via another application).

    * Thread 3
    Sends data somewhere else.

    Have you implemented this algorithm as a single thread? Why 3 threads instead of just 2 - one thread of I/O bound processing, and another for CPU bound processing?

    What OS?

    >> A semaphore with the maximum count to 1 is like a mutex?
    Yes.

    >> with mutex what can I determine what thread begins?
    You typically create all synchronization objects (setting their initial state) prior to starting threads.

    >> I need to set thread priority too ...
    No. A threads priority never has anything to do with the correctness of the algorithm.

    gg

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    35
    Quote Originally Posted by Codeplug View Post
    Why 3 threads instead of just 2 - one thread of I/O bound processing, and another for CPU bound processing?
    because that is design requirements, while thread 2 allocate data in memory thread3 can send data to other machine.

    Quote Originally Posted by Codeplug View Post
    What OS?
    windows xp

    >> A semaphore with the maximum count to 1 is like a mutex?
    Yes.

    >> with mutex what can I determine what thread begins?
    You typically create all synchronization objects (setting their initial state) prior to starting threads.

    >> I need to set thread priority too ...
    No. A threads priority never has anything to do with the correctness of the algorithm.

    gg[/QUOTE]

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    35
    I forgot to say that I need to set priority levels because thread1 and thread3 are using the same ports to send (it works so at the moment with the machine). That is both can want to send at the same time but when thread3 has data to send must be the first to do it....

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    As for how, WaitForSingleObject or WaitForMultipleObjects API functions can allow you to wait until a mutex or semaphore is signaled.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by radeberger View Post
    I forgot to say that I need to set priority levels because thread1 and thread3 are using the same ports to send (it works so at the moment with the machine). That is both can want to send at the same time but when thread3 has data to send must be the first to do it....
    But setting priority will not guarantee correctness:
    1. The OS may well schedule two threads of different priority to separate processors, so even though one has higher priority the other thread is not excluded. To solve this, you need locks to prevent one thread from using a resource when the other is running.
    2. The OS (in case of Windows, Linux and many others - RTOS's may have strict priorities, but Windows even in CE/Mobile form is definitely not a RTOS) may well "feel" that a particular thread hasn't been running for a long while, and decide that even though there is a higher priority task running right now, the lower priority thread must get to run for a bit.

    --
    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.

  7. #7
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> That is both can want to send at the same time but when thread3 has data to send must be the first to do it...
    This is a synchronization problem - which can not be solved using thread priorities.

    gg

  8. #8
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    That is a problem that can only be solved by an additional thread, since any interthread locking mechanism will only give access to the first thread to request it. So you have to have the threads queue their output and have a 4th thread sort the queue adn service requests by the 'priority' thread first. Or that may be what the 3rd thread is for, to service requests by thread 1 and 2 giving priority to say thread 1.

    So lets say TH1 adn TH2 can both send data, but we always want TH1 to have priority in cases where the system is IO bound.

    TH1 posts its send requests to queue 1
    TH2 posts its send requests to queue 2

    TH3 -
    Code:
    pseudo code
    
    start:
    
    if queue1 is not empty
       process first request in queue1
       goto start
    
    if queue2 is not empty
       process first request in queue2
       
    goto start
    Last edited by abachler; 03-27-2009 at 12:27 PM.

  9. #9
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    I haven't really analyzed this much... But your locking pattern looks complicated enough that it might deadlock.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The locking pattern is absolutely guaranteed to lock sooner or later, in fact. But I feel that the whole system is ill-designed, so it's not really worth worrying over. Are these absurd requirements some kind of homework? That thread 1 and 3 must use the same port is stupid - what's the point of having separate threads if they're I/O bound to the same resource?
    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

  11. #11
    Registered User
    Join Date
    Mar 2009
    Posts
    35
    Hi,

    I know it's stupid to have thr1 and thr3 in the same port but I communicate with a machine that use this port (actually I try to change this but at the moment it works so, and no, that's no homework).

    I need to use threads because these tasks must be run parallel (the tcp server must be ready before I request data and later too...it doesn't matter what the others do but the connection with this server must be there...)

  12. #12
    Registered User
    Join Date
    Mar 2009
    Posts
    3
    I'm not that familiar with Win32 synchronization factilities, but using pthreads something like the following might work. I don't think Win32 has a built in concept of wait conditions but they can be implemented using what is provided.

    Code:
    pthread_mutex m1;
    pthread_mutex m2;
    
    pthread_wait_cond_t wait_can_request;
    pthread_wait_cond_t wait_data_to_send;
    
    bool can_request = true;
    bool data_to_read = false;
    bool data_to_send = false;
    
    void
    thr1()
    {
    
       lock(m1);
       while(1)
       {
          if(can_request)
          {
             request_data();
             data_to_read = true;
             can_request = false;
             wake_one(wait_data_to_read);
          }
          else
          {
             wait(wait_can_request, m1);
          }
       }
    
    }
    
    void
    thr2()
    {
       lock(m1);
       while(1)
       {
          if(data_to_read)
          {
             lock(m2);
             read_data();
             data_to_send = true;
             data_to_read = false;
             wake_one(wait_can_request);
             wake_one(wait_data_to_send);
             unlock(m2);
          }
          else
          {
             wait(wait_data_to_read, m1);
          }
       }
    }
    
    void
    thr3()
    {
       lock(m2);
       while(1)
       {
          if(data_to_send)
          {
             send_data();
             data_to_send = false;
          }
          else
          {
             wait(wait_data_to_send, m2);
          }
       }

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I don't think Win32 has a built in concept of wait conditions
    It has since Vista.
    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

  14. #14
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Yes, Win32 has had waits since NT 4.0/95, and probably before that, I dont know for sure because i never programed for 3.0 and earlier, i was hardcore DOS in those days.

    For example -
    Code:
    HANDLE hFile = CreateFile(...); // must create for asynchronous read/write
     
    ReadFile(...);
     
    do some stuff
     
    WaitForSingleObject(hFile , INFINITE);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Thread Synchronization in Win32
    By passionate_guy in forum C Programming
    Replies: 0
    Last Post: 02-06-2006, 05:34 AM
  2. [code] Win32 Thread Object
    By Codeplug in forum Windows Programming
    Replies: 0
    Last Post: 06-03-2005, 03:55 PM
  3. Simple thread object model (my first post)
    By Codeplug in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2004, 11:34 PM
  4. Problem : Threads WILL NOT DIE!!
    By hanhao in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2004, 01:37 PM
  5. Thread Synchronization :: Win32 API vs. MFC
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 08-09-2002, 09:09 AM