A simple program with threads. It also demostrates how the parameters are passed to the thread functions.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <pthread.h>
#define MAX_1 5
#define MAX_2 10
/* ref: http://en.wikipedia.org/wiki/POSIX_Threads */
static void wait(void)
{
time_t start_time = time(NULL);
while (time(NULL) == start_time)
{
/* do nothing except chew CPU slices for up to one second */
}
}
static void *thread_func1(void *vptr_args)
{
int i;
for (i = 1; i <= MAX_2; i++) {
printf("[#1] (%2d/%2d) - %s\n", i, MAX_2, (char *)vptr_args);
wait();
}
printf("[#1] --- End of #1 ---\n");
return NULL;
}
static void *thread_func2(void *vptr_args)
{
int i;
for (i = 1; i <= MAX_2; i++) {
printf("[#2] (%2d/%2d) - t_count = %d\n", i, MAX_2, (*(int *)vptr_args)++);
wait();
}
printf("[#2] --- End of #2 ---\n");
return NULL;
}
int main(void)
{
int i;
pthread_t thread_1, thread_2;
char *t_param = "Hello Thread !";
int t_count = 1;
pthread_create(&thread_1, NULL, thread_func1, (void *) t_param );
pthread_create(&thread_2, NULL, thread_func2, (void *) &t_count);
for (i = 1; i <= MAX_1; i++) {
printf("[#0] (%2d/%2d) - t_count = %d \n", i, MAX_1, t_count);
wait();
}
printf("[#0] --- End of #0 ---\n");
// pthread_join(thread_1, NULL);
// pthread_join(thread_2, NULL);
printf("[ ] ------ End ------\n");
return EXIT_SUCCESS;
}
Compile the source code with -lpthread.
$ gcc pthread.c -o pthread -lpthread
$ ./pthread
[#1] ( 1/10) - Hello Thread !
[#2] ( 1/10) - t_count = 1
[#0] ( 1/ 5) - t_count = 2
[#0] ( 2/ 5) - t_count = 2
[#1] ( 2/10) - Hello Thread !
[#2] ( 2/10) - t_count = 2
[#2] ( 3/10) - t_count = 3
[#1] ( 3/10) - Hello Thread !
[#0] ( 3/ 5) - t_count = 4
[#1] ( 4/10) - Hello Thread !
[#2] ( 4/10) - t_count = 4
[#0] ( 4/ 5) - t_count = 5
[#1] ( 5/10) - Hello Thread !
[#2] ( 5/10) - t_count = 5
[#0] ( 5/ 5) - t_count = 6
[#2] ( 6/10) - t_count = 6
[#0] --- End of #0 ---
[ ] ------ End ------
Oh ! Both threads are not completed when main program terminates.
Uncomment both pthread_join() to wait threads terminate,
pthread_join(thread_1, NULL);
pthread_join(thread_2, NULL);
$ gcc pthread.c -o pthread_join -lpthread
$ ./pthread_join
[#1] ( 1/10) - Hello Thread !
[#2] ( 1/10) - t_count = 1
[#0] ( 1/ 5) - t_count = 2
[#0] ( 2/ 5) - t_count = 2
[#1] ( 2/10) - Hello Thread !
[#2] ( 2/10) - t_count = 2
[#1] ( 3/10) - Hello Thread !
[#2] ( 3/10) - t_count = 3
[#0] ( 3/ 5) - t_count = 4
[#1] ( 4/10) - Hello Thread !
[#0] ( 4/ 5) - t_count = 4
[#2] ( 4/10) - t_count = 4
[#1] ( 5/10) - Hello Thread !
[#0] ( 5/ 5) - t_count = 5
[#2] ( 5/10) - t_count = 5
[#0] --- End of #0 ---
[#1] ( 6/10) - Hello Thread !
[#2] ( 6/10) - t_count = 6
[#2] ( 7/10) - t_count = 7
[#1] ( 7/10) - Hello Thread !
[#2] ( 8/10) - t_count = 8
[#1] ( 8/10) - Hello Thread !
[#1] ( 9/10) - Hello Thread !
[#2] ( 9/10) - t_count = 9
[#1] (10/10) - Hello Thread !
[#2] (10/10) - t_count = 10
[#1] --- End of #1 ---
[#2] --- End of #2 ---
[ ] ------ End ------
Main thread will wait another two threads by thread_join().
.
沒有留言:
張貼留言