Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

OS LAB Process Management Ex. No.

– 4

#include "stdafx.h"
#include <Windows.h>
#include <thread>
#include <iostream>

using namespace std;

DWORD WINAPI MyThreadFunction(LPVOID lpParam)


{
cout<<" this is a thread ..."<<endl;
return 0;
}

int _tmain(int argc, _TCHAR* argv[])


{
DWORD dwThreadId, dwThrdParam = 1;
HANDLE hThread;

for(int x = 1; x <= 10; x++)


{
hThread = CreateThread(NULL, 0, MyThreadFunction, &dwThrdParam, 0,
&dwThreadId);
// hThread = CreateThread(
NULL, // default security attributes
0, // use default stack size
MyThreadFunction, // thread function
&dwThrdParam, // argument to thread function
0, // use default creation flags
&dwThreadId); // returns the thread identifier

// Check the return value for success. If something wrong...


if (hThread == NULL)
cout<<"CreateThread() failed, error: \n"<< GetLastError()<<endl;

else //else, gives some prompt...


{
cout<<"It seems that CreateThread() is OK. \n";
cout<<"The thread ID: \n"<< dwThreadId<<endl;
}
if (CloseHandle(hThread) != 0)
cout<<"Handle to thread closed successfully.\n";
} // end for loop...

getchar();

return 0;
}

Asst. Lect. Amthal Khaleel Page 1 of 3


OS LAB Process Management Ex. No. – 4

Prog-2-
#include "stdafx.h"
#include <iostream>
#include <string>
#include <thread>

using namespace std;

// The function we want to execute on the new thread.


void task1(string msg)
{
cout << " task1 says: " << msg;
}

void task2()
{
for(int i=1; i<=20 ; i++)
cout<<" i = "<<i<<endl;
}
void task3()
{
int x,y;
cout<<"enter 2 numbers \n";
cin>>x>>y;
cout<<x*y;

int _tmain(int argc, _TCHAR* argv[])


{

thread thread_1 (task1, "Hello");


thread thread_2 = thread(task2);
thread thread_3 = thread(task3);

thread_2.join();
thread_1.join();
thread_3.join();
/*(Joining means that the thread who invoked the new thread will wait for the new
thread to finish execution, before it will continue its own execution). */

getchar();

return 0;
}

Note: When thread::join() is called, the calling thread will block until the thread of
execution has completed. Basically, this is one mechanism that can be used to know when
a thread has finished. When thread::join() returns, the OS thread of execution has
completed and the C++ threadobject can be destroyed.
The thread::detach() is called, the thread of execution is "detached" from the thread object
and is no longer represented by a thread object - they are two independent things. The
C++ threadobject can be destroyed and the OS thread of execution can continue on. If the
program needs to know when that thread of execution has completed, some other
mechanism needs to be used. join() cannot be called on that thread object any more,
since it is no longer associated with a thread of execution.

Asst. Lect. Amthal Khaleel Page 2 of 3


OS LAB Process Management Ex. No. – 4

Prog-3-
#include "stdafx.h"
// CPP program to demonstrate multithreading using three different callables.
#include <iostream>
#include <thread>
using namespace std;

void func(int a)
{
for (int i = 0; i < a; i++) {
cout << "Thread using function pointer as callable\n";
}
}

// A callable object
class thread_obj {
public:
void operator()(int x)
{
for (int i = 0; i < x; i++)
cout << "Thread using function object as callable\n";
}
};

int main()
{
cout << "Threads 1 and 2 and 3 operating independently" << endl;

// This thread is launched by using function pointer as callable


thread th1(func, 3);

// This thread is launched by using function object as callable


thread th2(thread_obj(), 3);
Note: a lambda expression—
often called a lambda—is a
// Define a Lambda Expression convenient way of defining an
auto f = [](int x) {
for (int i = 0; i < x; i++) anonymous function object
cout << "Thread using lambda expression as callable\n"; right at the location where it is
};
invoked or passed as an
// This thread is launched by using lamda expression as callable
thread th3(f, 3); argument to a function.
Typically lambdas are used to
// Wait for the threads to finish
// Wait for thread t1 to finish encapsulate a few lines of code
th1.join(); that are passed to algorithms
or asynchronous methods. This
// Wait for thread t2 to finish
th2.join(); article defines what lambdas
are, compares them to other
// Wait for thread t3 to finish
programming techniques,
th3.join();
getchar(); describes their advantages, and
return 0; provides a basic example.
}

Asst. Lect. Amthal Khaleel Page 3 of 3

You might also like