Multi-Threading programming basics of Linux Programming (ii) __HTML5

Source: Internet
Author: User

The previous article simply said the basic operation of Linux multithreaded programming, this article to simply say that the synchronization between the threads and mutual exclusion. First of all multithreading synchronization and mutual exclusion of the basic concepts and operations, and then write a small example to test.

first, basic concepts and operations

Because threads are shared between resources and storage, the synchronization and mutex issues between multithreading must be considered when manipulating and accessing these resources. There are two kinds of thread synchronization mechanisms in POSIX: mutexes and semaphores. A mutex comparison applies to situations where resources available at the same time are unique, and semaphores are more appropriate for situations where resources are available at the same time. This article focuses on the use of mutual exclusion locks.

Mutex Common API Description:

A, initialization of a mutual exclusion lock

int Pthread_mutex_init (pthread_mutex_t *mutex, const pthread_mutexattr_t *mutexattr)
Mutex: Creating an identity for a mutex

Mutexattr: The properties of the mutex created, there are mainly three kinds, respectively: Pthread_mutex_initializer, create a fast mutex; pthread_recursive_mutex_initializer_np, create a recursive mutex ; pthread_errorcheck_mutex_initializer_np, creating a checkout mutex. The default is a fast mutex.

Return value: 0 successful, error return wrong code

b, other API functions

int Pthread_mutex_lock (pthread_mutex_t *mutex,)		//Mutex lock
int Pthread_mutex_trylock (pthread_mutex_t *mutex,)	//Mutex
to judge the lock int pthread_mutex_unlock (pthread_mutex_t *mutex,)	//Mutex unlock
int Pthread_mutex_destroy ( pthread_mutex_t *mutex,)	//Destroy mutual-exclusion lock
Mutex: Identification of the mutex to manipulate

Return value: 0 successful, error returns the wrong code.

Second, testing

Write a small example of a test that creates two child threads in the main thread, and when one of the child threads completes, another child thread begins to run. The specific code implementation looks like this:

#include <stdio.h> #include <pthread.h> #include <unistd.h> #include <stdlib.h>/*

Defines a global variable to define a mutex/static pthread_mutex_t mutex;
	* * * Child thread A's thread body */static void *start_routine_a (void *arg) {int loop_cnt = (int) arg;

	int i;		Pthread_mutex_lock (&mutex);

	Process A locks printf ("Thread a:start!\n");
		for (i = 0; i < 5; i++) {printf ("Tech-pro thread A:%d\n", i);
	Sleep (1);

	printf ("Thread a:stop!\n");	Pthread_mutex_unlock (&mutex);		Process a releases the lock Pthread_exit (NULL);
	/* Thread Completion/* * * thread Body b * * * *start_routine_b (void *arg) {int loop_cnt = (int) arg;

	int i;		Pthread_mutex_lock (&mutex);

	Process B locks printf ("Thread b:start!\n");
		for (i = 0; i < 5; i++) {printf ("Tech-pro thread B:%d\n", i);
	Sleep (1);

	printf ("Thread b:stop!\n");	Pthread_mutex_unlock (&mutex);		Process B Releases the lock Pthread_exit (NULL);
	/* Thread completion//* Define two child threads, let two child threads running at the same time * * int main (void) {int ret; pthread_t pthread_a, Pthread_b;
	/* Initialize this mutex/ret = Pthread_mutex_init (&mutex, NULL);
		if (0!= ret) {printf ("Pthread_mutex_init error!\n");
	return-1;

	/* Start/printf ("main:start!\n");
	* * Create two child threads/ret = Pthread_create (&pthread_a, NULL, start_routine_a, (void *) 5);
		if (0!= ret) {printf ("Pthread_create for A is failed!\n");
	return-1;
	ret = Pthread_create (&pthread_b, NULL, Start_routine_b, (void *) 5);
		if (0!= ret) {printf ("Pthread_create for B-is failed!\n");
	return-1;
	}/* Wait for process to end/Pthread_join (pthread_a, NULL);

	Pthread_join (Pthread_b, NULL);

	/* End */printf ("main:stop!\n");	Pthread_mutex_destroy (&mutex);
Destroy the mutex return 0;
 }
To compile the program, note the compilation and link to add-lpthread, the results of the operation are as follows:

As can be seen from the running results, a thread runs first, and the B thread starts running after a thread runs.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.