I am a beginner on Stack Overflow. I am working on a Unix platform in C/C++. Knowing basic programming in these regards how could I start with multithreading?
Multithreading seems to be very interesting and I want to grow my knowledge in this regard.
How could I get started with multithreading and what are the best techniques/books/ebooks/articles available to grab the concepts as early as possible?
To start a thread we simply need to create a new thread object and pass the executing code to be called (i.e, a callable object) into the constructor of the object. Once the object is created a new thread is launched which will execute the code specified in callable. After defining callable, pass it to the constructor.
In order to create a Thread, first we need to create an Instance of RunnableWorker which implements the Runnable Interface. The above code creates a Runnable Instance r. Then it create 3 threads t1, t2 and t3 and passes r as the argument to the 3 threads. Then the start() function is used to start all the 3 threads.
To execute the c file, we have to use the -pthread or -lpthread in the command line while compiling the file. Syntax: int pthread_create(pthread_t * thread, const pthread_attr_t * attr, void * (*start_routine)(void *), void *arg);
Something else to try: http://www.threadingbuildingblocks.org
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With