Book Image

Raspberry Pi Computer Architecture Essentials

By : Andrew K. Dennis, Teemu O Pohjanlehto
Book Image

Raspberry Pi Computer Architecture Essentials

By: Andrew K. Dennis, Teemu O Pohjanlehto

Overview of this book

With the release of the Raspberry Pi 2, a new series of the popular compact computer is available for you to build cheap, exciting projects and learn about programming. In this book, we explore Raspberry Pi 2’s hardware through a number of projects in a variety of programming languages. We will start by exploring the various hardware components in detail, which will provide a base for the programming projects and guide you through setting up the tools for Assembler, C/C++, and Python. We will then learn how to write multi-threaded applications and Raspberry Pi 2’s multi-core processor. Moving on, you’ll get hands on by expanding the storage options of the Raspberry Pi beyond the SD card and interacting with the graphics hardware. Furthermore, you will be introduced to the basics of sound programming while expanding upon your knowledge of Python to build a web server. Finally, you will learn to interact with the third-party microcontrollers. From writing your first Assembly Language application to programming graphics, this title guides you through the essentials.
Table of Contents (18 chapters)
Raspberry Pi Computer Architecture Essentials
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
Index

Going further – mutexes and joins


We touched upon mutexes and what they are; now we are going to implement a C program that demonstrates how they work.

Create a new file called third_c_prog.c inside the c_programs directory.

vim third_c_prog.c

Add the following code to this file:

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>


pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
pthread_t thread_id[4];
int counter = 0;

void *thread_processor()
{
 pthread_mutex_lock( &mutex1 );
 counter++;
 printf(" Counter: %d\n",counter);
 pthread_mutex_unlock( &mutex1 );
}

int main(void)
{

int i = 0;
int error;

  while(i < 4) {

  error = pthread_create(&(thread_id[i]), NULL, &thread_processor, NULL);
  if (error != 0)
  {
      printf("\nthere was a problem creating thread: %s", strerror(error));
  }
  else
  {
      printf("\n Thread number %d created.\n", i);
  }
  pthread_join( thread_id[i], NULL);
  i++;
 }

}

This program is a variation on the first...