Ask your own question, for FREE!
Computer Science 19 Online
OpenStudy (anonymous):

What is a thread?

OpenStudy (anonymous):

It's a parallel execution path that executes code in parallel (at the same time) along with other code. It's the smallest unit of execution that an OS can schedule - basically, it encapsulates a set of operations (code) and storage, many of which can be handed to the OS, which then takes care of executing them in parallel based on available CPU resources. Consider the example of an application with a UI that can perform some expensive operation upon request. You want the UI to stay functional while performing this expensive operation, so in essence you want the code that processes UI events to run in parallel to the code that processes the expensive operation. One way to do this would be cooperatively - the expensive operation could call a function in the UI code periodically, so the UI can update; that's messy though, and depending on the operation, the frequency of these calls could be unpredictable. Instead, you could execute the expensive operation on a separate thread, and the UI code would run in parallel with it. The OS and CPU take care of scheduling time for each of the threads and making sure they run on separate cores if available. Also consider a loop that processes a million elements of an array and does expensive math on them. You could do this in a single for loop. Alternatively, you could split the loop in two and run the first half in one thread and the second half in another. If the code runs on a multi-core CPU, it will end up being twice as fast, because both threads will likely be scheduled on independent CPU cores and run in parallel.

OpenStudy (anonymous):

I haven't encountered threads yet :( when do I learn how to build multi-threaded stuff in C/C++/Java/Python etc.

OpenStudy (anonymous):

Multithreaded programming is easy for simple cases like the ones mentioned above, but gets *really* complicated for more complex code. You end up with race conditions, needing locks or lock free data structures if several threads want to access the same memory locations at the same time, the problem of how to divide your computational work into jobs that can be executed in separate threads so that the work is done most efficiently (with the least amount of waiting of one thread for another needed, for example). The easiest way to start is to pick a simple loop that processes data, and try to run it in several threads. From there, you could pick increasingly more complex algorithms and try to make them more efficient by multithreading them. Since threads are OS dependent, you'll have to get used to a threading API. pthreads is relatively easy to use, free, and cross-platform.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!