Unix/Linux 编程:多线程编程
2017-12-10 14:00
211 查看
之前已经用过不少在Windows和C++上多多线程编程技术来。
在Linux上原理大致和Windows是相同多。主要多区别在于了解POSIX规范和一些API
参考文档:http://www.cnblogs.com/skynet/archive/2010/10/30/1865267.html
实际上笔者认为创建线程池和使用epoll(windows上的完成端口)可能更适合开发商用应用程序。但是还是需要理解线程同步多本质。
有关线程操作的函数
运行结果:
在Linux上原理大致和Windows是相同多。主要多区别在于了解POSIX规范和一些API
参考文档:http://www.cnblogs.com/skynet/archive/2010/10/30/1865267.html
实际上笔者认为创建线程池和使用epoll(windows上的完成端口)可能更适合开发商用应用程序。但是还是需要理解线程同步多本质。
有关线程操作的函数
#include <pthread.h> int pthread_create(pthread_t *tid, const pthread_attr_t *attr, void *(*func) (void *), void *arg); int pthread_join (pthread_t tid, void ** status); pthread_t pthread_self (void); int pthread_detach (pthread_t tid); void pthread_exit (void *status);
/* 是否熟悉POSIX多线程编程技术?如熟悉,编写程序完成如下功能: 1)有一int型全局变量g_Flag初始值为0; 2)在主线称中起动线程1,打印“this is thread1”,并将g_Flag设置为1 3)在主线称中启动线程2,打印“this is thread2”,并将g_Flag设置为2 4)线程序1需要在线程2退出后才能退出 5)主线程在检测到g_Flag从1变为2,或者从2变为1的时候退出 */
/* File: mt.c multithread demo */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <pthread.h> #include <errno.h> #include <unistd.h> // global variable. int g_Flag = 0; static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; static pthread_cond_t cond = PTHREAD_COND_INITIALIZER; // thread function 1 void * thread1(void* ); // thread function 2 void * thread2(void* ); /* * when the process is initialized, the main thread is creatd by auto. * So we just need to create two children thread. */ int main(int argc, char ** argv) { printf("Enter main thread.\n"); pthread_t tid1, tid2; int rc1 = 0, rc2 = 0; // create the thread2 rc2 = pthread_create(&tid2, NULL, thread2, NULL); if(rc2 != 0) printf("%s: %s\n", __func__, strerror(rc2)); // create the thread1 rc1 = pthread_create(&tid1, NULL, thread1, &tid2); if(rc1 != 0) printf("%s: %s\n", __func__, strerror(rc1)); pthread_cond_wait(&cond, &mutex); printf("Leave main thread.\n"); exit(0); } /* * thread1() will execute by pthread_Create * it will set g_Flag = 1. */ void * thread1(void * arg) { printf("Enter thread1.\n"); printf("this is thread1, g_Flag: %d, thread id is %u.\n", g_Flag, (unsigned int)pthread_self()); pthread_mutex_lock(&mutex); if(g_Flag == 2) pthread_cond_signal(&cond); g_Flag = 1; printf("this is thread1, g_Flag: %d, thread id is %u.\n", g_Flag, (unsigned int)pthread_self()); pthread_mutex_unlock(&mutex); // wait for the thread2 to be exited at first. pthread_join(*(pthread_t *)arg, NULL); printf("Leave thread1.\n"); pthread_exit(0); } /* * thread2() will execute by pthread_Create * it will set g_Flag = 2. */ void * thread2(void * arg) { printf("Enter thread2.\n"); printf("this is thread2, g_Flag: %d, thread id is %u.\n", g_Flag, (unsigned int)pthread_self()); pthread_mutex_lock(&mutex); if(g_Flag == 1) pthread_cond_signal(&cond); g_Flag = 2; printf("this is thread2, g_Flag: %d, thread id is %u.\n", g_Flag, (unsigned int)pthread_self()); pthread_mutex_unlock(&mutex); printf("Leave thread2.\n"); pthread_exit(0); }
运行结果:
Enter main thread. Enter thread2. this is thread2, g_Flag: 0, thread id is 162025472. Enter thread1. this is thread1, g_Flag: 0, thread id is 162562048. this is thread2, g_Flag: 2, thread id is 162025472. Leave thread2. this is thread1, g_Flag: 1, thread id is 162562048. Leave main thread. LintaodeMBP:MultiThread stjohnson$
相关文章推荐
- Linux/Unix下Socket编程
- Unix/Linux环境C编程入门教程(14) Mandriva LinuxCCPP开发环境搭建
- UNIX环境高级编程——Linux系统调用列表
- Unix/Linux环境下的Socket编程
- 《Unix-Linux编程实践教程》读书笔记(七)
- Linux/Unix C/C++编程 Hello World篇 (图)
- Unix-Linux编程实践教程——第九章
- Linux下的多线程编程 转载 自 http://linux.chinaunix.net/doc/program/2001-08-11/642.shtml
- 【linux 编程】linux/unix 进程的创建
- 学习linux/unix编程方法的建议,学习Linux的四个步骤
- Linux/Unix下ODBC的安装、配置与编程
- UNIX/Linux编程相关工具和资源 .
- UNIX环境高级编程——Linux进程地址空间和虚拟内存
- Unix/Linux环境C编程入门教程(14) Mandriva LinuxCCPP开发环境搭建
- UNIX环境高级编程——Linux系统调用列表
- UNIX环境高级编程——Linux终端设备详解
- 【Unix/Linux编程实践】文件系统:编写pwd
- 【Unix/Linux编程实践】shell如何运行程序—编写命令解析器sh
- 深入浅出Shell 编程:Unix/Linux 命令
- Unix/Linux编程之size_t、ssize_t