#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    17
    Rep Power
    0

    need help with thread!


    Ok im talking about C++ here:

    anyone know how to create a thread to updating a progress of a certain operation? for example.
    a thread to shows the operation of counting number of words in a file. It's like a progress bar in games where u see the loading bar loading the game before the game starts. But for this program, we dont have to use some type of GUI to show, but just display out the "-" (hyphens) to show the progress.

    I got everything done except the thread part, I dont know how to make the thread to update after show the first amount of progress. In another words, I have no idea how to make the thread to displays more hyphens after first one. if anyone could help thx
  2. #2
  3. Left due to despotic ad-min
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Posts
    1,044
    Rep Power
    14
    In vanilla C or C++, there is no way of creating threads. All concepts related to threading (creating threads, synchronisation, etc etc) receive no mention in the C or C++ standards.

    The solution to your problem is therefore operating system and often compiler dependent --- fortunately, most modern operating systems and compilers support multithreaded applications, although some older ones do not. If you identify your operating system and compiler (with both, include version numbers, if appropriate), you'll probably get a more useful answer.
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    17
    Rep Power
    0
    the operating system is Solaris 9 running Unix . The compiler is g++ .
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    17
    Rep Power
    0
    also, the threads im talking about are POSIX or pthreads .
  8. #5
  9. Left due to despotic ad-min
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Posts
    1,044
    Rep Power
    14
    If you know enough to know you're working with posix threads, I'm surprised you don't already know the answer.

    type in

    man pthread_create

    pthread_create is the posix function for creating threads, and is available (IIRC) in <pthread.h>.

    The third and fourth arguments of pthread_create identify the function to be run by the new thread, and the argument (normally a 32 bit value) received by that function.

    A rather rough and incomplete example follows ....


    #include <pthread.h>

    void *func(void * arg)
    {
    // this is the function.

    unsigned x = (unsigned)arg;
    // x will be 32 because of setup below.
    }

    int main()
    {
    int value;
    pthread_t thread;
    pthread_attr_t attributes;

    // set values in the attributes struct

    unsigned long a_value = 32;
    void *arg = (void *)a_value;

    int value = pthread_create(&thread, &attributes, func, arg);

    // presumably some other actions, such as waiting until
    // the thread completes....

    return 0;
    }
  10. #6
  11. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    17
    Rep Power
    0
    Yea, Iknow how to create a thread and all. But my problem is this:

    typedef struct {
    long *CurrentStatus;
    long InitialValue;
    long TerminationValue;
    } PROGRESS_STATUS
    void *progress_monitor (void *arg){
    //print out the hyphens
    }

    long wordcount (char *filename){
    //open the file
    //a loop to read through each char in the file
    //count the number of words
    //return the number of words
    }

    int main (int argc, char *argv[]){
    //get argument from user which is the file name
    //then call function wordcount
    cout << wordcount(argv[1]) << endl;
    }


    I have to create a progress_monitor thread inside wordcount , so that each time it go through a char (1 byte) , it will update the thread and print out the hyphen , and when it reaches the last char of the file, the thread will stop first and it will print out the number of words of the file.
    My problem is that where do i put the pthread_create inside wordcount and how do I get the thread to update when the loop read through each byte? Do I need to make a loop to create threads (like when it goes through each byte , create a new thread) ???? thx.
  12. #7
  13. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    17
    Rep Power
    0
    comon can anyone help me plz/.
  14. #8
  15. Left due to despotic ad-min
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Posts
    1,044
    Rep Power
    14
    Hmmm...

    Why do you need to do this with multiple threads? Why not just have the function wordcount() print out data on it's status previously? I must admit, doing what you're trying to do with threads seems like a difficult solution to an easy problem: you don't need threads at all.

    But, ignoring that little quibble, I'll assume your purpose is to learn about threads, even if you've picked a bad example..... One way to do it would be to

    1) Have one thread that is launched before wordcount() is even called.

    2) Set up a critical section (or a mutex) that is used to protect data that must be shared between wordcount() and progress_monitor(), which are presumably running on different threads.

    3) Whenever wordcount() has done something worth reporting, enter the critical section (or lock the mutex), write appropriate data on what's happened, and then release the critical section (or unlock the mutex).

    4) In the meantime, progress_monitor() will also need to periodically (presumably in a loop) enter the critical section, interpret the data, do whatever you want on the basis of that data (eg print the hyphens), and then release the critical section.


    This is not actually a particularly good usage of threads. The whole point of using threads is to allow things to happen concurrently (at least, in the sense that the operating system allows switching between threads, or the threads are running on different CPUs). The more you share data between the threads --- which is what needs to happen if you want one thread to monitor another --- the more you force the threads to synchronise with each other (eg one has to wait for the other).
  16. #9
  17. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    17
    Rep Power
    0
    Yea I know that this problem doesn't need threads, and we can just do a regular funtion call or just print directly inside wordcount. But im taking this class called Operating System, and my professor asked us to use threads.
    So the only thing I have to do is to implement critical region right? if so, can I use semaphore? or? thx.
  18. #10
  19. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    17
    Rep Power
    0
    oh one more thing, worcount is not a thread, it's a function that being called from the main routine. (so the main routine always has the main thread, so wordcount is inside that thread, am I right? )
  20. #11
  21. Left due to despotic ad-min
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Posts
    1,044
    Rep Power
    14
    The basic principle of communication between threads is that you don't want one thread modifying something that another is accessing (eg reading, modifying). The only way to achieve that is to ensure that the threads synchronise with each other i.e. while one is doing something, the other is forced to wait.

    Critical sections and mutex's are (conceptually) similar things: only one thread can own one at a time, and other threads must wait for the thread with ownership to release it. The only difference is that some implementations of mutex's (eg under windows) work across processes while mutexes only work between threads in a process. They need to be named differently, so ....

    Semaphores are a little different, as it is possible to have more than one thread with ownership. This sort of thing is useful if a lot of threads read a value, but only one modifies it. A semaphore can be used to control that: multiple threads are allowed to read but, when one thread modifies the data, it can stop all the reads. Another use of semaphores is simply limiting the total number of threads (eg the thread function attempts to grab ownership of the semaphore, and is forced to wait until another thread exits).

    On multi-tasking systems, all functions execute in threads (whether you create a thread or not). The basic logic is that every process starts with one thread, and the main() function is executed by that thread. That is (roughly) referred to as the main thread and, in your example, calles the word_count() function. If you create a thread, you specify what function it executes. That is sometimes called the "thread function"; in your case, that function is progress_monitor().

    [Note: the language describing this sort of stuff varies a bit].
  22. #12
  23. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    17
    Rep Power
    0
    hum...I just asked my professor and she said that this program doesn't need to implement critical region and still runs fine, so what's the solution?
  24. #13
  25. Left due to despotic ad-min
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Posts
    1,044
    Rep Power
    14
    Depends.

    It is possible to get a similar effect by (for example) posting a message to an event processor, and waiting until the message has been processed. IOW, the thread is forced to wait on the event processing thread, so only one of them accesses the relevant data at a time. This is used fairly commonly in windows apps (eg Post a message back to the main GUI loop, and wait for completion). There are some limits on doing this (eg this is not sufficient if there are multiple worker threads accessing the same data, even if all synchronise on the event processing loop).

IMN logo majestic logo threadwatch logo seochat tools logo