pthread_create()的基本使用
2016-07-28 16:06
363 查看
Linux系统下使用线程
其中变量为:typedef unsigned long int pthread_t;
1建立一个线程:
//hellothread.c
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *function(void *arg);
int main(int argc, char *argv[]){
pthread_t thread1;
pthread_create(&thread1,NULL,function,(char*)"111");
pthread_join(thread1,NULL);
return 0;
}
void *function(void *arg){
char *m;
m = (char *)arg;
while(*m != '\0') {
printf("%c",*m);
fflush(stdout);
m++;
sleep(1);
}
printf("\n");
}
2多个线程同时执行:
因为使用了while()所以需要手动Ctrl+c进行中断程序运行;
3使用线程互斥锁进行分别执行:
//hellothread.c
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *function(void *arg);
pthread_mutex_t mutex;//互斥锁
int main(int argc, char *argv[]){
pthread_t thread1, thread2, thread3;
pthread_mutex_init(&mutex,NULL);
pthread_create(&thread1,NULL,function,(char*)"111");
pthread_create(&thread2,NULL,function,(char*)"222");
pthread_create(&thread3,NULL,function,(char*)"333");
while (1) {
sleep(1);
}
return 0;
}
void *function(void *arg)
{
char *m;
m = (char *)arg;
pthread_mutex_lock(&mutex);
while(*m != '\0')
{
printf("%c",*m);
fflush(stdout);
m++;
sleep(1);
}
printf("\n");
pthread_mutex_unlock(&mutex);
}
*tips:
案例1中调用pthread_join()等待线程结束后退出主程序,
也可以使用while()循环来阻住程序退出;
推荐使用pthread_join();
int pthread_create(); 返回值为0时线程创建成功;
存在的疑问:多次测试均是线程执行顺序与建立顺序相反;
其中变量为:typedef unsigned long int pthread_t;
1建立一个线程:
//hellothread.c
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *function(void *arg);
int main(int argc, char *argv[]){
pthread_t thread1;
pthread_create(&thread1,NULL,function,(char*)"111");
pthread_join(thread1,NULL);
return 0;
}
void *function(void *arg){
char *m;
m = (char *)arg;
while(*m != '\0') {
printf("%c",*m);
fflush(stdout);
m++;
sleep(1);
}
printf("\n");
}
2多个线程同时执行:
//hellothread.c #include <stdio.h> #include <stdlib.h> #include <pthread.h> void *function(void *arg); pthread_mutex_t mutex; int main(int argc, char *argv[]){ pthread_t thread1, thread2, thread3; pthread_mutex_init(&mutex,NULL); pthread_create(&thread1,NULL,function,(char*)"111"); pthread_create(&thread2,NULL,function,(char*)"222"); pthread_create(&thread3,NULL,function,(char*)"333"); while (1) { sleep(1); } return 0; } void *function(void *arg){ char *m; m = (char *)arg; while(*m != '\0') { printf("%c",*m); fflush(stdout); m++; sleep(1); } printf("\n"); }
因为使用了while()所以需要手动Ctrl+c进行中断程序运行;
3使用线程互斥锁进行分别执行:
//hellothread.c
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *function(void *arg);
pthread_mutex_t mutex;//互斥锁
int main(int argc, char *argv[]){
pthread_t thread1, thread2, thread3;
pthread_mutex_init(&mutex,NULL);
pthread_create(&thread1,NULL,function,(char*)"111");
pthread_create(&thread2,NULL,function,(char*)"222");
pthread_create(&thread3,NULL,function,(char*)"333");
while (1) {
sleep(1);
}
return 0;
}
void *function(void *arg)
{
char *m;
m = (char *)arg;
pthread_mutex_lock(&mutex);
while(*m != '\0')
{
printf("%c",*m);
fflush(stdout);
m++;
sleep(1);
}
printf("\n");
pthread_mutex_unlock(&mutex);
}
*tips:
案例1中调用pthread_join()等待线程结束后退出主程序,
也可以使用while()循环来阻住程序退出;
推荐使用pthread_join();
int pthread_create(); 返回值为0时线程创建成功;
存在的疑问:多次测试均是线程执行顺序与建立顺序相反;
相关文章推荐
- jQuery EasyUI DataGrid 编辑列下拉框做级联操作
- 华为devCloud
- win7下 安装 source code pro
- One English Sentence
- 讯飞语音识别最简Demo实现(1)
- Android ImageView属性adjustViewBounds和ScaleType的介绍
- xShell终端调用VBS脚本 使用方法说明
- Ubuntu14.04下使用Telnet
- 使用ajaxfileupload.js进行文件上传,文件可以上传成功,但是不走success回调函数解决办法。
- Java编程中的equals方法使用全解
- linux下的java调用ffmpeg进行视频转码
- Moogoose操作之Schema实现增删查改
- Android中的ProgressBar的android:indeterminate
- cmake 生成目标三种方式
- HTML篇之CSS样式——CSS水平垂直居中对齐(多种方式)
- SessionID
- ubuntu 的 python从2.* 升级到3.*
- JVM实用参数(八)GC日志
- hdu 5774 Bubble Sort(树状数组)
- 生活就像北京的地铁1