Example pthread demo

From wiki.emacinc.com
Jump to: navigation, search
TODO: {{#todo:SEOKWREV;(12.11.13-12:03->JG+);(12.11.13-17:25->MD-);(12.12.13-11:25->JG+);(12.31.13-12:45->MD+);(04.07.14-09:50->BS+);(04.10.14-11:00->BS+)|Jgreene|oe 4,oe 5,jg,md,SEOKWREV,mg,bs}}

This is a guide to the pthread_demo C example project included in the EMAC OE SDK.

This is a simple pthreads example application. A single thread is created to generate random data that is read by a specified number of reader threads.

The term pthread refers to POSIX Threads, a POSIX standard for threads.

The pthread_demo project builds one executable: p_thread_demo.

Opening, Building and Uploading the Project Files

For information on opening the project from within Eclipse, please see Importing the EMAC OE SDK Projects with Eclipse. Then, follow Using the EMAC OE SDK Projects with Eclipse for information on how to build, upload and execute the example.

Alternatively, the Makefile can be used with the make command from the commandline to build and upload the example. For information on this method, please see Using EMAC OE SDK Example Projects.

EMAC SDK 5.X

For information on opening the project from within QtCreator, please see QtCreator: Adding Source Files. Then, follow Getting Started With Qt Creator for information on how to build, upload and execute the example.

Alternatively, the CMakefile.txt can be used with the cmake command from the commandline to build and upload the example. For information on this method, please see Getting Started with the EMAC OE SDK.

Code can be found at http://git.emacinc.com/OE/example-projects

Usage and Behavior

The pthread_demo project is an example of multithreaded code for generating and interacting with an arbitrary number of pthreads. Within the pthread_demo.c C source file can be found demonstrations of how to, specifically:

  • Create pthreads, line 90.
  • Write to pthreads, line 141.
  • Read from pthreads, line 127.
  • Terminate and clean up after pthreads, line 119.

Hardware Requirements

The pthread_demo C example project has no special hardware requirements. It should run just fine on any system for which the project files can be successfully compiled.

Using pthread_demo

./pthread_demo n

where n is the number of threads

Usage Example

root@PPCE7:/tmp# ./pthread_demo 3
generating new random data
generating new random data
    reader: count = 0, data = 693451072
generating new random data
    reader: count = 1, data = 1195545710
generating new random data
    reader: count = 2, data = 462104121

Usage and Behavior for Ipac-9x25 Examples

The p_hello project is an example of multithreaded code for generating and interacting with an arbitrary number of pthreads. Within the p_hello.c C source file can be found demonstrations using the pthread library.


Using p_hello

./p_hello n

where n is the number of threads

Usage Example

root@PPCE7:/tmp# ./p_hello 3
Hello from node 0
Hello from node 1
Hello from node 2


Summary

The pthread_demo C example project demonstrates how to use pthreads. For another example of pthread usage see Example pthread hello.