linux多线程 pthread用法
生活随笔
收集整理的這篇文章主要介紹了
linux多线程 pthread用法
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
#include
int pthread_create(pthread_t *restrict tidp,const pthread_attr_t *restrict attr,
void *(*start_rtn)(void),void *restrict arg);
Returns: 0 if OK, error number on failure
第一個(gè)參數(shù)為指向線程標(biāo)識(shí)符的指針。
第二個(gè)參數(shù)用來(lái)設(shè)置線程屬性。
第三個(gè)參數(shù)是線程運(yùn)行函數(shù)的起始地址。
第四個(gè)參數(shù)是運(yùn)行函數(shù)的參數(shù)。
當(dāng)創(chuàng)建線程成功時(shí),函數(shù)返回0,若不為0則說(shuō)明創(chuàng)建線程失敗,常見(jiàn)的錯(cuò)誤返回代碼為EAGAIN和EINVAL。前者表示系統(tǒng)限制創(chuàng)建新的線程,例如線程數(shù)目過(guò)多了;后者表示第二個(gè)參數(shù)代表的線程屬性值非法.
pthread_create的用法:由于pthread庫(kù)不是Linux系統(tǒng)默認(rèn)的庫(kù),所以在使用pthread_create創(chuàng)建線程時(shí),需要在編譯中請(qǐng)加-lpthread參數(shù),eg:gcc -o test -lpthrea test.c
例1:
#include "pthread.h"
#include "stdio.h"
void* thread_test(void* ptr)
{ while(1)
printf("i am pthread\n");
}
int main()
{
pthread_t pid;
pthread_create(&pid, NULL, test_thread, NULL);
while(1)
printf("i am main pthread\n");
return 0;
}
例2:
#include
#include
pthread_t id;
int ret;
void thread_1()
{
while(1)
{printf(“I am thread\n”);
sleep(1);
}
}
main()
{ret = pthread_create(&id,NULL,(void*)thread_1,NULL);
if(ret != 0)
printf("Create pthread error!\n");
while(1)
{
printf(“I am main thread\n”);
sleep(2);
}
}
例3:
#include
#include
#include
#include
void *thread_function(void *arg);
char message[] = "Hello World";
int main()
{
int res;
pthread_t a_thread;
void *thread_result;
res = pthread_create(&a_thread, NULL, thread_function, (void *)message);
if (res != 0)
{
perror("Thread creation failed");
exit(EXIT_FAILURE);
}
printf("Waiting for thread to finish...\n");
res = pthread_join(a_thread, &thread_result); //pthread_join 阻塞執(zhí)行的線程直到某線程結(jié)束
if (res != 0)
{
perror("Thread join failed");
exit(EXIT_FAILURE);
}
printf("Thread joined, it returned %s\n", (char *)thread_result);
printf("Message is now %s\n", message);
exit(EXIT_SUCCESS);
}
void *thread_function(void *arg)
{
printf("thread_function is running. Argument was %s\n", (char *)arg);
sleep(3);
strcpy(message, "Bye!");
pthread_exit("Thank you for the CPU time");
}
[root@plinux tmp]# cc -D_REENTRANT -I/usr/include/nptl thread2.c -o thread2 -L/usr/lib/nptl -lpthread
[root@plinux tmp]# ./thread2
thread_function is running. Argument was Hello World
Waiting for thread to finish...
Thread joined, it returned Thank you for the CPU time
Message is now Bye!
pthread_join()
void pthread_exit(void *retval)
int pthread_join(pthread_t pid, void **thread_return)
pthread_join()的調(diào)用者將掛起并等待th線程終止,retval是調(diào)用pthread_exit()的線程(線程ID為pid)的返回值,如果thread_return不為NULL,則*thread_return=retval。
需要注意的是一個(gè)線程僅允許唯一的另一個(gè)線程使用 pthread_join()等待本線程的終止,并且被等待的線程應(yīng)該處于可join狀態(tài),即非DETACHED狀態(tài)。
int pthread_create(pthread_t *restrict tidp,const pthread_attr_t *restrict attr,
void *(*start_rtn)(void),void *restrict arg);
Returns: 0 if OK, error number on failure
第一個(gè)參數(shù)為指向線程標(biāo)識(shí)符的指針。
第二個(gè)參數(shù)用來(lái)設(shè)置線程屬性。
第三個(gè)參數(shù)是線程運(yùn)行函數(shù)的起始地址。
第四個(gè)參數(shù)是運(yùn)行函數(shù)的參數(shù)。
當(dāng)創(chuàng)建線程成功時(shí),函數(shù)返回0,若不為0則說(shuō)明創(chuàng)建線程失敗,常見(jiàn)的錯(cuò)誤返回代碼為EAGAIN和EINVAL。前者表示系統(tǒng)限制創(chuàng)建新的線程,例如線程數(shù)目過(guò)多了;后者表示第二個(gè)參數(shù)代表的線程屬性值非法.
pthread_create的用法:由于pthread庫(kù)不是Linux系統(tǒng)默認(rèn)的庫(kù),所以在使用pthread_create創(chuàng)建線程時(shí),需要在編譯中請(qǐng)加-lpthread參數(shù),eg:gcc -o test -lpthrea test.c
例1:
#include "pthread.h"
#include "stdio.h"
void* thread_test(void* ptr)
{ while(1)
printf("i am pthread\n");
}
int main()
{
pthread_t pid;
pthread_create(&pid, NULL, test_thread, NULL);
while(1)
printf("i am main pthread\n");
return 0;
}
例2:
#include
#include
pthread_t id;
int ret;
void thread_1()
{
while(1)
{printf(“I am thread\n”);
sleep(1);
}
}
main()
{ret = pthread_create(&id,NULL,(void*)thread_1,NULL);
if(ret != 0)
printf("Create pthread error!\n");
while(1)
{
printf(“I am main thread\n”);
sleep(2);
}
}
例3:
#include
#include
#include
#include
void *thread_function(void *arg);
char message[] = "Hello World";
int main()
{
int res;
pthread_t a_thread;
void *thread_result;
res = pthread_create(&a_thread, NULL, thread_function, (void *)message);
if (res != 0)
{
perror("Thread creation failed");
exit(EXIT_FAILURE);
}
printf("Waiting for thread to finish...\n");
res = pthread_join(a_thread, &thread_result); //pthread_join 阻塞執(zhí)行的線程直到某線程結(jié)束
if (res != 0)
{
perror("Thread join failed");
exit(EXIT_FAILURE);
}
printf("Thread joined, it returned %s\n", (char *)thread_result);
printf("Message is now %s\n", message);
exit(EXIT_SUCCESS);
}
void *thread_function(void *arg)
{
printf("thread_function is running. Argument was %s\n", (char *)arg);
sleep(3);
strcpy(message, "Bye!");
pthread_exit("Thank you for the CPU time");
}
[root@plinux tmp]# cc -D_REENTRANT -I/usr/include/nptl thread2.c -o thread2 -L/usr/lib/nptl -lpthread
[root@plinux tmp]# ./thread2
thread_function is running. Argument was Hello World
Waiting for thread to finish...
Thread joined, it returned Thank you for the CPU time
Message is now Bye!
pthread_join()
void pthread_exit(void *retval)
int pthread_join(pthread_t pid, void **thread_return)
pthread_join()的調(diào)用者將掛起并等待th線程終止,retval是調(diào)用pthread_exit()的線程(線程ID為pid)的返回值,如果thread_return不為NULL,則*thread_return=retval。
需要注意的是一個(gè)線程僅允許唯一的另一個(gè)線程使用 pthread_join()等待本線程的終止,并且被等待的線程應(yīng)該處于可join狀態(tài),即非DETACHED狀態(tài)。
總結(jié)
以上是生活随笔為你收集整理的linux多线程 pthread用法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: window 之命令行的cd
- 下一篇: Windows静态库和动态库的调用方法汇