How to use pthread_create && mutex?
1
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
void *thread_function(void *arg)
{
int i;
for (i=0; i<5; i++){
printf("Thread says hi!\n");
sleep(1);
}
return NULL;
}
int main(void)
{
pthread_t mythread;
if ( pthread_create( &mythread, NULL, thread_function, NULL) ){
printf("error creating thread.\n");
abort();
}
if ( pthread_join( mythread, NULL )){
printf("error joining thread.");
abort();
}
exit(0);
}
gcc -o pthread1 pthread1.c -lpthread
2
#cat thread2.c
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int myglobal;
void *thread_function(void *arg)
{
int i,j;
for (i=0; i<20; i++){
i=myglobal;
j=j+1;
printf(".");
fflush(stdout);
sleep(1);
myglobal=j;
}
return NULL;
}
int main(void)
{
pthread_t mythread;
int i;
if (pthread_create(&mythread, NULL, thread_function, NULL)){
printf("Error creating thread.");
abort();
}
for (i=0; i<20; i++){
myglobal=myglobal+1;
printf("o");
fflush(stdout);
sleep(1);
}
if (pthread_join(mythread, NULL)){
printf("Error joining.");
abort();
}
printf("\nmyglobal equals %d\n ",myglobal);
exit(0);
}
3. Use mutex
#cat thread3.c
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
int myglobal;
pthread_mutex_t mymutex=PTHREAD_MUTEX_INITIALIZER;
void *thread_function(void *arg)
{
int i,j;
for (i=0; i<10; i++){
pthread_mutex_lock(&mymutex);
j=myglobal;
j=j+1;
printf(".");
fflush(stdout);
sleep(1);
myglobal=j;
pthread_mutex_unlock(&mymutex);
}
return NULL;
}
int main(void)
{
pthread_t mythread;
int i;
if (pthread_create(&mythread, NULL, thread_function, NULL)){
printf("Error creating function;");
abort();
}
for (i=0; i<10; i++){
pthread_mutex_lock(&mymutex);
myglobal=myglobal+1;
pthread_mutex_unlock(&mymutex);
printf("o");
fflush(stdout);
sleep(1);
}
if (pthread_join(mythread, NULL)){
printf("Error joining thread.");
abort();
}
printf("myglobal equals %d\n",myglobal);
exit(0);
}
REF
https://www.ibm.com/developerworks/cn/linux/thread/posix_thread2/index.html
How to use pthread_create && mutex?的更多相关文章
- Mutex和内存可见性
http://ifeve.com/mutex-and-memory-visibility/ POSIX内存可见性规则 IEEE 1003.1-2008定义了XBD 4.11内存同步中的内存可见性规则. ...
- 【Linux】Mutex互斥量线程同步的例子
0.互斥量 Windows下的互斥量 是个内核对象,每次WaitForSingleObject和ReleaseMutex时都会检查当前线程ID和占有互斥量的线程ID是否一致. 当多次Wait**时就 ...
- pthread_create()之前的属性设置
一.pthread_create()之前的属性设置1.线程属性设置我们用pthread_create函数创建一个线程,在这个线程中,我们使用默认参数,即将该函数的第二个参数设为NULL.的确,对大多数 ...
- 线程异常:undefined reference to 'pthread_create' 处理
源代码: #include <stdio.h> #include <pthread.h> #include <sched.h> void *producter_f ...
- UNIX环境高级编程——pthread_create的问题
linux 下常用的创建多线程函数pthread_create(pthread_t * thread , pthread_attr_t * attr , void *(*start_routine)( ...
- mutex,thread
//#include <stdio.h> //#include <stdlib.h> //#include <unistd.h> #include <wind ...
- 互斥量mutex的简单使用
几个重要的函数: #include <pthread.h> int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pt ...
- linux mutex
#include <iostream> #include <queue> #include <cstdlib> #include <unistd.h> ...
- C++多线程同步之Mutex(互斥量)
原文链接: http://blog.csdn.net/olansefengye1/article/details/53086141 一.互斥量Mutex同步多线程 1.Win32平台 相关函数和头文件 ...
随机推荐
- POJ2777 Count Color 线段树区间更新
题目描写叙述: 长度为L个单位的画板,有T种不同的颜料.现要求按序做O个操作,操作分两种: 1."C A B C",即将A到B之间的区域涂上颜色C 2."P A B&qu ...
- 微信小程序的小问题(2)
1.在小程序中,有时需要用到背景图片,但是如果使用background-image的话,就无法控制图片的大小,background-image一般用于将图片压缩为1像素的背景图片,然后自动填充铺满.使 ...
- Android之怎样使用ListView列表视图
ListView 列表视图创建方法: (1)直接使用ListView 组件创建 (2)让Activity继承ListActivity实现 第一种:在XML中直接使用ListView 组件创建 在val ...
- C++ 移位运算与进制转换 浅析
移位运算包括"逻辑移位"(logical shift)和"算术移位"(arithmetic shift). 逻辑移位:移出去的位丢弃,空缺位(vacant bi ...
- React-Router 中文简明教程(上)
概述 说起 前端路由,如果你用过前端 MV* 框架构建 SPA 应用(单页面应用),对此一定不陌生. 传统开发中的 路由,是由服务端根据不同的用户请求地址 URL,返回不同内容的页面,而前端路由则将这 ...
- Codeforces 2018-2019 ICPC, NEERC, Southern Subregional Contest
2018-2019 ICPC, NEERC, Southern Subregional Contest 闲谈: 被操哥和男神带飞的一场ACM,第一把做了这么多题,荣幸成为7题队,虽然比赛的时候频频出锅 ...
- Rails5 关联表格搜索
创建: 2017/08/13 other_type_car = Car.joins(:car_type).active.find_by(car_type: car_type) @recomme ...
- Html5 ajax的跨域请求
1.XMLHttpRequest升级版已经实现了跨域请求.不过需要在后台设置:header("Access-Control-Allow-Origin:http://www.a.com&quo ...
- Django day08 多表操作 (一) 多表模型创建
多表模型创建分析:1)作者表:一个作者有姓名和年龄2)作者信息表: 有作者就有信息,点击作者的名字可以查询他的电话和地址, 作者表对应作者信息表,所以他们之间是一对一对的关系3)出版社表: 出版社有对 ...
- idea使用svn
一.在idea中配置svn 二.导出maven项目 连接svn 点击checkout 点击ok就可以 到Commit Changes 这里有几个选项需要了解的: Auto-update after c ...