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. 一个oracle bug

    最近发现一个RAC db的listener log增长特别快,于是去查看了一下. 先是查看了一下log的内容,发现都是 service_update这种内容,刷新的特别快. service_updat ...

  2. 上机题目(0基础)- 数据库事务(Java)

    /* * 文件名称:JDBCTestCase.java * 版权:Copyright 2006-2011 Huawei Tech. Co. Ltd. All Rights Reserved. * 描写 ...

  3. HDU 3397 Sequence operation 多标记线段树

    /* 一开始维护了两个标记 开了两个数组 想的是 可能当前两种操作都要做 但是太复杂了 不好处理 其实 当前要做的标记可以只有一个 我们在Insert的时候 要打的标记是2即翻转区间: 1.如果原来是 ...

  4. wesome-android

    awesome-android Introduction android libs from github System requirements Android Notice If the lib ...

  5. hibernate初步

    Hibernate开发步骤1.新创建工程并且导入相关的包 主要是hibernate.mysql相关的JAR包. 注意:新导入的hibernate相关的JAR包是否与你当前所使用的jdk版本是否兼容,且 ...

  6. bzoj4034 [HAOI2015]树上操作——树链剖分

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4034 树剖裸题: 一定要注意 long long !!! update 的时候别忘了 pus ...

  7. ortp库使用入门

    我们知道, RTP(Real-timeTransportProtocol)是用于Internet上针对多媒体数据流的一种传输协议,做流媒体传输方面的应用离不开RTP协议的实现及使用,为了更加快速地在项 ...

  8. Java中继承,类的高级概念的知识点

    1. 继承含义 在面向对象编程中,可以通过扩展一个已有的类,并继承该类的属性和行为,来创建一个新的类,这种方式称为继承(inheritance). 2. 继承的优点 A.代码的可重用性 B.子类可以扩 ...

  9. 97. ExtJS之EditorGridPanel afteredit属性

    转自:https://zccst.iteye.com/blog/1328869 1. 之前大多用Ext.grid.GridPanel,现在需要可编辑功能,发现比以前稍复杂一些. 就是需要对指定列进行可 ...

  10. 一段时间加载的js函数

    <html><head><meta charset="utf8"><script type="text/javascript&q ...