Skip to main content

Posts

Programming: Windows Threading Vs. Linux Threading (Part 4)

Thread Cancellation is a way where it's necessary sometimes to terminate a thread explicitly from another thread. In Windows we have a Function, it's TerminateThread() The function signature : BOOL WINAPI TerminateThread(   _Inout_  HANDLE hThread,   _In_     DWORD dwExitCode ); This function is always asynchronous. It never guarantees that the thread will always terminate by the time the function returns. This function never cleans up a stack of destroyed threads. So, it's not recommended to use this function until it's absolutely necessary. Note : As mentioned by Jeffrey Richter, Microsoft designed this API purposely implemented this way to terminate thread(s). This ensures that other threads are still running and accessing the terminated threads stack will not get an access violation. So, by leaving the terminated threads stack in memory, other threads which are accessing the stack of the killed thread can continue to execute. In Linux we have a function, i

Programming: Windows Threading Vs Linux Threading (Part 3)

Continued from Part 2: In Linux, normally (when we use default pthread_attribute , aka sending null to pthread_create()), we're creating the  joinable thread . There is another type of thread named detached thread , which We'll see later. A joinable thread is like a process, isn't automatically cleaned up by GNU/Linux when it terminates. Instead, its exit states hang around the system (like a zombie process) until the thread called pthread_join() obtains its return value. Let's see what happens in the Windows world: When the thread terminates (the normal way, aka the thread function returns), it ensures the following : 1. All C++ objects created in the thread function will be destroyed properly via their destructor. 2. The OS will clean up memory owned by the thread's stack. 3. The system will decrement threads kernel objects usage count. The following code snippet (GNU/Linux) shows a thread created using pthread_create() with default pthread_attr_t

Programming: Windows Threading Vs Linux Threading (Part 2)

This is in continuation of the previous article 'Windows Threading Vs. Linux Threading ', here we'll see a subtle but significant difference in the  thread start routine which gets a call from CreateThread() API in the Windows world or pthread_create() function call in the Linux world. The ThreadProc function in Windows : DWORD WINAPI ThreadProc(   _In_  LPVOID lpParameter ); This is an application-defined function that serves as the starting address for a thread. Note: As per MSDN, Do not declare this callback function with a void return type and cast the function pointer to LPTHREAD_START_ROUTINE when creating the thread. Code that does this is common, but it can crash on 64-bit Windows . The return value indicates the success or failure of this function. The return value should never be set to STILL_ACTIVE (259). In Linux : The function passed as start_routine in the pthread_create() function should correspond to the following C function prototype:  void

Programming: Windows Threading Vs Linux Threading

In this article, I'm trying to show some differences between Windows and Linux thread creation and their respective usage. Creating Threads: 1.     a. In Windows: CreateThread() API is used to create a thread to execute within the virtual address space of the calling process.     b. In Linux: pthread_create() function creates a thread.     Function Signature:     // Windows     HANDLE WINAPI CreateThread(           _In_opt_   LPSECURITY_ATTRIBUTES lpThreadAttributes,           _In_       SIZE_T dwStackSize,           _In_       LPTHREAD_START_ROUTINE lpStartAddress,           _In_opt_   LPVOID lpParameter,           _In_       DWORD dwCreationFlags,           _Out_opt_  LPDWORD lpThreadId     );     Function Signature:     // Linux     int pthread_create(pthread_t *thread, const pthread_attr_t *attr,          void *(*start_routine)(void *), void *arg);     In Windows: We can pass stack size in bytes if required to the same CreateThread() API.     In Linux:  The stack size is set in

A STL vector usage through shared_ptr

We know vector can store homogeneous kind of stuff. That is, it can store type int, string etc individually but not element of type int with string etc. However, sometimes we might have requirements that it can store elements which are in inheritance relation and can do stuff by calling function of respective element stored in vector. As an example, I've a Shape class from which I've derived Circle, Polygon etc. Now I want to store Circle, Polygon object in a vector and then access each element by its iterator and draw shape accordingly. C++ 2011 made it possible via shared_ptr. Thanks to Stephan aka STL. Code snippet #include < iostream > #include < memory > #include < vector > using namespace std; class Shape { public:     Shape(){}     virtual ~Shape(){}     virtual void draw(int x, int y)     {         cout << "Shape::draw(" << x << ", " << y << ")" << endl;     } private:     Shape(const Sh

How do we list out files in Recycle Bin (Windows 7)

In this post, I tried to enumerate files within recycle bin using a small Win32 program. As we know that recycle bin is a special folder on Windows File System. The location of this directory is not in the registry; it is marked with hidden and system attributes to prevent the user from moving or deleting it. The steps to list out the contents of Recycle bin are as below: 1. It's a special folder denoted by CSIDL_BITBUCKET, which we need to pass to the function SHGetFolderLocation() method. 2. Source code snippet, I've avoided checks as much as possible to make the code simple and clean: int _tmain(int argc, _TCHAR* argv[]) {     LPITEMIDLIST pidlWinRecycleFiles    = NULL;     LPITEMIDLIST pidlItems                = NULL;     IShellFolder *psfWinRecycleFiles    = NULL;     IShellFolder *psfDeskTop            = NULL;     LPENUMIDLIST ppenum                    = NULL;     STRRET strDispName;     TCHAR pszParseName[MAX_PATH];     ULONG celtFetched;     HRESULT hr;     hr

Locking a physical disk on windows....

In this, I've tried to lock a physical disk using a small Windows C++ program. The steps to follow to achieve this are: 1. Get the Physical Drive and volume mapping. Say, the computer is attached with three physical drives, and we're interested to lock the Physical Drive 1 ("\\\\.\\PhysicalDrive1"). Then we need to figure out how many volumes are there on that physical disk. 2. Then Lock that volume one by one using control code FSCTL_LOCK_VOLUME . 3. Do the stuff we'd like to perform on the disk and then unlock each volume using control code  FSCTL_UNLOCK_VOLUME . 4. Close disk and volume handle(s). Few things to remember here. As per Microsoft documentation, a. The NTFS file system treats a locked volume as a dismounted volume. b. Lock volume call will fail with Access Code 5 (Access Denied) if the volume is in use. If we're not sure who's using the volume, just unmount it once. c. The FSCTL_DISMOUNT_VOLUME control code functions similarl