#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("\nThread creation failed!\n");
exit(EXIT_FAILURE);
}
printf("Waiting for thread to finish...\n");
res = pthread_join(a_thread, &thread_result);
if(res != 0) {
perror("\nThread join failed!\n");
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 is: %s\n", (char *)arg);
sleep(3);
strcpy(message, "Bye!");
pthread_exit("Thank you for your CPU time!");
}
#include
#include
#include
int flag = 1;
void *thread_function(void *arg);
int main()
{
int res;
pthread_t a_thread;
void *thread_result;
int count = 1;
res = pthread_create(&a_thread, NULL, thread_function, NULL);
if(res != 0) {
perror("\nThread creation failed!\n");
exit(EXIT_FAILURE);
}
while(count++ <= 10) {
if(flag == 1) {
printf("1");
flag = 2;
} else {
sleep(1);
}
}
printf("\nWaiting for thread to finish...\n");
res = pthread_join(a_thread, &thread_result);
if(res != 0) {
perror("\nThread join failed!\n");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
void *thread_function(void *arg)
{
int count = 1;
while(count++ <= 10) { /* 不共享进程的局部变量count */
if(flag == 2) { /* 共享进程的全局变量flag */
printf("2");
flag = 1;
} else {
sleep(1);
}
}
}
#include
#include
#include
#include
#include
#define SIZE 1024
char buffer[SIZE] = {0};
void *thread_function(void *arg);
sem_t sem;
int main()
{
int res;
pthread_t a_thread;
void *thread_result;
res = sem_init(&sem, 0, 0);
if(res != 0) {
perror("\nSem init failed!\n");
exit(EXIT_FAILURE);
}
res = pthread_create(&a_thread, NULL, thread_function, NULL);
if(res != 0) {
perror("\nThread create failed!\n");
exit(EXIT_FAILURE);
}
printf("Input some text, Enter 'end' to finish: \n");
while(scanf("%s", buffer)) {
sem_post(&sem);
if(strncmp("end", buffer, 3) == 0) {
break;
}
}
printf("Waiting for thread to finish...\n");
res = pthread_join(a_thread, &thread_result);
if(res != 0) {
perror("\nThread join failed!\n");
exit(EXIT_FAILURE);
}
printf("Thread join!\n");
sem_destroy(&sem);
exit(EXIT_SUCCESS);
}
void *thread_function(void *arg)
{
sem_wait(&sem);
while(strncmp("end", buffer, 3) != 0) {
printf("You input %d characters/n", strlen(buffer));
sem_wait(&sem);
}
pthread_exit(NULL);
}
与其他函数一样,成功时返回0,失败时将返回错误代码,但这些函数并不设置errno,所以必须对函数的返回代码进行检查。互斥量的属性设置这里不讨论,因此设置成NULL。我们用互斥量来重写刚才的代码如下(thread4.c):
#include
#include
#include
#include
#include
#define SIZE 1024
char buffer[SIZE] = {0};
void *thread_function(void *arg);
pthread_mutex_t mutex;
int main()
{
int res;
pthread_t a_thread;
void *thread_result;
res = pthread_mutex_init(&mutex, NULL);
if(res != 0) {
perror("\nMutex init failed!");
exit(EXIT_FAILURE);
}
res = pthread_create(&a_thread, NULL, thread_function, NULL);
if(res != 0) {
perror("\nThread create failed!");
exit(EXIT_FAILURE);
}
printf("Input some text, Enter 'end' to finish: \n");
while(1) {
pthread_mutex_lock(&mutex);
scanf("%s", buffer);
pthread_mutex_unlock(&mutex);
if(strncmp("end", buffer, 3) == 0) {
break;
}
sleep(1);
}
res = pthread_join(a_thread, &thread_result);
if(res != 0) {
perror("\nThread join failed!");
exit(EXIT_FAILURE);
}
printf("Thread joined\n");
pthread_mutex_destroy(&mutex);
exit(EXIT_SUCCESS);
}
void *thread_function(void *arg)
{
sleep(1);
while(1) {
pthread_mutex_lock(&mutex);
printf("You input %d characters\n", strlen(buffer));
pthread_mutex_unlock(&mutex);
if(strncmp("end", buffer, 3) == 0) {
break;
}
sleep(1);
}
}
编译这个程序:gcc -D _REENTRANT thread4.c -o thread4 -lpthread,可见,程序运行结果与thread3.c相同。
#include
#include
#include
#include
void *thread_function(void *arg);
char message[] = "Hello World!";
int thread_finished = 0;
int main()
{
int res;
pthread_t a_thread;
pthread_attr_t thread_attr;
res = pthread_attr_init(&thread_attr);
if(res != 0) {
perror("\nAttribute creation failed!\n");
exit(EXIT_FAILURE);
}
res = pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED); // 分离于主线程
if(res != 0) {
perror("\nSetting detached attribute failed!\n");
exit(EXIT_FAILURE);
}
res = pthread_create(&a_thread, &thread_attr, thread_function, (void *)message);
if(res != 0) {
perror("\nThread creation failed!\n");
exit(EXIT_FAILURE);
}
pthread_attr_destroy(&thread_attr);
while(!thread_finished) {
printf("Waiting for thread to say it's finished...\n");
sleep(1);
}
printf("Other thread finished, Bye!\n");
exit(EXIT_SUCCESS);
}
void *thread_function(void *arg)
{
printf("thread_function() is running. Argument was %s\n", (char *)arg);
sleep(4);
printf("Second thread setting finished flag, and exiting now\n");
thread_finished = 1;
pthread_exit(NULL);
}
#include
#include
#include
void *thread_function(void *arg);
int main()
{
int res;
pthread_t a_thread;
void *thread_result;
res = pthread_create(&a_thread, NULL, thread_function, NULL);
if(res != 0) {
perror("\nThread create failed!\n");
exit(EXIT_FAILURE);
}
sleep(4);
printf("Canceling thread...\n");
res = pthread_cancel(a_thread);
if(res != 0) {
perror("\nThread cancel failed!\n");
exit(EXIT_FAILURE);
}
printf ("Waiting for thread to finished...\n");
res = pthread_join(a_thread, &thread_result);
if(res != 0) {
perror ("\nThread join failed!\n");
exit(EXIT_FAILURE);
}
printf("Thread canceled!");
exit(EXIT_FAILURE);
}
void *thread_function(void *arg)
{
int i;
int res;
res = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); // 可以被取消
if(res != 0) {
perror("\nThread setcancelstate failed!\n");
exit(EXIT_FAILURE);
}
res = pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL); // 等待阻塞后再结束
if(res != 0) {
perror("\nThread setcanceltype failed!\n");
exit(EXIT_FAILURE);
}
printf("thread_function is running...\n");
for(i = 0; i < 10; i++) {
printf("Thread is still running (%d)...\n", i);
sleep(1);
}
pthread_exit(0);
}
#include
#include
#include
#define NUM 6
void *thread_function(void *arg);
int main()
{
int res;
pthread_t a_thread[NUM];
void *thread_result;
int index;
for(index = 0; index < NUM; index++) {
res = pthread_create(&a_thread[index], NULL, thread_function, (void *)index);
if(res != 0) {
perror("\nThread create failed!\n");
exit(EXIT_FAILURE);
}
sleep(1);
}
printf("Waiting for threads to finished...\n");
for(index = NUM - 1; index >= 0; index--) {
res = pthread_join(a_thread[index], &thread_result);
if(0 == res) {
printf("Picked up a thread:%d\n", index + 1);
} else {
perror("\npthread_join failed!\n");
}
}
printf("All done\n");
exit(EXIT_SUCCESS);
}
void *thread_function(void *arg)
{
int my_number = (int)arg;
int rand_num;
printf("thread_function is running. Argument was %d\n", my_number);
rand_num = 1 + (int)(9.0 * rand()/(RAND_MAX + 1.0));
sleep(rand_num);
printf("Bye from %d\n", my_number);
pthread_exit(NULL);
}
本文主要介绍了Linux环境下的多线程编程,介绍了信号量和互斥量、线程属性控制、线程同步、线程终止、取消线程及多线程并发。本文只作为Linux多线程编程入门之用。