search
HomeOperation and MaintenanceLinux Operation and MaintenanceThere are several methods for thread synchronization

There are several methods for thread synchronization

What are the methods of thread synchronization? Under Linux, the system provides many ways to achieve thread synchronization, the most commonly used of which are mutex locks, condition variables and semaphores. There may be many partners who are not familiar with these three methods. The following Let me introduce it to you in detail.

Three methods to achieve thread synchronization under Linux:

1. Mutex lock (mutex)

Through lock Mechanism to achieve synchronization between threads.

1. Initialize the lock. Under Linux, the thread's mutex data type is pthread_mutex_t. Before use, it must be initialized.

Static allocation: pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

Dynamic allocation: int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutex_attr_t *mutexattr);

2. Lock. To access shared resources, the mutex must be locked. If the mutex is already locked, the calling thread will block until the mutex is unlocked.

int pthread_mutex_lock (pthread_mutex *mutex);

int pthread_mutex_trylock (pthread_mutex_t *mutex);

3. Unlock. After completing the access to the shared resource, the mutex must be unlocked.

int pthread_mutex_unlock(pthread_mutex_t *mutex);

4. Destroy the lock. After the lock is used, it needs to be destroyed to release resources.

int pthread_mutex_destroy(pthread_mutex *mutex);

#include <cstdio>
#include <cstdlib>
#include <unistd.h>
#include <pthread.h>
#include "iostream"
using namespace std;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
int tmp;
void* thread(void *arg)
{
cout << "thread id is " << pthread_self() << endl;
pthread_mutex_lock(&mutex);
tmp = 12;
cout << "Now a is " << tmp << endl;
pthread_mutex_unlock(&mutex);
return NULL;
}
int main()
{
pthread_t id;
cout << "main thread id is " << pthread_self() << endl;
tmp = 3;
cout << "In main func tmp = " << tmp << endl;
if (!pthread_create(&id, NULL, thread, NULL))
{
cout << "Create thread success!" << endl;
}
else
{
cout << "Create thread failed!" << endl;
}
pthread_join(id, NULL);
pthread_mutex_destroy(&mutex);
return 0;
}
//编译:g++ -o thread testthread.cpp -lpthread

Related recommendations: "PHP Getting Started Tutorial"

2. Condition variables (cond)

Different from mutex locks, condition variables are used to wait instead of locking. Condition variables are used to automatically block a thread until a special situation occurs. Usually condition variables and mutex locks are used together. Condition variables are divided into two parts: conditions and variables. The condition itself is protected by a mutex. The thread must lock the mutex before changing the conditional state. Condition variables allow us to sleep and wait for a certain condition to occur. Condition variables are a mechanism for synchronizing using global variables shared between threads. It mainly includes two actions: one thread waits for "the condition of the condition variable to be true" and hangs; the other thread makes "the condition is true" (given that the condition is true) Signal). Detection of conditions is performed under the protection of a mutex lock. If a condition is false, a thread automatically blocks and releases the mutex waiting for the state to change. If another thread changes the condition, it signals the associated condition variable, wakes up one or more threads waiting on it, reacquires the mutex, and reevaluates the condition. If two processes share readable and writable memory, condition variables can be used to achieve thread synchronization between the two processes.

1. Initialize condition variables.

Static initialization, pthread_cond_t cond = PTHREAD_COND_INITIALIER;

Dynamic initialization, int pthread_cond_init (pthread_cond_t *cond, pthread_condattr_t *cond_attr);

2. Wait for the condition to be established. Release the lock and block waiting for the condition variable to be true. timewait() sets the waiting time, and if there is no signal, returns ETIMEOUT (locking ensures that there is only one thread wait)

int pthread_cond_wait (pthread_cond_t *cond, pthread_mutex_t *mutex);

int pthread_cond_timewait (pthread_cond_t *cond, pthread_mutex *mutex, const timespec *abstime);

3. Activate condition variables. pthread_cond_signal, pthread_cond_broadcast (activate all waiting threads)

int pthread_cond_signal (pthread_cond_t *cond);

int pthread_cond_broadcast (pthread_cond_t *cond); //Unblock all threads

4. Clear the condition variable. No thread is waiting, otherwise EBUSY

int pthread_cond_destroy(pthread_cond_t *cond);

[cpp] view plain copy
#include <stdio.h>
#include <pthread.h>
#include "stdlib.h"
#include "unistd.h"
pthread_mutex_t mutex;
pthread_cond_t cond;
void hander(void *arg)
{
free(arg);
(void)pthread_mutex_unlock(&mutex);
}
void *thread1(void *arg)
{
pthread_cleanup_push(hander, &mutex);
while(1)
{
printf("thread1 is running\n");
pthread_mutex_lock(&mutex);
pthread_cond_wait(&cond, &mutex);
printf("thread1 applied the condition\n");
pthread_mutex_unlock(&mutex);
sleep(4);
}
pthread_cleanup_pop(0);
}
void *thread2(void *arg)
{
while(1)
{
printf("thread2 is running\n");
pthread_mutex_lock(&mutex);
pthread_cond_wait(&cond, &mutex);
printf("thread2 applied the condition\n");
pthread_mutex_unlock(&mutex);
sleep(1);
}
}
int main()
{
pthread_t thid1,thid2;
printf("condition variable study!\n");
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&cond, NULL);
pthread_create(&thid1, NULL, thread1, NULL);
pthread_create(&thid2, NULL, thread2, NULL);
sleep(1);
do
{
pthread_cond_signal(&cond);
}while(1);
sleep(20);
pthread_exit(0);
return 0;
}
#include <pthread.h>
#include <unistd.h>
#include "stdio.h"
#include "stdlib.h"
static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
struct node
{
int n_number;
struct node *n_next;
}*head = NULL;
static void cleanup_handler(void *arg)
{
printf("Cleanup handler of second thread./n");
free(arg);
(void)pthread_mutex_unlock(&mtx);
}
static void *thread_func(void *arg)
{
struct node *p = NULL;
pthread_cleanup_push(cleanup_handler, p);
while (1)
{
//这个mutex主要是用来保证pthread_cond_wait的并发性
pthread_mutex_lock(&mtx);
while (head == NULL)
{
//这个while要特别说明一下,单个pthread_cond_wait功能很完善,为何
//这里要有一个while (head == NULL)呢?因为pthread_cond_wait里的线
//程可能会被意外唤醒,如果这个时候head != NULL,则不是我们想要的情况。
//这个时候,应该让线程继续进入pthread_cond_wait
// pthread_cond_wait会先解除之前的pthread_mutex_lock锁定的mtx,
//然后阻塞在等待对列里休眠,直到再次被唤醒(大多数情况下是等待的条件成立
//而被唤醒,唤醒后,该进程会先锁定先pthread_mutex_lock(&mtx);,再读取资源
//用这个流程是比较清楚的
pthread_cond_wait(&cond, &mtx);
p = head;
head = head->n_next;
printf("Got %d from front of queue/n", p->n_number);
free(p);
}
pthread_mutex_unlock(&mtx); //临界区数据操作完毕,释放互斥锁
}
pthread_cleanup_pop(0);
return 0;
}
int main(void)
{
pthread_t tid;
int i;
struct node *p;
//子线程会一直等待资源,类似生产者和消费者,但是这里的消费者可以是多个消费者,而
//不仅仅支持普通的单个消费者,这个模型虽然简单,但是很强大
pthread_create(&tid, NULL, thread_func, NULL);
sleep(1);
for (i = 0; i < 10; i++)
{
p = (struct node*)malloc(sizeof(struct node));
p->n_number = i;
pthread_mutex_lock(&mtx); //需要操作head这个临界资源,先加锁,
p->n_next = head;
head = p;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mtx); //解锁
sleep(1);
}
printf("thread 1 wanna end the line.So cancel thread 2./n");
//关于pthread_cancel,有一点额外的说明,它是从外部终止子线程,子线程会在最近的取消点,退出
//线程,而在我们的代码里,最近的取消点肯定就是pthread_cond_wait()了。
pthread_cancel(tid);
pthread_join(tid, NULL);
printf("All done -- exiting/n");
return 0;
}

3. Semaphore (sem)

Like processes, threads can also communicate through semaphores, although they are lightweight. The names of semaphore functions all start with "sem_". There are four basic semaphore functions used by threads.

1. Semaphore initialization.

int sem_init (sem_t *sem, int pshared, unsigned int value);

This is to initialize the semaphore specified by sem and set up its sharing option (Linux only supports 0, which means it is a local semaphore of the current process), and then give it an initial value VALUE.

2. Wait for the semaphore. Decrement the semaphore by 1 and wait until the semaphore value is greater than 0.

int sem_wait(sem_t *sem);

3. Release the semaphore. Increase the semaphore value by 1. And notify other waiting threads.

int sem_post(sem_t *sem);

4. Destroy the semaphore. We clean up the semaphore after we use it. Return all resources possessed.

int sem_destroy(sem_t *sem);

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>
#include <errno.h>
#define return_if_fail(p) if((p) == 0){printf ("[%s]:func error!/n", __func__);return;}
typedef struct _PrivInfo
{
sem_t s1;
sem_t s2;
time_t end_time;
}PrivInfo;
static void info_init (PrivInfo* thiz);
static void info_destroy (PrivInfo* thiz);
static void* pthread_func_1 (PrivInfo* thiz);
static void* pthread_func_2 (PrivInfo* thiz);
int main (int argc, char** argv)
{
pthread_t pt_1 = 0;
pthread_t pt_2 = 0;
int ret = 0;
PrivInfo* thiz = NULL;
thiz = (PrivInfo* )malloc (sizeof (PrivInfo));
if (thiz == NULL)
{
printf ("[%s]: Failed to malloc priv./n");
return -1;
}
info_init (thiz);
ret = pthread_create (&pt_1, NULL, (void*)pthread_func_1, thiz);
if (ret != 0)
{
perror ("pthread_1_create:");
}
ret = pthread_create (&pt_2, NULL, (void*)pthread_func_2, thiz);
if (ret != 0)
{
perror ("pthread_2_create:");
}
pthread_join (pt_1, NULL);
pthread_join (pt_2, NULL);
info_destroy (thiz);
return 0;
}
static void info_init (PrivInfo* thiz)
{
return_if_fail (thiz != NULL);
thiz->end_time = time(NULL) + 10;
sem_init (&thiz->s1, 0, 1);
sem_init (&thiz->s2, 0, 0);
return;
}
static void info_destroy (PrivInfo* thiz)
{
return_if_fail (thiz != NULL);
sem_destroy (&thiz->s1);
sem_destroy (&thiz->s2);
free (thiz);
thiz = NULL;
return;
}
static void* pthread_func_1 (PrivInfo* thiz)
{
return_if_fail(thiz != NULL);
while (time(NULL) < thiz->end_time)
{
sem_wait (&thiz->s2);
printf ("pthread1: pthread1 get the lock./n");
sem_post (&thiz->s1);
printf ("pthread1: pthread1 unlock/n");
sleep (1);
}
return;
}
static void* pthread_func_2 (PrivInfo* thiz)
{
return_if_fail (thiz != NULL);
while (time (NULL) < thiz->end_time)
{
sem_wait (&thiz->s1);
printf ("pthread2: pthread2 get the unlock./n");
sem_post (&thiz->s2);
printf ("pthread2: pthread2 unlock./n");
sleep (1);
}
return;
}

The above are the three commonly used methods to achieve thread synchronization under Linux. As we all know, the biggest highlight of threads It is resource sharing, and the thread synchronization problem in resource sharing is a major difficulty.

The above is the detailed content of There are several methods for thread synchronization. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Java语言线程同步和互斥的实现方法Java语言线程同步和互斥的实现方法Jun 10, 2023 am 09:43 AM

Java语言是在早期引入了多线程的语言,线程的运用使得Java语言在程序的并发处理方面大放异彩。然而线程间的同步问题和互斥问题一直是编程过程中的关键。在Java语言中,线程同步和互斥的实现方法有许多,本文将介绍其中的几种方法。一、使用synchronized关键字实现同步和互斥synchronized是Java语言中最基础的实现同步和互斥的方式。在Java中

C#开发中如何处理线程同步和并发访问问题C#开发中如何处理线程同步和并发访问问题Oct 08, 2023 pm 12:16 PM

C#开发中如何处理线程同步和并发访问问题,需要具体代码示例在C#开发中,线程同步和并发访问问题是一个常见的挑战。由于多个线程可以同时访问和操作共享数据,可能会出现竞态条件和数据不一致的问题。为了解决这些问题,我们可以使用各种同步机制和并发控制方法来确保线程之间的正确协作和数据一致性。互斥锁(Mutex)互斥锁是一种最基本的同步机制,用于保护共享资源。在需要访

如何使用Java中的锁机制实现线程同步?如何使用Java中的锁机制实现线程同步?Aug 02, 2023 pm 01:47 PM

如何使用Java中的锁机制实现线程同步?在多线程编程中,线程同步是一个非常重要的概念。当多个线程同时访问和修改共享资源时,可能会导致数据不一致或竞态条件的问题。Java提供了锁机制来解决这些问题,并确保线程安全的访问共享资源。Java中的锁机制由synchronized关键字和Lock接口提供。接下来,我们将学习如何使用这两种机制来实现线程同步。使用sync

PHP文件下载方法及常见问题解答PHP文件下载方法及常见问题解答Jun 09, 2023 pm 12:37 PM

PHP是一个广泛使用的服务器端编程语言,它的许多功能和特性可以将其用于各种任务,包括文件下载。在本文中,我们将了解如何使用PHP创建文件下载脚本,并解决文件下载过程中可能出现的常见问题。一、文件下载方法要在PHP中下载文件,我们需要创建一个PHP脚本。让我们看一下如何实现这一点。创建下载文件的链接通过HTML或PHP在页面上创建一个链接,让用户能够下载文件。

Go 语言中的方法是怎样定义和使用的?Go 语言中的方法是怎样定义和使用的?Jun 10, 2023 am 08:16 AM

Go语言是近年来备受青睐的编程语言,因其简洁、高效、并发等特点而备受开发者喜爱。其中,方法(Method)也是Go语言中非常重要的概念。接下来,本文就将详细介绍Go语言中方法的定义和使用。一、方法的定义Go语言中的方法是带有接收器(Receiver)的函数,它是一个与某个类型绑定的函数。接收器可以是值类型或者指针类型。用于接收者的参数可以在方法名

Vue 中的 createApp 方法是什么?Vue 中的 createApp 方法是什么?Jun 11, 2023 am 11:25 AM

随着前端开发的快速发展,越来越多的框架被用来构建复杂的Web应用程序。Vue.js是流行的前端框架之一,它提供了许多功能和工具来简化开发人员构建高质量的Web应用程序。createApp()方法是Vue.js中的一个核心方法之一,它提供了一种简单的方式来创建Vue实例和应用程序。本文将深入探讨Vue中createApp方法的作用,其如何使用以及使用时需要了解

C#开发中如何处理线程同步和并发访问问题及解决方法C#开发中如何处理线程同步和并发访问问题及解决方法Oct 08, 2023 am 09:55 AM

C#开发中如何处理线程同步和并发访问问题及解决方法随着计算机系统和处理器的发展,多核处理器的普及使得并行计算和多线程编程变得非常重要。在C#开发中,线程同步和并发访问问题是我们经常面临的挑战。没有正确处理这些问题,可能会导致数据竞争(DataRace)、死锁(Deadlock)和资源争用(ResourceContention)等严重后果。因此,本篇文章将

Java中的线程同步和互斥机制Java中的线程同步和互斥机制Jun 16, 2023 am 10:09 AM

Java中的线程同步和互斥机制在Java中,多线程是一个重要的技术。要高效地并发执行多个任务,需要掌握线程之间的同步和协作机制。本文将介绍Java中的线程同步和互斥机制。线程同步线程同步指的是多个线程在执行过程中,通过合作来完成指定的任务。多个线程执行的代码段互斥地访问共享资源,在执行完一段代码后,只有一个线程能够访问共享资源,其他线程需要等待。线程同步遵循

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool