0% found this document useful (0 votes)
17 views6 pages

Lab 08

g

Uploaded by

girodil346
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views6 pages

Lab 08

g

Uploaded by

girodil346
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Lab 8: Thread Creation and Management using

Windows API
Objective

 Understanding Windows APIs Functions related to Thread Management


 Implementation of related functions
 Study of related parameters to observe the flexibility of Windows OS

Current Lab Learning Outcomes (LLO)


By completion of the lab the students should be able to
1. Understanding Windows APIs Functions related to Thread Management
2. Implementation of related functions

Lab Requirements
Technical Requirements (software/ hardware).
Tools

Lab Assessment
Lab Assignment

Lab Description

e.g Windows Threads

 A thread is an independent unit of execution within a process.


 The multithreaded programming challenge requires organization and coordination of
thread execution to simplify programs and to take advantage of the inherent parallelism
of the host computer.
 Traditionally, programs execute as a single thread of execution. While several
processes can execute concurrently, and even interact through mechanisms such as
shared memory single-threaded processes have several disadvantages.
 single-threaded processes have several disadvantages:

 It is expensive and time consuming for the OS to switch running processes.


 Except in the case of shared memory, processes are not tightly coupled to one
another, and it is difficult to share resources.

 It is difficult and inefficient for single-threaded processes to manage several


concurrent and interacting tasks.

Thread Management

CreateThread
 The CreateThread system call creates an executable thread in the calling process's
address space.

 has several unique requirements:

 Specify the thread's start address within the process's code.

 Specify the stack size, and the stack space is allocated from the process's virtual
address space.

 Specify a pointer to an argument for the thread.

 CreateThread returns a thread's ID value and its handle. A NULL handle value
indicates a failure.

HANDLE CreateThread ( PSECURITY_ATTRIBUTES lpsa,


DWORD dwStackSize,
LPTHREAD_START_ROUTINE lpStartAddr,
LPVOID lpThreadParm,
DWORD dwCreationFlags,
LPDWORD lpThreadId)
Parameters:
 lpsa is the familiar security attributes structure

 dwStackSize is the byte size of the new thread's stack. Use 0 to default to the primary
thread's stack size.

 lpStartAddr points to the function (within the calling process) to be executed. This
function accepts a single pointer argument and returns a 32-bit DWORD exit code. The
thread can interpret the argument as a DWORD or a pointer. The thread function
signature, then, is as follows:

 DWORD WINAPI ThreadFunc (LPVOID)


 lpThreadParm is the pointer passed as the thread argument and is interpreted by the
thread, normally as a pointer to an argument structure.

 dwCreationFlags, if 0, means that the thread is ready to run immediately. If


dwCreationFlags is CREATE_SUSPENDED, the new thread will be in the suspended
state, requiring a ResumeThread function call to move the thread to the ready state.

 lpThreadId points to a DWORD that receives the new thread's identifier. The pointer
can also be NULL, indicating that no thread ID will be returned.

Terminate Thread
 All threads in a process can terminate themselves using the ExitThread function.

 VOID ExitThread (DWORD dwExitCode)

 When the last thread in a process terminates, the process itself terminates.

 One thread can terminate another thread with the TerminateThread function

Thread Identity
 You can obtain thread IDs and handles using functions that are similar to those used
with processes.

 GetCurrentThread returns a non-inheritable pseudohandle to the calling thread.

 GetCurrentThreadId obtains the thread ID, rather than the handle.

 GetThreadId obtains a thread's ID from its handle;

Suspending and Resuming Threads


 Every thread has a suspend count, and a thread can execute only if this count is 0.

 One thread can increment or decrement the suspend count of another thread using
SuspendThread and ResumeThread. Recall that a thread can be created in the
suspended state with a count of 1.

 DWORD ResumeThread (HANDLE hThread)

 DWORD SuspendThread (HANDLE hThread)

Waiting for Threads to Terminate


 One thread can wait for another thread to terminate in the same way that threads wait
for process termination.
 Use a wait function (WaitForSingleObject or WaitForMultipleObjects) using thread
handles.

CreateThread code

#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include <time.h>

DWORD WINAPI thread1 (void *);


DWORD WINAPI thread2 (void *);

DWORD main (DWORD argc, LPTSTR argv [])


{
DWORD ThId;
HANDLE thread1_h, thread2_h;
/* Create the two threads. */
thread1_h = CreateThread (NULL, 0, thread1, NULL, 0,
&ThId);
thread2_h = CreateThread (NULL, 0, thread2, NULL, 0,
&ThId);

/* Wait for the two threads to complete. */


WaitForSingleObject (thread1_h, INFINITE);
WaitForSingleObject (thread2_h, INFINITE);
printf ("thread1 and thread2 threads terminated\n");
return 0;
}

DWORD WINAPI thread1 (void *arg)


{
for(int i=0;i<10;i++)
printf("############### \n");
return 0;
}

DWORD WINAPI thread2 (void *arg)


{
for(int j=0;j<10;j++)
printf("*************** \n");
return 0;
}
LAB Assignment
Write a program that runs two threads, each one of them prints on the screen a counter
value (which starts from 0 until a user input value(20 for example)) associated with its
thread ID. Make an infinite loop in the main program associated with a sleep function to
print the state of each thread.

Print the output screen which will have

 states (by main) and


 counter/threadID (by threads).

Repeat the above two times and observe the order of execution of each thread
and comment.

You might also like