To see me speak about these topics and go over the code, please watch this youtube video.
Introduction
In another blog post, we learned about what multithreading is and how to do multithreading in C++. Please refer to that blog post if you're a beginner.
In this blog post, we're going to be looking at advanced C++ multithreading examples. Starting Out - No Multithreading
So let's say that we have a particular function that we want to use a bunch of times. This function is called srcmakeMulti and can be seen in the following example.
In this particular example, we call the function three times.
We can compile and run this code with the following two lines, in your terminal. We'll be using this command to run all of the following code, as well.
g++ main.cpp -std=c++11 -pthread -o run ./run Use Multithreading, But Notice The Issue
That's great, but what if we have to call the function many, many times? We could wait for the functions to execute sequentially, but it would be better to take advantage of multithreading to execute the function as often as we can, to finish quicker.
Fantastic, our code will be much faster now (assuming that our CPU has extra threads available), but we have some issues...
The problem is that we're using shared resources (the 'num' variable and 'cout') between multiple threads, and that leads to errors during execution. If you run the above code a few times, it's obvious that what gets outputted to the console isn't correct, and you get a different answer every time. (If you had one phone and three people, they can't all use the phone at the same time, right?) Mutexes - Shared Resources Between Multiple Threads
The way we handle this is by "locking" parts of our code while it's using a shared resource. We do this using mutexes. A mutex will not let another thread access the part of the locked code until the mutex finishes executing that piece of code and unlocks it. Notice the four lines of additional code in the following example, and notice that the output is what we expect.
Better For Some Purposes: Async and Futures
If you simply need to spin some work off on separate thread, and do something else while waiting for the return result, then use async and futures. Basically, async does some work on another thread, and the future facilitates the async so that when the task is complete, you can get the return value in the part of your code that you need it.
You can read up on the documentation for futures and async yourself, but for the most part, follow the syntax shown above.
Final Pro-Tips
There are a few protips that we should adhere to to ensure that our code is good. These tips are heavily inspired by this article, so read the article after this.
Basically, be careful with your multithreading architecture, if your project is large.
To see me go over the code and speak about the topic, watch the following video:
Like this content and want more? Feel free to look around and find another blog post that interests you. You can also contact me through one of the various social media channels.
Twitter: @srcmake Discord: srcmake#3644 Youtube: srcmake Twitch: www.twitch.tv/srcmake Github: srcmake
To watch the youtube video where I go through the demo with you, please click here.
What are Browser Extensions?
Extensions (also known as Addons) help our Web Browsers (such as Chrome and Firefox) perform specific tasks that are useful to us. For example, I use an Adblock extension that, anytime a webpage loads, is called and blocks all advertisements from loading.
There are many useful reasons to use a browser extension, mainly to get helpful functionality that you'd like for certain websites, purposes, or the web browser itself. Making Our Own FireFox Addon
In another post, we looked at making Google Chrome Extensions. In this article, we're going to make a FireFox extension (addon).
Our addon is going to add a red border to all webpages on srcmake.com. As with the Chrome extension, we need three simple files.
First, create a file called "manifest.json" that defines the features and entrypoints of our app. Inside of it, add the following code:
The first few lines just define the name, version, and description of our app. Simple. Next, we define a 48x48 png file for our icon. Then, in content_scripts, we've got two important features:
Here's the png file; download it and rename it to src48x48:
Create a file named "srcborder.js" file and add the following code to it (which will give a webpage a red border):
And that's it. Those three files make up our Firefox addon. To test it, type to "about:debugging" in the address bar of your FireFox browser and hit enter. Then click "Load Temporary Add-on" and choose any file inside of your project folder to load the addon into your browser. To ensure that it's working, reload this page and look at the red border that appears. Conclusion
Making a FireFox addon/extension was super simple. Because the method is so similar to developing a Chrome extension, you can easily make one extension that works for both web browsers.
What we talked about covers on the basics of Extensions. We'll eventually look in-depth at making a useful extension with useful functionality. Stay tuned for new articles and youtube videos.
Watch the following video if you'd like to see me make the FireFox demo described in this article, with you:
Like this content and want more? Feel free to look around and find another blog post that interests you. You can also contact me through one of the various social media channels.
Twitter: @srcmake Discord: srcmake#3644 Youtube: srcmake Twitch: www.twitch.tv/srcmake Github: srcmake
To see a video of me making a chrome extension with you, please watch this youtube video.
What Are Browser Extensions?
Extensions (also known as Addons) help our Web Browsers (such as Chrome and Firefox) perform specific tasks that are useful to us. For example, I use an Adblock extension that, anytime a webpage loads, is called and blocks all advertisements from loading.
There are many useful reasons to use a browser extension, mainly to get helpful functionality that you'd like for certain websites, purposes, or the web browser itself. Our Own Chrome Extension
We're going to be making our own Chrome Extension. Just a simple one, for the sake of showing that we can do it. Later (in another article), we'll expand upon our knowledge to make a more useful extensiion.
An Extension is...basically the same thing as a webpage. We need some HTML, CSS, JavaScript, and any necessary media files (such as PNG pictures). If you haven't already, go ahead and set up an empty project folder. You can download this project on github, but there isn't much code so just keep reading.
The first thing we need is a "manifest.json" file. Create it. This manifest file basically describes our extension and declares some actions and permissions that we may need. Ours will be pretty basic.
You can see that we specify the html for a tiny popup window, and icon for chrome to display. Let's define this "hello.html" file:
Finally, make sure you have the icon, which is a 48x48 png file named "src48x48". (Save the picture below and rename it if you choose not to download the project on github.
Okay so our project folder is setup. Now we need to test this in Chrome. In chrome's address bar, type "chrome://extensions". Make sure "Developer Mode" is checked, and click "Load Unpacked Extension". Locate your project folder, and make sure your extension is enabled.
Now, the extension should be able to be used. On the top right of your Chrome menu, click the icon.
And that's it. We made a Chrome Extension.
To continue learning about Chrome Extensions, see the reference list at the end of this article. To see the article where we make a FireFox extension, please click here.
To see me walk through this demo with you, please watch the following youtube video:
Like this content and want more? Feel free to look around and find another blog post that interests you. You can also contact me through one of the various social media channels.
Twitter: @srcmake Discord: srcmake#3644 Youtube: srcmake Twitch: www.twitch.tv/srcmake Github: srcmake |
AuthorHi, I'm srcmake. I play video games and develop software. Pro-tip: Click the "DIRECTORY" button in the menu to find a list of blog posts.
License: All code and instructions are provided under the MIT License.
|