歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Ubuntu 13.04,g++4.7,Pthread實現多線程模擬實現下載進度條

Ubuntu 13.04,g++4.7,Pthread實現多線程模擬實現下載進度條:

#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#include <iostream>
#include <sys/times.h>
//pthread實現多線程:一個線程模擬下載,一個線程讀取當前進度

//線程用的參數,用於傳遞參數
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();//線程傳遞參數放在棧上有問題,所以只能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", a);
        int err = pthread_create(&thread_id, NULL, download, a);
        //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<Ftp*>(args);//這個線程參數也是一樣,只不過因為有pthread_join保證了main中的ftp沒有析構
    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;
}

更多Ubuntu相關信息見Ubuntu 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=2

Copyright © Linux教程網 All Rights Reserved