linux 多进程互斥,linux 使用互斥量多线程互斥访问

通过使用互斥量可以完成多线程间对变量的互斥访问。主要函数如下:

头文件:#include

函数原型:int pthread_mutex_init(pthread_mutex_t *restrict mutex,const pthread_mutexattr_t *restrict attr);

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

pthread_mutex_init()函数是以动态方式创建互斥锁的,参数attr指定了新建互斥锁的属性。如果参数attr为空,则使用默认的互斥锁属性,默认属性为快速互斥锁 。互斥锁的属性在创建锁的时候指定,在LinuxThreads实现中仅有一个锁类型属性,不同的锁类型在试图对一个已经被锁定的互斥锁加锁时表现不同。

pthread_mutexattr_init()函数成功完成之后会返回零,其他任何返回值都表示出现了错误。

函数成功执行后,互斥锁被初始化为未锁住态。

当pthread_mutex_lock()返回时,该互斥锁已被锁定。线程调用该函数让互斥锁上锁,如果该互斥锁已被另一个线程锁定和拥有,则调用该线程将阻塞,直到该互斥锁变为可用为止。

int pthread_mutex_lock(pthread_mutex_t *mutex);

在成功完成之后会返回零。其他任何返回值都表示出现了错误。

int pthread_mutex_unlock(pthread_mutex_t *mutex);

释放互斥锁,与pthread_mutex_lock成对存在。

#include

#include

#include

#include

#include

#include /* 获得线程ID*/

using namespace std;

#define NUM_Thread 6

int iThread_index;

//获得线程ID

pid_t gettid(void)

{

return syscall(SYS_gettid);

}

//线程调用函数

void *thread_func(void *arg);

//线程互次变量

pthread_mutex_t work_mutex;

int main()

{

int res;

pthread_t a_thread[NUM_Thread];

pthread_mutex_init(&work_mutex,NULL);

void *thread_result;

iThread_index = 0;

for(int iIndex = 0; iIndex < NUM_Thread; iIndex ++)

{

//创建线程

res = pthread_create(&a_thread[iIndex],NULL,thread_func,(void *)&iThread_index);

if(0 != res)

{

perror("create thread failed\n");

exit(-1);

}

}

printf("Waiting for thread to finish\n");

for(int iIndex = 0; iIndex < NUM_Thread; iIndex++)

{

//等待线程结束

res = pthread_join(a_thread[iIndex],&thread_result);

if(0!= res)

{

printf("the %d thread failed\n",iIndex);

}

}

pthread_mutex_destroy(&work_mutex);

printf("press any key exit\n");

char c = getchar();

c = getchar();

}

void *thread_func(void * arg)

{

printf("thread: %5u do something\n", gettid());

int rand_num =rand()%10;

sleep(rand_num);

printf("thread: %5u finish do something\n", gettid());

//互次变量加锁

pthread_mutex_lock(&work_mutex);

int my_num = iThread_index;

printf("the %d thread enter func\n",my_num);

rand_num =rand()%10;

sleep(rand_num);

printf("the %d thread exit func\n",my_num);

iThread_index++;

//互次变量解锁

pthread_mutex_unlock(&work_mutex);

pthread_exit(NULL);

}

结果

administrator@ubuntu:~/桌面/Java/thread$ g++ threadmutex.cpp  -o threadmutex -lpthread

administrator@ubuntu:~/桌面/Java/thread$ ./threadmutex thread:  3888 do something

thread:  3892 do something

thread:  3890 do something

thread:  3889 do something

Waiting for thread to finish

thread:  3891 do something

thread:  3893 do something

thread:  3888 finish do something

the 0 thread enter func

thread:  3891 finish do something

thread:  3890 finish do something

thread:  3893 finish do something

thread:  3892 finish do something

thread:  3889 finish do something

the 0 thread exit func

the 1 thread enter func

the 1 thread exit func

the 2 thread enter func

the 2 thread exit func

the 3 thread enter func

the 3 thread exit func

the 4 thread enter func

the 4 thread exit func

the 5 thread enter func

the 5 thread exit func

press any key exit

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值