10
Parallel programming. Threads BOTNARI NICOLAE FAF-141 UTM

Botnari Nicolae - Threads

Embed Size (px)

DESCRIPTION

Threads in c++

Citation preview

Page 1: Botnari Nicolae - Threads

Parallel programming. Threads

BOTNARI NICOLAEFAF-141UTM

Page 2: Botnari Nicolae - Threads

Threads

A Thread is an independent stream of instructions that can be schedule to run as such by the OS.

Think of a thread as a “procedure” that runs independently from its main program.

Multi-threaded programs are where several procedures are able to be scheduled to run simultaneously and/or independently by the OS.

A Thread exists within a process and uses the process resources.

Page 3: Botnari Nicolae - Threads

Why Use Pthreads

The primary motivation behind Pthreads is improving program performance.

Can be created with much less OS overhead.

Needs fewer system resources to run. The procceses are running in parralel.

Page 4: Botnari Nicolae - Threads

Pthread Management – Creating Threads

The main() method comprises a single, default thread.

pthread_create() creates a new thread and makes it executable.

The maximum number of threads that may be created by a process in implementation dependent.

Once created, threads are peers, and may create other threads.

Page 5: Botnari Nicolae - Threads

Pthread Management – Terminating Threads

Several ways to terminate a thread: The thread is complete and returns The pthread_exit() method is called The pthread_cancel() method is invoked The exit() method is called

The pthread_exit() routine is called after a thread has completed its work and it no longer is required to exist.

Page 6: Botnari Nicolae - Threads

Terminating Threads

If the main program finishes before the thread(s) do, the other threads will continue to execute if a pthread_exit() method exists.

The pthread_exit() method does not close files; any files opened inside the thread will remain open, so cleanup must be kept in mind.

Page 7: Botnari Nicolae - Threads

Pthread Examplevoid enemyy(void*){

Texture tileSet, moveplatform, megaman, fang;

tileSet.loadFromFile("files/mario.png");

Level lvl;

lvl.LoadFromFile("plat_test.tmx");

 

AnimationManager anim3;

anim3.create("move",tileSet,0,0,16,16,2,0.002,18);

anim3.create("dead",tileSet,58,0,16,16,1,0);

 

std::list<Entity*> entities;

std::list<Entity*>::iterator it;

 

std::vector<Object> e = lvl.GetObjects("enemy");

 

for (int i=0;i < e.size();i++)

entities.push_back(new ENEMY(anim3, lvl, e[i].rect.left, e[i].rect.top) );

}

pthread_create(&pth, NULL, mus, NULL);

pthread_join(pth, NULL);

Page 8: Botnari Nicolae - Threads

About SFML library

Simple and Fast Multimedia Library (SFML) is a cross-platform software development library designed to provide a simple interface to various multimedia components in computers. It is written in C++ with bindings available for C, Java, Python, Ruby and others languages.

Page 9: Botnari Nicolae - Threads

Why SFML ?

SFML consists of various modules:

I. System (Vector and Unicode string classes, portable threading and timer facilities)

II. Window(Window and input device management including support for joysticks, OpenGL context management)

III. Graphics(Hardware-accelerated 2D graphics including sprites, polygons and text rendering)

IV. Audio(Hardware-accelerated spatialised audio playback and recording)

V. Network(TCP and UDP sockets, data encapsulation facilities, HTTP and FTP classes)

Page 10: Botnari Nicolae - Threads

Exemple…

Thank you for listening