Liunx C 線程部分的習題:主線程,子線程,清理函數,加鎖解鎖等問題

Q1: 創建一個子線程,傳入數值1,在子線程中能夠獲取並打印,子線程退出,返回數值2,主線程通過pthread_join獲取等待子線程結束並獲取子線程的退出值並打印。

#include <func.h>
void* threadFunc(void* p)
{
	printf("I am child thread %d\n",*(int*)p);
    pthread_exit((void*)2);
}
int main(int argc,char* argv[])
{
	pthread_t pthid;
    int ret;
    int val=1;
    ret=pthread_create(&pthid,NULL,threadFunc,&val);
    THREAD_ERROR_CHECK(ret,"pthread_create");
    long threadRet;
    pthread_join(pthid,(void**)&threadRet);
    THREAD_ERROR_CHECK(ret,"pthread_join");
    printf("I am main pthread, %ld\n",threadRet);
    return 0;
}

Q2: 創建一個子線程,子線程申請內存,通過清理函數進行free,子線程停留在read標準輸入,主線程cancel子線程,子線程能夠通過清理函數free對應的malloc的內存。

#include <func.h>
void cleanup(void* p)
{
    free(p)printf("free sucess\n");
}
void* threadFunc(void* p)
{
    char buf[128];
    p=malloc(100);
    pthread_clean_up(cleanup,p);
    read(STDIN_FILENO,buf,128);
    pthread_cleanup_pop(1);
    pthread_exit(NULL);
}
int main(int argc,char* argv[])
{
    pthread_t pthid;
    int ret;
    ret=pthread_create(&pthid,NULL,threadFunc,NULL);
    THREAD_ERROR_CHECK(ret,"pthread_create");
    ret=pthread_cancel(pthid);
    THREAD_ERROR_CHECK(ret,"pthread_cancel");
    long threadRet;
    ret=pthread_join(pthid,(void**)&threadRet);
    THREAD_ERROR_CHECK(ret,"pthread_join");
    printf("I am main pthread %ld\n",threadRet);
    return 0;
}

Q3: 創建一個子線程,cancel子線程,在子線程中push兩個清理函數,感受清理函數的執行順序。

#incldue <func.h>
void cleanup1(void* p)
{
    printf("I am cleanup1\n");
}
void cleanup2(void* p)
{
    printf("I am cleanup2\n");
}
void* threadFunc(void* p)
{
    pthread_cleanup_push(cleanup1,p);
    pthread_cleanup_push(cleanup2,p);
    sleep(1);
    pthread_cleanup_pop(1);
    pthread_cleanup_pop(1);
}
int main(int argc,char* argv[])
{
	pthread_t pthid;
    int ret;
    ret=pthread_create(&pthid,NULL,threadFunc,NULL);
    THREAD_ERROR_CHECK(ret,"pthread_create");
    ret=pthread_cancel(pthid);
    THREAD_ERROR_CHECK(ret,"pthread_cancel");
    long threadRet;
    ret=pthread_join(pthid,(void**)&threadRet);
 	THREAD_ERROR_CHECK(ret,"pthread_join");
    printf("I am main thread %d\n",threadRet);
    return 0;
}

Q4: 子線程,主線程,同時對一個全局變量加2千萬,通過加鎖,實現最終效果是4千萬。

#include <func.h>
#define N 20000000
typedef struct{
    int val;
    pthread_mutex_t mutex;
}Data_t;
void* threadFunc(void* p)
{
    int i;
    Data_t *pVal = (Data_t*)p;
    for(i=0;i<N;i++)
    {
        pthread_mutex_lock(&pVal->mutex);
        pVal->val+=1;
        pthread_mutex_unlock(&pVal->mutex);
    }
}
int main(int argc,char* argv[])
{
    pthread_t pthid;
    int ret;
    Data_t threadInfo;
    threadInfo.val=0;
    ret=pthread_mutex_init(&threadInfo.mutex,NULL);
    THREAD_ERROR_CHECK(ret,"pthread_mutex_init");
    struct timeval start, end;
    gettimeofday(&start,NULL);
    ret=pthread_create(&pthid,NULL,threadFunc,&threadInfo);
    THREAD_ERROR_CHECK(ret,"pthread_create");
    int i;
    for(i=0;i<N;i++)
    {
        pthread_mutex_lock(&threadInfo.mutex);
        threadInfo.val+=1;
        pthread_mutex_unlock(&threadInfo.mutex);
    }
    pthread_join(pthid,NULL);   //等待子線程
    gettimeofday(&end,NULL);
    printf("I am main pthread,val=%d,use_time=%ld\n",threadInfo.val,(end.tv_sec-start.tv_sec)*1000000+end.tv_usec-start.tv_usec);
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章