Ubuntu13.04, g ++ 4.7, and Pthread implement multi-thread simulation to implement download progress bar

Source: Internet
Author: User
Ubuntu13.04, g ++ 4.7, Pthread implements multi-thread simulation to achieve download progress bar: # include & lt; pthread. h & gt; # include & lt; stdlib. h & gt; # include & lt; unistd. h & gt; # include & lt; iostream & gt; # include & lt; sys/times. h & gt; // use pthread to implement multithreading: one thread simulates download, and one

Ubuntu 13.04, g ++ 4.7, and Pthread implement multi-thread simulation to achieve download progress bar:

# Include
# Include
# Include
# Include
# Include
// Implement multiple threads in pthread: one thread simulates the download and one thread reads the current progress.

// The parameter used by the thread to pass the Parameter
Struct Args {
Int * length_ptr;
Pthread_rwlock_t * rwlock_ptr;
};

Void * download (void * args ){
// Printf ("args address = % p \ n", args );
Args args_object = * (Args *) args;
Args * a = & args_object;
Printf ("start_download OK \ n ");
Printf ("before while length = % d \ n", * a-> length_ptr );
// Printf ("before while length = % p \ n", a-> length_ptr );
While (* a-> length_ptr> 0 ){
// Printf ("in while length = % d \ n", * a-> length_ptr );
// Printf ("in while length = % p \ n", a-> length_ptr );
Int number = rand () % 100;
Pthread_rwlock_wrlock (a-> rwlock_ptr );
* A-> length_ptr-= number;
Printf ("after length-% d; length = % d \ n", number, * a-> length_ptr );
Pthread_rwlock_unlock (a-> rwlock_ptr );
Sleep (1 );
}
Return NULL;
}

Class Ftp {
Private:
Int length;
Static const int LENGTH = 10000000;
Pthread_rwlock_t rwlock;
Public:
Ftp (){
Length = LENGTH;
Int err = pthread_rwlock_init (& rwlock, NULL );
If (err! = 0 ){
// Error
Fprintf (stderr, "Error! \ N ");
}
}
~ Ftp (){
Pthread_rwlock_destroy (& rwlock );
}
Pthread_t start_download (){
Pthread_t thread_id;
Args * a = new Args (); // The thread passing parameter has a problem on the stack, so only one new
A-> length_ptr = & length;
A-> rwlock_ptr = & rwlock;

// Printf ("in start_download and before pthread_create: length = % d \ n", * a-> length_ptr );
// Printf ("in start_download and before pthread_create: length = % p \ n", a-> length_ptr );
// Printf ("in start_download (): args address = % p \ n", );
Int err = pthread_create (& thread_id, NULL, download, );
// Printf ("in start_download and before pthread_create: length = % p \ n", a-> length_ptr );
If (err! = 0 ){
// Error
Fprintf (stderr, "Error! \ N ");
Return-1;
}
Return thread_id;
}
Int get_length (){
Int len =-1;
Pthread_rwlock_rdlock (& rwlock );
Len = length;
Pthread_rwlock_unlock (& rwlock );
Return len;
}
};

Void * test_thread_fun (void * args ){
Ftp * ftp_ptr = static_cast (Args); // The same is true for this thread parameter, except that pthread_join ensures that ftp in main has no destructor.
While (ftp_ptr-> get_length ()> 0 ){
Printf ("read length from test_thread_fun: length = % d \ n", ftp_ptr-> get_length ());
// Sleep (rand () % 5 );
Sleep (1 );
}
}

Int main (){
Srand (0 );
// There is still a lot to do for this program
Ftp ftp;
Pthread_t thread_id = ftp. start_download ();
Pthread_t id;
// Test thread
Int err = pthread_create (& id, NULL, test_thread_fun, & ftp );
If (err! = 0 ){
// Error
Fprintf (stderr, "Error! \ N ");
Return-1;
}

// Wait thread_id exit
Pthread_join (thread_id, NULL );
Return 0;
}

For more information about Ubuntu, see Ubuntu special page http://www.linuxidc.com/topicnews.aspx? Tid = 2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.