#include <pthread.h>
int pthread_create ( pthread_t *thread, pthread_attr_t *attr, void * (*start_routine) (void *), void *arg );
#include <pthread.h> |
#include <stdio.h> |
/*一直往 stderr 上打印x,参数这里没用.*/ |
void *print_xs(void *unused) |
{ |
while(1) |
fputc('x', stderr); |
return NULL; |
} |
int main() |
{ |
pthread_t thread_id; |
/*创建新进程,新进程将运行 print_xs 函数*/ |
pthread_create(&thread_id, NULL, &print_xs, NULL); |
/*一直在 stderr 上打印 o*/ |
while(1) |
fputc('o',stderr); |
return 0; |
} |
#include <pthread.h>
#include <stdio.h>
/*要传递给线程函数的参数*/
struct char_print_parms
{
char character; /*要打印的字符*/
int count; /*打印字符的次数*/
}
void *char_print (void *parameters)
{
struct char_print_parms *p = (struct char_print_parms*) parameters;
int i;
for(i = 0; i < p->count; i++)
fputc(p->character, stderr);
return NULL;
}
int main()
{
pthread_t pthread1_id;
pthread_t pthread2_id;
struct char_print_parms thread1_args;
struct char_print_parms thread2_args;
thread1_args.character = 'x';
thread1_args.count = 30000;
pthread_create(&pthread1_id, NULL, &char_print, &thread1_args);
thread1_args.character = 'o';
thread1_args.count = 20000;
pthread_create(&pthread2_id, NULL, &char_print, &thread2_args);
printf("hello world\n");
return 0;
}
欢迎光临 曲径通幽论坛 (http://www.groad.net/bbs/) | Powered by Discuz! X3.2 |