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?的更多相关文章

  1. Mutex和内存可见性

    http://ifeve.com/mutex-and-memory-visibility/ POSIX内存可见性规则 IEEE 1003.1-2008定义了XBD 4.11内存同步中的内存可见性规则. ...

  2. 【Linux】Mutex互斥量线程同步的例子

    0.互斥量  Windows下的互斥量 是个内核对象,每次WaitForSingleObject和ReleaseMutex时都会检查当前线程ID和占有互斥量的线程ID是否一致. 当多次Wait**时就 ...

  3. pthread_create()之前的属性设置

    一.pthread_create()之前的属性设置1.线程属性设置我们用pthread_create函数创建一个线程,在这个线程中,我们使用默认参数,即将该函数的第二个参数设为NULL.的确,对大多数 ...

  4. 线程异常:undefined reference to &#39;pthread_create&#39; 处理

    源代码: #include <stdio.h> #include <pthread.h> #include <sched.h> void *producter_f ...

  5. UNIX环境高级编程——pthread_create的问题

    linux 下常用的创建多线程函数pthread_create(pthread_t * thread , pthread_attr_t * attr , void *(*start_routine)( ...

  6. mutex,thread

    //#include <stdio.h> //#include <stdlib.h> //#include <unistd.h> #include <wind ...

  7. 互斥量mutex的简单使用

    几个重要的函数: #include <pthread.h> int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pt ...

  8. linux mutex

    #include <iostream> #include <queue> #include <cstdlib> #include <unistd.h> ...

  9. C++多线程同步之Mutex(互斥量)

    原文链接: http://blog.csdn.net/olansefengye1/article/details/53086141 一.互斥量Mutex同步多线程 1.Win32平台 相关函数和头文件 ...

随机推荐

  1. Android图片异步载入框架Android-Universal-Image-Loader

    Android-Universal-Image-Loader是一个图片异步载入,缓存和显示的框架.这个框架已经被非常多开发人员所使用,是最经常使用的几个Android开源项目之中的一个,主流的应用,随 ...

  2. ASP.NET MVC 4源代码分析之怎样定位控制器

    利用少有的空余时间.具体的浏览了下ASP.NET MVC 4的源代码.照着之前的步伐继续前进(尽管博客园已经存在非常多大牛对MVC源代码分析的博客,可是从个人出发.还是希望自己可以摸索出这些). 首先 ...

  3. log_archive_dest_1设置报错

    DG搭建完之后,又报错: Tue Dec 22 16:24:33 2015 Errors in file /u01/app/oracle/admin/orcl/bdump/orcl_arc1_2994 ...

  4. android 音乐播放器总结

    学习从模仿開始 一个星期完毕的音乐播放器基本功能,具有下一首,上一首,暂停和随机.顺序和单曲等播放.以及保存上一次播放的状态,缺少了歌词显示功能.使用了andbase框架的欢迎动画和界面title. ...

  5. android获取当前软件版本号号和版本号名称

    <span style="font-size:18px;">/** * 获取本地软件版本号 */ public static int getLocalVersion(C ...

  6. zoj1940

    链接:点击打开链接 题意:三维搜索'S'为起点,'E'为终点,求走出的最短时间 代码: #include <iostream> #include <stdio.h> #incl ...

  7. mysql 忘记了root的password(linux下解决方法,window同理)

    mysql 忘记了root的password的时候的解决步骤, 1: cd /etc/mysql/(进入mysql的配置文件夹) 2:vim my.cnf \skip-grant-tables(进入m ...

  8. luogu3084 Photo 单调队列优化DP

    题目大意 农夫约翰决定给站在一条线上的N(1 <= N <= 200,000)头奶牛制作一张全家福照片,N头奶牛编号1到N.于是约翰拍摄了M(1 <= M <= 100,000 ...

  9. android按钮被点击文字颜色变化效果

    有的时候做应用需要点击按钮时文字颜色也跟着变,松开后又还原,目前发现两种解决方案:第一用图片,如果出现的地方比较多,那么图片的量就相当可观:第二,也就是本文讲到的.废话少说,先贴图片,再上代码. 正常 ...

  10. 269D

    扫描线+dp 先对坐标排序,然后·用set维护端点,每次插入左端点,扫描到右端点时删除.每次考虑新插入时分割了哪两个木板,自己分别连边,再删除原来的边,最后dp(好像得维护used,有环) #incl ...