求linux上开发C++使用多线程的教程

lalio77 2011-04-06 11:59:56
要在linux上使用C++开发一个项目,看网友介绍说学习C++多线程最好的书是windows核心编程,那个应该讲的是windows的一些API,在linux上不行了吧。。。麻烦高手推荐一些入门的书籍或是方法,感激不尽

P.S. 多线程是跟操作系统有关的? C++实现多线程是通过调用操作系统提供的接口?
...全文
110 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
冻结 2011-04-07
  • 打赏
  • 举报
回复
《Programming with POSIX Threads》
boost thread
赵4老师 2011-04-06
  • 打赏
  • 举报
回复
#include <pthread.h>
int pthread_create(pthread_t *thread, pthread_attr_t *attr, void *(*start_routine)(void *), void *arg) ;
void pthread_exit(void *retval);
int pthread_join(pthread_t th, void **pthread_return) ;

//pthread_join is the thread equivalent of wait that processes use to collect child processes.

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>

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);

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");
}
luciferisnotsatan 2011-04-06
  • 打赏
  • 举报
回复
《linux程序设计》第三版 里有linux下的多线程介绍
也可以google下
giant7 2011-04-06
  • 打赏
  • 举报
回复
Unix高级环境编程
bluesky12312388 2011-04-06
  • 打赏
  • 举报
回复
看APUE(Unix高级环境编程)

 如果只是用多线程,查查相关例子应该够用吧。
 一般都是用POSIX标准的pthread库

15,440

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧