设为首页 加入收藏

TOP

用Pthreads进行共享内存编程----HelloWorld
2014-11-24 07:37:39 来源: 作者: 【 】 浏览:1
Tags:Pthreads 进行 共享 内存 编程 ----HelloWorld

1、编译


gcc -g -Wall -o pth_hello pth_hello.c -lpthread


2、运行


./pth_hello


例:运行4个线程,./pth_hello 4


3、源代码


#include
#include
#include


int thread_count;


void * Hello(void * rank);


int main(int argc, char * argv[])
{
long thread;
pthread_t * thread_handles;


thread_count = strtol(argv[1], NULL, 10);


thread_handles = malloc(thread_count * sizeof(pthread_t));


for(thread=0 ; thread pthread_create(&thread_handles[thread], NULL, Hello, (void *)thread);


printf("Hello from the main thread\n");


for(thread=0; thread pthread_join(thread_handles[thread], NULL);


free(thread_handles);
return 0;
}


void * Hello(void * rank)
{
long my_rank = (long)rank;
printf("Hello from thread %ld of %d\n", my_rank, thread_count);
return NULL;
}


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇串行直方图程序 下一篇golang中的defer panic recover

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·PostgreSQL 索引 - (2025-12-25 22:20:43)
·MySQL Node.js 连接 (2025-12-25 22:20:41)
·SQL 撤销索引、表以 (2025-12-25 22:20:38)
·Linux系统简介 (2025-12-25 21:55:25)
·Linux安装MySQL过程 (2025-12-25 21:55:22)