Annales UMCS Informatica AI 1 (2003) UMCS. Concurrent programming and futures. Andrzej Daniluk

Similar documents
Annales UMCS Informatica AI 2 (2004) UMCS. OpenMP parser for Ada

Annales UMCS Informatica AI 2 (2004) UMCS. Simple hardware method of data protection. Jerzy Kotliński

Introduction to the DLL for the USB Interface Board K8061

Annales UMCS Informatica AI 1 (2003) UMCS. Using PHP and HTML languages and MySQL databases to create servers of scientific information

Delphi Generics.Collections

UMCS. Annales UMCS Informatica AI 6 (2007) 23-36

Parallel Programming using OpenMP

Parallel Programming using OpenMP

Concurrency, Thread. Dongkun Shin, SKKU

Annales UMCS Informatica AI 1 (2003) UMCS. Registration of CT and MRI brain images. Karol Kuczyński, Paweł Mikołajczak

Questions: ECE551 PRACTICE Final

Annales UMCS Informatica AI 1 (2003) UMCS. Using PHP and HTML languages to create graphical interfaces and to remote control of programs

CS 160: Interactive Programming

Locks. Dongkun Shin, SKKU

CS4411 Intro. to Operating Systems Exam 1 Fall points 10 pages

ADT: Design & Implementation

Tom Ball Sebastian Burckhardt Madan Musuvathi Microsoft Research

Lesson 6 Programming Arduino Example 9.7. Chapter-9 L06: "Internet of Things ", Raj Kamal, Publs.: McGraw-Hill Education

Software Engineering /48

Lecture 5: Implementing Lists, Version 1

Vector and Free Store (Pointers and Memory Allocation)

CMSC 330: Organization of Programming Languages

Concurrency & Parallelism. Threads, Concurrency, and Parallelism. Multicore Processors 11/7/17

Threads, Concurrency, and Parallelism

THREADS & CONCURRENCY

In examining performance Interested in several things Exact times if computable Bounded times if exact not computable Can be measured

Lecture 5: Synchronization w/locks

The New Java Technology Memory Model

Lecture 24 Notes Search in Graphs

Short Notes of CS201

Common Misunderstandings from Exam 1 Material

Programming with MPI

Threads CS1372. Lecture 13. CS1372 Threads Fall / 10

CS201 - Introduction to Programming Glossary By

Concurrent Programming

Process Synchronization

Chapter 5: Process Synchronization. Operating System Concepts 9 th Edition

Autumn 2012 November 7 th 9 th, 2012 Section Solution

ENGI 1020 Introduction to Computer Programming J U L Y 5, R E Z A S H A H I D I

CS61B, Spring 2003 Discussion #17 Amir Kamil UC Berkeley 5/12/03

Background. The Critical-Section Problem Synchronisation Hardware Inefficient Spinning Semaphores Semaphore Examples Scheduling.

CSCI-1200 Data Structures Spring 2018 Lecture 14 Associative Containers (Maps), Part 1 (and Problem Solving Too)

Intro. Scheme Basics. scm> 5 5. scm>

Raspberry Pi Basics. CSInParallel Project

CMSC Computer Architecture Lecture 15: Memory Consistency and Synchronization. Prof. Yanjing Li University of Chicago

Concurrent Programming Lecture 10

Lecture Overview Code generation in milestone 2 o Code generation for array indexing o Some rational implementation Over Express Over o Creating

Hands-On Lab. Multi-Touch WMTouch - Native. Lab version: Last updated: 12/3/2010

Financial computing with C++

C++ And Threads.

Lab 2: ADT Design & Implementation

Learning from Bad Examples. CSCI 5828: Foundations of Software Engineering Lecture 25 11/18/2014

Operational Semantics of Cool

Background. Old Producer Process Code. Improving the Bounded Buffer. Old Consumer Process Code

Lecture 10 Midterm review

CpSc212 Goddard Notes Chapter 10. Linked Lists

CSE 153 Design of Operating Systems

Chapter 6: Process [& Thread] Synchronization. CSCI [4 6] 730 Operating Systems. Why does cooperation require synchronization?

C# Asynchronous Programming Model

THREADS AND CONCURRENCY

Concurrency: Mutual Exclusion (Locks)

C++ Memory Model. Don t believe everything you read (from shared memory)

1 of 6 Lecture 7: March 4. CISC 879 Software Support for Multicore Architectures Spring Lecture 7: March 4, 2008

Pointers in C/C++ 1 Memory Addresses 2

Implementing Coroutines with call/cc. Producer/Consumer using Coroutines

Assertion. C++ Object Oriented Programming Pei-yih Ting NTOU CS

Slide Set 1. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

C++FA 6.1 PRACTICE FINAL EXAM

CPS221 Lecture: Threads

Chapter 6 Parallel Loops

Guaranteeing memory safety in Rust

CSE 4/521 Introduction to Operating Systems

Sets and MultiSets. Contents. Steven J. Zeil. July 19, Overview of Sets and Maps 4

CIS233J Java Programming II. Threads

This is a talk given by me to the Northwest C++ Users Group on May 19, 2010.

Concurrency in Java Prof. Stephen A. Edwards

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

CS18000: Problem Solving And Object-Oriented Programming

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

CS3733: Operating Systems

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

THREADS: (abstract CPUs)

C++: Const Function Overloading Constructors and Destructors Enumerations Assertions

Hands-On Lab. Multitouch Gestures - Native. Lab version: Last updated: 12/3/2010

Pointers, Pointers, Pointers!

RSARTE External C++ Integration

OpenACC Course. Office Hour #2 Q&A

APPLICATION NOTE: KONSTANTER LSP32K Interface Protocol

Solution: a lock (a/k/a mutex) public: virtual void unlock() =0;

Concurrent Programming

FAQ. For ICPDAS DCON (I-7000/8000/87K) series modules [Version 1.0]

Overview. CMSC 330: Organization of Programming Languages. Concurrency. Multiprocessors. Processes vs. Threads. Computation Abstractions

CSE 143. Linked Lists. Linked Lists. Manipulating Nodes (1) Creating Nodes. Manipulating Nodes (3) Manipulating Nodes (2) CSE 143 1

Chapter 17 vector and Free Store

Lesson 6: Process Synchronization

CSE 143. Complexity Analysis. Program Efficiency. Constant Time Statements. Big Oh notation. Analyzing Loops. Constant Time Statements (2) CSE 143 1

Agenda. Threads. Single and Multi-threaded Processes. What is Thread. CSCI 444/544 Operating Systems Fall 2008

struct Properties C struct Types

High Performance Computing Course Notes Shared Memory Parallel Programming

Chapter 6: Process Synchronization. Operating System Concepts 9 th Edit9on

Transcription:

Annales Informatica AI 1 (2003) 303-308 Concurrent programming and futures Andrzej Daniluk Institute of Physics, Maria Curie-Skłodowska University, Pl. M.Curie-Skłodowskiej 1, 20-031 Lublin, Poland Annales Informatica Lublin-Polonia Sectio AI http://www.annales.umcs.lublin.pl/ Abstract If we are manage complexity, we must create a model of the universe. The goal of model is to create a meaningful abstraction of the real world. Such an abstraction should be simpler than the real world but should also reflect accurately the real world so that we can use the model to predict the behavior of things in the real world. The object-oriented software design is about building good models. It consists of two significant pieces: a modelling language and process. In the present paper a new and little-known approach for concurrent programming is presented. 1. Introduction The modelling language is the least important aspect of object-oriented analysis and design; unfortunately, it tends to attract the most attention. A modelling language is nothing more than a convention for how we ll draw our model on paper. The process of object-oriented analysis and design is much more complex and important than the modelling language; as an industry, we ve decided to use the UML (Universal Modelling Language) as a commercial product from Rational Software, Inc. The process of software design is iterative. It means that as we develop software, we go through the entire process repeatedly as we strive for enhanced understanding of the requirements. The design directs the implementation, but the details uncovered during implementation feed back into the design. Most important, we do not try to develop any sizable project in a single, orderly, straight line; rather, we iterate over pieces of the projects, constantly improving our design and refining our implementation [1-2]. The goal of this work is to produce code that meets the stated requirements and that is reliable, extensible, and maintainable. E-mail address: adaniluk@tytan.umcs.lublin.pl

2. Concurrent programming The future of programming is concurrent programming. Not too long ago, sequential, command-line programming gave way to graphical, event-driven programming and now single-threaded programming is yielding to multithreaded programming. Borland C++ Builder and Borland Delphi includes features to support concurrent programming-not as much support as we find in Ada, but more than in most traditional programming languages. In addition to the language features, we can use the Windows API and its threads, process, futures, and so on [1, 3]. 3. Threads and processes A thread is a flow of control in a program. A program can have many threads, each with its own stack, its own copy of the processor register, and related information. On a multiprocessor system, each processor can run a separate thread. On a uniprocessor system, Windows and Linux create the illusion that threads are running concurrently, though only one thread at a time gets to run. A process is a collection of threads all running in a single address space. Every process has at least one thread, called the main thread. Threads in the same process can share resources such as open files and can access any valid memory address in the process address space. 4. The TThread class and BeginThread() function The easiest way to create a multithread application in C++ Builder or Delphi is to write a thread class that inherits from TThread. TThread class is not part of the C++ Builder and Delphi languages, but is declared in the Classes.hpp or Classes.pas units [1, 3-4]. Example 4.1 shows the declaration for the TThread class. Example 4.1. Using the TThread class #ifndef TPrintThreadH #define TPrintThreadH ------------------- #include <StdCtrls.hpp> #include <ExtCtrls.hpp> #include <Dialogs.hpp> #include <Forms.hpp> #include <Controls.hpp> #include <Graphics.hpp> #include <Classes.hpp> #include <SysUtils.hpp> #include <Messages.hpp> #include <Windows.hpp>

#include <System.hpp> ------------------- class TTPrintThreadForm : public TForm published: TButton *Button1; TLabel *Label1; TBevel *Bevel1; TBevel *Bevel2; TBevel *Bevel3; TLabel *Label2; TLabel *Label3; void fastcall ButtonPrint(TObject *Sender);... private: int ThreadsRunning;... void fastcall ThreadDone(TObject *Sender); public: void fastcall PrintArray(TPaintBox *Box, const int *A, const int A_Size);... ; ------------------- typedef int TPrintArray[225]; typedef TPrintArray *PPrintArray; ------------------- extern TTPrintThreadForm *TPrintThreadForm; ------------------- #endif If we don t want to write a class, we can use BeginThread() function. We are wrappers for the Windows API calls CreateThread() function, but we must use C++ Builder s and Delphi s functions instead of the Windows API directly. C++ Builder and Delphi keeps a global flag, IsMultiThread, which is true if our program calls BeginThread() or starts a thread using TThread class. C++ Builder and Delphi check this flag to ensure thread safety when allocating memory. If we call the CreateThread() function directly be sure to set IsMultiThread to true. Example 4.2 shows the declaration for the BeginThread() function. Example 4.2. Using the BeginThread() function for calculate of sinl() function #include <vcl.h> #include <math.h> #pragma hdrstop #include "Unit_26.h"

#pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; LONG Run_Thread = 0; UINT Thread_ID; HANDLE hevent; SECURITY_ATTRIBUTES security_attrib = sizeof(security_attributes), NULL, TRUE ; fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) void fastcall TForm1::FormCreate(TObject *Sender) Memo1->Lines->Clear(); int fastcall Count_Sin(LPVOID Parameter) long double y, x=0.0; for(;;) y = sinl(x*m_pi/180); Form1->Memo1->Lines->Add(FloatToStr(x)+ " "+FloatToStr(y)); WaitForSingleObject((LPVOID)Run_Thread, 100); x += 1.0; // x = x + 1 void fastcall TForm1::Button1Click(TObject *Sender) Run_Thread = BeginThread(&security_attrib, 4096, Count_Sin, this, CREATE_SUSPENDED, Thread_ID); if(run_thread == (int)invalid_handle_value) MessageBox(0, "Thread ERROR", "ERROR", MB_OK); else

ResumeThread((LPVOID)Run_Thread); hevent = CreateEvent(NULL, FALSE, FALSE, NULL); if (hevent) WaitForSingleObject(hEvent, 100 /*ms*/ ); CloseHandle(hEvent); Button1->Enabled = FALSE; void fastcall TForm1::Button2Click(TObject *Sender) SuspendThread((LPVOID)Run_Thread); void fastcall TForm1::Button4Click(TObject *Sender) ResumeThread((LPVOID)Run_Thread); void fastcall TForm1::Button3Click(TObject *Sender) CloseHandle((LPVOID)Run_Thread); Application->Terminate(); 5. Futures Writing a concurrent program can be more difficult than writing a sequential program. We need to think about race conditions, synchronization, shared variables, and others. Futures help reduce the intellectual clutter of using threads. A future is an object that promises to deliver a value sometime in the future. The application does its work in a main thread and calls upon futures to fetch or compute information concurrently. The future does its work in a separated thread, and when the main thread needs the information, it gets from the future object. For example, we can define the future class by inheriting from TFuture and overriding the Compute method. The Compute method does whatever work is necessary and returns its result as a Variant type [2]. Example 5.1 shows the declaration for the TFuture class. Example 5.1 Declaration of the TFuture class class TFuture private:

System::TObject* fastcall ExceptObject(void); void * fexceptaddr; HANDLE fhandle; bool fterminated; LongWord fthreadid; LongWord ftimeout; Variant fvalue; bool fastcall GetIsReady(void); bool fastcall GetValue(void); protected: void fastcall RaiseException(void); public: virtual void fastcall AfterConstruction(void); Variant fastcall Compute(void); void fastcall Terminate(void); property HANDLE handle=read=fhandle; property bool IsReady=read=GetIsReady; property bool Terminated=read=fTerminated, write=fterminated; property LongWord ThreadID=read=fThreadID; property LongWord TimeOut=read=fTimeOut, write=ftimeout; property Variant Value=read=GetValue; ; When the application needs the future, it reads the Value property and the GetValue method waits until the thread is finished. If the thread raised an exception, the future object reraises the same exception object at the original exception address. If in application everything goes as planned, the future value is returned as a Variant type. 6. Summary In this paper the technique of programming by futures is presented. The major advantage of using futures is their simplicity. We can often implement the TFuture-derived class as a simple, linear subroutine. Using a future is as simple as accessing a property. All the synchronization is handled automatically by TFuture. Concurrent programming can be tricky, but with care and caution, we can write application that uses threads and process correctly, efficiently, and effectively. Using a future is as simple as accessing a property. It is worth noticing that the synchronization is handled automatically by futures. References [1] Lischner R., Delphi in a Nutshell, O Reilly & Associates, Inc., (2000). [2] Liberty J., Teach Yourself C++ in 21 Days (4th Edition), SAMS, (2000). [3] Daniluk A., C++ Builder 6. Ćwiczenia zaawansowane, Helion, (2003), in Polish. [4] Daniluk A., C++ Builder. Kompendium programisty, Helion, (2003), in Polish. Powered by TCPDF (www.tcpdf.org)